Explorar el Código

修改modbus tcp 多连接引起的错误

lg28870983 hace 3 años
padre
commit
460a7497a4
Se han modificado 1 ficheros con 15 adiciones y 7 borrados
  1. 15 7
      port/porttcp.c

+ 15 - 7
port/porttcp.c

@@ -42,19 +42,27 @@ static void tcpserver_event_notify(tcpclient_t client, rt_uint8_t event)
     {
     case TCPSERVER_EVENT_CONNECT:
         if (mb_client == RT_NULL)
+        {
             mb_client = client;
+        }
         else
+        {
+            tcpserver_close(client);
             rt_kprintf("Multi-host is not supported, please disconnect the current host first!");
+        }
         break;
     case TCPSERVER_EVENT_RECV:
-        prvvTCPLength = tcpserver_recv(mb_client, &prvvTCPBuf, MB_TCP_BUF_SIZE, 100);
-        if (prvvTCPLength)
-        {
-            xMBPortEventPost(EV_FRAME_RECEIVED);
+        if(mb_client==client){
+            prvvTCPLength = tcpserver_recv(mb_client, &prvvTCPBuf, MB_TCP_BUF_SIZE, 100);
+            if (prvvTCPLength)
+            {
+                xMBPortEventPost(EV_FRAME_RECEIVED);
+            }
         }
         break;
     case TCPSERVER_EVENT_DISCONNECT:
-        mb_client = RT_NULL;
+        if(mb_client==client)
+            mb_client = RT_NULL;
         break;
     default:
         break;
@@ -65,10 +73,10 @@ BOOL
 xMBTCPPortInit(USHORT usTCPPort)
 {
     struct tcpserver *serv;
-    
+
     if (usTCPPort == 0)
         usTCPPort = MB_TCP_DEFAULT_PORT;
-    
+
     serv = tcpserver_create(0, usTCPPort);
 
     tcpserver_set_notify_callback(serv, tcpserver_event_notify);