porttcp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * FreeModbus Libary: RT-Thread Port
  3. * Copyright (C) 2019 flybreak <guozhanxin@rt-thread.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id: portserial.c,v 1.60 2019/07/11 17:04:32 flybreak $
  20. */
  21. #include "port.h"
  22. #ifdef PKG_MODBUS_SLAVE_TCP
  23. /* ----------------------- Modbus includes ----------------------------------*/
  24. #include "mb.h"
  25. #include "mbport.h"
  26. #include "tcpserver.h"
  27. /* ----------------------- Defines -----------------------------------------*/
  28. #define MB_TCP_DEFAULT_PORT 502
  29. #define MB_TCP_BUF_SIZE ( 256 + 7 )
  30. /* ----------------------- Static variables ---------------------------------*/
  31. static tcpclient_t mb_client;
  32. static UCHAR prvvTCPBuf[MB_TCP_BUF_SIZE];
  33. static USHORT prvvTCPLength;
  34. static void tcpserver_event_notify(tcpclient_t client, rt_uint8_t event)
  35. {
  36. static rt_tick_t recv_tick = 0;
  37. switch (event)
  38. {
  39. case TCPSERVER_EVENT_CONNECT:
  40. if (mb_client == RT_NULL)
  41. {
  42. mb_client = client;
  43. }
  44. else
  45. {
  46. if(rt_tick_get() - recv_tick > 30 * RT_TICK_PER_SECOND) /* set timeout as 30s */
  47. {
  48. tcpserver_close(mb_client);
  49. mb_client = client;
  50. recv_tick = rt_tick_get();
  51. }
  52. else
  53. {
  54. tcpserver_close(client);
  55. rt_kprintf("Multi-host is not supported, please disconnect the current host first!\n");
  56. }
  57. }
  58. break;
  59. case TCPSERVER_EVENT_RECV:
  60. if( mb_client == client)
  61. {
  62. recv_tick = rt_tick_get();
  63. prvvTCPLength = tcpserver_recv(mb_client, &prvvTCPBuf, MB_TCP_BUF_SIZE, 100);
  64. if (prvvTCPLength)
  65. {
  66. xMBPortEventPost(EV_FRAME_RECEIVED);
  67. }
  68. }
  69. break;
  70. case TCPSERVER_EVENT_DISCONNECT:
  71. if (mb_client == client)
  72. mb_client = RT_NULL;
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. BOOL
  79. xMBTCPPortInit(USHORT usTCPPort)
  80. {
  81. struct tcpserver *serv;
  82. if (usTCPPort == 0)
  83. usTCPPort = MB_TCP_DEFAULT_PORT;
  84. serv = tcpserver_create(0, usTCPPort);
  85. tcpserver_set_notify_callback(serv, tcpserver_event_notify);
  86. return TRUE;
  87. }
  88. void
  89. vMBTCPPortClose(void)
  90. {
  91. tcpserver_destroy(mb_client->server);
  92. }
  93. void
  94. vMBTCPPortDisable(void)
  95. {
  96. tcpserver_close(mb_client);
  97. }
  98. BOOL
  99. xMBTCPPortGetRequest(UCHAR **ppucMBTCPFrame, USHORT *usTCPLength)
  100. {
  101. *ppucMBTCPFrame = &prvvTCPBuf[0];
  102. *usTCPLength = prvvTCPLength;
  103. return TRUE;
  104. }
  105. BOOL
  106. xMBTCPPortSendResponse(const UCHAR *pucMBTCPFrame, USHORT usTCPLength)
  107. {
  108. rt_int16_t ret;
  109. BOOL bFrameSent = FALSE;
  110. if (mb_client)
  111. {
  112. ret = tcpserver_send(mb_client, (void *)pucMBTCPFrame, usTCPLength, 0);
  113. if (ret == usTCPLength)
  114. bFrameSent = TRUE;
  115. }
  116. return bFrameSent;
  117. }
  118. #endif