diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt index 798c851..6f77bb6 100644 --- a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt +++ b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt @@ -446,3 +446,106 @@ 2020-9-15 10:24:5 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 2020-9-15 10:24:11 Send:00 03 00 00 00 06 01 01 00 00 00 01 2020-9-15 10:24:14 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E +2020-9-15 12:15:51 Send:00 00 00 00 00 06 01 01 00 00 00 01 +2020-9-15 12:15:51 Recv:00 00 00 00 00 03 01 81 02 +2020-9-15 12:16:36 Send:00 01 00 00 00 06 01 01 00 00 00 01 +2020-9-15 12:17:5 Send:00 02 00 00 00 06 01 01 00 01 00 0A +2020-9-15 12:17:5 Recv:00 02 00 00 00 05 01 01 02 00 00 +2020-9-15 12:17:26 Send:00 03 00 00 00 06 01 03 00 05 00 06 +2020-9-15 12:17:26 Recv:00 03 00 00 00 0F 01 03 0C CC CC CC CC CC CC CC CC CC CC CC CC +2020-9-15 12:17:49 Send:00 04 00 00 00 09 01 0F 00 01 00 09 02 55 01 +2020-9-15 12:17:49 Recv:00 04 00 00 00 06 01 0F 00 01 00 09 +2020-9-15 12:18:14 Send:00 05 00 00 00 0B 01 10 00 05 00 02 04 55 55 55 55 +2020-9-15 12:18:14 Recv:00 05 00 00 00 06 01 10 00 05 00 02 +2020-9-15 12:18:42 Send:00 06 00 00 00 06 01 01 00 00 00 05 +2020-9-15 12:18:50 Recv:01 06 00 00 00 04 01 01 01 0A +2020-9-15 12:19:4 Send:00 07 00 00 00 06 01 03 00 00 00 01 +2020-9-15 12:19:10 Recv:00 07 01 00 00 05 01 03 02 CC CC +2020-9-15 12:19:23 Send:00 08 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 12:19:24 Recv:00 08 00 00 00 06 01 0F 00 00 00 01 +2020-9-15 12:19:40 Send:00 09 00 00 00 09 01 10 00 00 00 01 02 22 22 +2020-9-15 12:19:52 Recv:00 09 00 00 04 06 01 10 00 00 00 01 +2020-9-15 12:21:17 Send:00 0A 00 00 00 06 01 01 00 00 00 01 +2020-9-15 12:21:25 Recv:00 0A 00 00 00 09 01 01 01 01 +2020-9-15 12:23:9 Send:00 0B 00 00 00 06 01 01 00 00 00 01 +2020-9-15 12:23:15 Recv:00 0B 00 00 00 03 01 01 01 01 +2020-9-15 12:23:43 Send:00 0C 00 00 00 06 01 01 00 00 00 01 +2020-9-15 12:23:48 Recv:00 0C 00 00 00 04 02 01 01 01 +2020-9-15 12:24:7 Send:00 0D 00 00 00 06 01 03 00 00 00 01 +2020-9-15 12:24:15 Recv:00 0D 00 00 00 05 01 83 02 22 22 +2020-9-15 12:24:41 Send:00 0E 00 00 00 06 01 03 00 00 00 02 +2020-9-15 12:24:49 Recv:00 0E 00 00 00 07 01 01 04 22 22 00 00 +2020-9-15 12:53:6 Send:00 00 00 00 00 06 01 03 00 00 00 02 +2020-9-15 12:53:19 Recv:00 00 00 00 00 07 01 01 04 22 22 00 00 +2020-9-15 12:53:28 Send:00 01 00 00 00 06 01 03 00 00 00 01 +2020-9-15 12:53:31 Recv:00 00 00 00 00 07 01 01 04 22 22 00 00 +2020-9-15 12:53:44 Send:00 02 00 00 00 08 01 0F 00 05 00 01 01 03 +2020-9-15 12:54:5 Recv:00 02 00 00 00 08 01 0F 00 06 00 01 +2020-9-15 12:54:14 Send:00 03 00 00 00 08 01 0F 00 06 00 01 01 01 +2020-9-15 12:54:28 Recv:00 03 00 00 00 08 01 0F 00 06 00 02 +2020-9-15 15:48:0 Send:00 00 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:48:7 Recv:00 05 00 00 00 04 01 03 00 01 +2020-9-15 15:48:26 Send:00 01 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:49:9 Recv:00 01 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:49:16 Send:00 02 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:49:36 Recv:00 02 00 01 00 04 01 01 01 +2020-9-15 15:49:53 Send:00 03 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:50:5 Recv:00 02 00 01 00 04 01 01 01 +2020-9-15 15:50:10 Send:00 04 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:50:41 Recv:00 04 00 00 00 03 01 01 01 +2020-9-15 15:51:19 Send:00 05 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:51:33 Recv:00 05 00 00 00 04 01 01 01 01 +2020-9-15 15:51:43 Send:00 06 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:51:45 Recv:00 05 00 01 00 04 01 01 01 01 +2020-9-15 15:52:0 Send:00 07 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:52:2 Recv:00 07 00 01 00 04 01 01 01 01 +2020-9-15 15:52:14 Send:00 08 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:52:17 Recv:00 07 01 00 00 04 01 01 01 01 +2020-9-15 15:52:23 Send:00 09 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:52:56 Recv:00 07 01 00 00 05 01 01 01 01 01 +2020-9-15 15:53:10 Send:00 0A 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:53:17 Recv:00 0A 00 01 00 05 01 01 01 01 01 +2020-9-15 15:53:29 Send:00 0B 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:53:33 Recv:00 0B 00 00 00 05 01 01 01 01 01 +2020-9-15 15:53:46 Send:00 0C 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:53:55 Recv:00 0C 00 00 00 04 01 01 01 01 00 +2020-9-15 15:54:9 Send:00 0D 00 00 00 06 01 01 00 00 00 01 +2020-9-15 15:54:42 Recv:00 0D 00 00 00 04 01 01 01 FF +2020-9-15 15:57:47 Send:00 0E 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:0:43 Send:00 0F 00 00 00 06 01 01 00 00 00 01 +2020-9-15 16:1:9 Recv:00 0F 00 00 00 06 01 01 00 00 00 01 +2020-9-15 16:1:16 Send:00 10 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:3:37 Send:00 11 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:4:18 Recv:00 11 00 00 00 04 01 0F 00 01 00 01 +2020-9-15 16:5:56 Send:00 12 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:5:58 Recv:00 12 00 00 00 04 01 0F 01 00 00 01 +2020-9-15 16:6:11 Send:00 13 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:6:13 Recv:00 12 00 00 00 04 01 0F 00 00 00 01 +2020-9-15 16:6:25 Send:00 14 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:6:27 Recv:00 14 00 00 00 04 01 0F 00 00 00 01 +2020-9-15 16:6:43 Send:00 15 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:6:44 Recv:00 15 00 00 00 05 01 0F 00 00 00 01 +2020-9-15 16:6:56 Send:00 16 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:6:59 Recv:00 16 00 00 00 06 01 0F 00 00 00 01 +2020-9-15 16:7:34 Send:00 17 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:7:40 Recv:00 17 00 00 00 06 01 0F 00 00 00 01 +2020-9-15 16:7:59 Send:00 18 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:8:12 Recv:00 18 00 00 00 07 01 0F 00 00 00 01 00 +2020-9-15 16:8:27 Send:00 19 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 16:17:27 Send:00 1A 00 00 00 09 01 10 00 00 00 01 02 00 00 +2020-9-15 16:18:6 Recv:00 1A 00 00 00 07 01 0F 00 00 00 01 00 +2020-9-15 16:18:21 Send:00 1B 00 00 00 09 01 10 00 00 00 01 02 55 55 +2020-9-15 16:20:10 Send:00 00 00 00 00 09 01 10 00 00 00 01 02 FF 10 +2020-9-15 16:20:11 Recv:00 00 00 00 00 03 01 90 01 +2020-9-15 16:20:32 Send:00 01 00 00 00 09 01 0F 27 06 00 0A 02 55 55 +2020-9-15 16:20:32 Recv:00 01 00 00 00 06 01 0F 27 06 00 0A +2020-9-15 16:20:49 Send:00 02 00 00 00 09 01 0F 27 06 00 0A 02 00 00 +2020-9-15 16:20:49 Recv:00 02 00 00 00 06 01 0F 27 06 00 0A +2020-9-15 16:21:19 Send:00 03 00 00 00 0B 01 0F 26 F2 00 1E 04 00 00 00 00 +2020-9-15 16:21:19 Recv:00 03 00 00 00 06 01 0F 26 F2 00 1E +2020-9-15 18:1:59 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-15 18:2:8 Recv:00 00 00 00 00 05 01 01 02 CC CC +2020-9-15 18:2:37 Send:00 01 00 00 00 08 01 0F 00 00 00 01 01 01 +2020-9-15 18:2:46 Recv:00 01 00 00 00 06 01 0F 08 00 00 01 +2020-9-15 18:3:6 Send:00 02 00 00 00 09 01 10 00 00 00 01 02 00 55 +2020-9-15 18:3:15 Recv:00 02 00 00 00 06 01 10 00 00 00 02 diff --git a/Modbus_communication/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp index d5ce8f4..d664197 100644 --- a/Modbus_communication/Modbus_TCP/TCP_client.cpp +++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp @@ -106,7 +106,7 @@ SOCKET Init_client() } cout << "连接TCP从站成功" << endl; TIMEVAL timeout; - timeout.tv_sec = 20000; //ms + timeout.tv_sec = 60000; //ms timeout.tv_usec = 0; //us setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间 return ClientSocket; @@ -216,7 +216,7 @@ bool Analysis_Response_Message(UINT8 *Response_Message, UINT8 *Request_Message, if (Response_Message[i] != Request_Message[i]) //0 1 传输标识,2 3 协议标识,6设备ID return false; //一旦不一致,判定为异常报文数据 } - if (Response_Message[4] != 0x00) //4 固定0x00 帧长度判定 + if (Response_Message[4] != 0x00) //4 固定0x00 return false; if (Response_Message[7] == Request_Message[7] + 0x80)//先处理异常响应 @@ -229,6 +229,16 @@ bool Analysis_Response_Message(UINT8 *Response_Message, UINT8 *Request_Message, else return false; } + if (Response_Message[7] != Request_Message[7]) //功能码判断 + return false; + if (Request_Message[7] == 0x0F || Request_Message[7] == 0x10) //0F 10 地址和操作数量判定 + { + for (int j = 8; j < 12; j++) + { + if (Request_Message[j] != Response_Message[j]) + return false; + } + } if (!Check_Response_Message_len(Response_Message, Request_Message, Response_Message_len)) return false; if (Response_Message[7] == 0x01) @@ -252,7 +262,7 @@ void Clear_buf(SOCKET ClientSocket) { ioctlsocket(ClientSocket, FIONREAD, &bytesToRecv); - if (bytesToRecv != 0) + if (bytesToRecv != 0)//不等于0时进行清理操作 { if (bytesToRecv > 500) { @@ -268,11 +278,10 @@ void Clear_buf(SOCKET ClientSocket) /********************************************************************************************* * 功能    : 运行客户端 * 描述   : 根据终端输入从站IP地址和端口号连接对应的服务器并且生成发送请求等待响应 -* 输入 : IP 地址 (IPV4) -* Port_number 端口号(1-65535) -* 输出 : false 执行出错 true 执行成功 +* 输入 : 无 +* 输出 : 无 *********************************************************************************************/ -bool Tcp_client() +int Tcp_client() { SOCKET ClientSocket; do @@ -302,7 +311,7 @@ bool Tcp_client() } printf("\n"); Log_Note(Request_Message, 1, Request_Message_len); - Clear_buf(ClientSocket); + Clear_buf(ClientSocket); //清理缓冲区 int test = send(ClientSocket, (char*)Request_Message, Request_Message_len, 0); if (Transmission_Indicator == 0xffff) { @@ -387,5 +396,5 @@ bool Tcp_client() closesocket(ClientSocket); //关闭服务 WSACleanup(); - return true; + return 0; } diff --git a/Modbus_communication/Modbus_TCP/TCP_client.h b/Modbus_communication/Modbus_TCP/TCP_client.h index e38cf8e..4ed4271 100644 --- a/Modbus_communication/Modbus_TCP/TCP_client.h +++ b/Modbus_communication/Modbus_TCP/TCP_client.h @@ -8,7 +8,7 @@ bool InitSocket_Version(void); bool Check_IP(char* IP); void Input_IP(string& IP, unsigned int *Port_number); -bool Tcp_client(); +int Tcp_client(); bool Analysis_Response_Message(UINT8 *Response_Message, UINT8 *Request_Message, int Response_Message_len);