modbus_tcp_test.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "modbus_tcp_test.h"
  2. #include <modbus.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <dfs_posix.h>
  6. #include <sys/time.h>
  7. #include <dfs_select.h>
  8. #include <sal_socket.h>
  9. // static void test_thread(void *param)
  10. // {
  11. // uint16_t tab_reg[64] = {0};
  12. // modbus_t *ctx = RT_NULL;
  13. // ctx = modbus_new_tcp("192.168.1.103", 601, AF_INET);
  14. // modbus_set_slave(ctx, 3);
  15. // modbus_set_response_timeout(ctx, 0, 1000000);
  16. // _mbtcp_start:
  17. // if(modbus_connect(ctx) < 0)
  18. // goto _mbtcp_restart;
  19. // int num = 0;
  20. // while (1)
  21. // {
  22. // memset(tab_reg, 0, 64 * 2);
  23. // int regs = modbus_read_registers(ctx, 0, 20, tab_reg);
  24. // if(regs < 0)
  25. // goto _mbtcp_restart;
  26. // printf("-------------------------------------------\n");
  27. // printf("[%4d][read num = %d]", num, regs);
  28. // num++;
  29. // int i;
  30. // for (i = 0; i < 20; i++)
  31. // {
  32. // printf("<%#x>", tab_reg[i]);
  33. // }
  34. // printf("\n");
  35. // printf("-------------------------------------------\n");
  36. // rt_thread_mdelay(1000);
  37. // }
  38. // _mbtcp_restart:
  39. // //7-关闭modbus端口
  40. // modbus_close(ctx);
  41. // rt_thread_mdelay(2000);
  42. // goto _mbtcp_start;
  43. // //8-释放modbus资源
  44. // modbus_free(ctx);
  45. // }
  46. #define MAX_CLIENT_NUM 3
  47. #define CLIENT_TIMEOUT 10 //单位 s
  48. typedef struct
  49. {
  50. int fd;
  51. rt_tick_t tick_timeout;
  52. }client_session_t;
  53. static void test_thread(void *param)
  54. {
  55. int server_fd = -1;
  56. modbus_t *ctx = NULL;
  57. modbus_mapping_t *mb_mapping = NULL;
  58. client_session_t client_session[MAX_CLIENT_NUM];
  59. for (int i = 0; i < MAX_CLIENT_NUM; i++)
  60. {
  61. client_session[i].fd = -1;
  62. client_session[i].tick_timeout = rt_tick_get() + rt_tick_from_millisecond(CLIENT_TIMEOUT * 1000);
  63. }
  64. int max_fd = -1;
  65. fd_set readset;
  66. int rc;
  67. struct timeval select_timeout;
  68. select_timeout.tv_sec = 1;
  69. select_timeout.tv_usec = 0;
  70. ctx = modbus_new_tcp(RT_NULL, 1502, AF_INET);
  71. RT_ASSERT(ctx != RT_NULL);
  72. mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
  73. MODBUS_MAX_READ_REGISTERS, 0);
  74. RT_ASSERT(mb_mapping != RT_NULL);
  75. _mbtcp_start:
  76. server_fd = modbus_tcp_listen(ctx, 1);
  77. if (server_fd < 0)
  78. goto _mbtcp_restart;
  79. while (1)
  80. {
  81. max_fd = -1;
  82. FD_ZERO(&readset);
  83. FD_SET(server_fd, &readset);
  84. if(max_fd < server_fd)
  85. max_fd = server_fd;
  86. for (int i = 0; i < MAX_CLIENT_NUM; i++)
  87. {
  88. if(client_session[i].fd >= 0)
  89. {
  90. FD_SET(client_session[i].fd, &readset);
  91. if(max_fd < client_session[i].fd)
  92. max_fd = client_session[i].fd;
  93. }
  94. }
  95. rc = select(max_fd + 1, &readset, RT_NULL, RT_NULL, &select_timeout);
  96. if(rc < 0)
  97. {
  98. goto _mbtcp_restart;
  99. }
  100. else if(rc > 0)
  101. {
  102. if(FD_ISSET(server_fd, &readset))
  103. {
  104. int client_sock_fd = modbus_tcp_accept(ctx, &server_fd);
  105. if(client_sock_fd >= 0)
  106. {
  107. int index = -1;
  108. for (int i = 0; i < MAX_CLIENT_NUM; i++)
  109. {
  110. if(client_session[i].fd < 0)
  111. {
  112. index = i;
  113. break;
  114. }
  115. }
  116. if(index >= 0)
  117. {
  118. client_session[index].fd = client_sock_fd;
  119. client_session[index].tick_timeout = rt_tick_get() + rt_tick_from_millisecond(CLIENT_TIMEOUT * 1000);
  120. }
  121. else
  122. {
  123. close(client_sock_fd);
  124. }
  125. }
  126. }
  127. for (int i = 0; i < MAX_CLIENT_NUM; i++)
  128. {
  129. if(client_session[i].fd >= 0)
  130. {
  131. if(FD_ISSET(client_session[i].fd, &readset))
  132. {
  133. uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
  134. modbus_set_socket(ctx, client_session[i].fd);
  135. rc = modbus_receive(ctx, query);
  136. if (rc > 0)
  137. {
  138. rc = modbus_reply(ctx, query, rc, mb_mapping);
  139. if(rc < 0)
  140. {
  141. close(client_session[i].fd);
  142. client_session[i].fd = -1;
  143. }
  144. else
  145. {
  146. client_session[i].tick_timeout = rt_tick_get() + rt_tick_from_millisecond(CLIENT_TIMEOUT * 1000);
  147. }
  148. }
  149. else
  150. {
  151. close(client_session[i].fd);
  152. client_session[i].fd = -1;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. // 客户端超时未收到数据断开
  159. for(int i =0;i<MAX_CLIENT_NUM;i++)
  160. {
  161. if(client_session[i].fd >= 0)
  162. {
  163. //超时
  164. if((rt_tick_get() - client_session[i].tick_timeout) < (RT_TICK_MAX / 2))
  165. {
  166. close(client_session[i].fd);
  167. client_session[i].fd = -1;
  168. }
  169. }
  170. }
  171. }
  172. _mbtcp_restart:
  173. if(server_fd >= 0)
  174. {
  175. close(server_fd);
  176. server_fd = -1;
  177. }
  178. for(int i =0;i<MAX_CLIENT_NUM;i++)
  179. {
  180. if(client_session[i].fd >= 0)
  181. {
  182. close(client_session[i].fd);
  183. client_session[i].fd = -1;
  184. }
  185. }
  186. rt_thread_mdelay(5000);
  187. goto _mbtcp_start;
  188. modbus_free(ctx);
  189. }
  190. static int tcp_test_init(void)
  191. {
  192. rt_thread_t tid;
  193. tid = rt_thread_create("mbtcp_test",
  194. test_thread, RT_NULL,
  195. 2048,
  196. 12, 10);
  197. if (tid != RT_NULL)
  198. rt_thread_startup(tid);
  199. return RT_EOK;
  200. }
  201. INIT_APP_EXPORT(tcp_test_init);