diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt index 377ea9b..798c851 100644 --- a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt +++ b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt @@ -409,3 +409,40 @@ 2020-9-14 20:59:53 Send:00 03 00 00 00 06 01 01 00 00 00 01 2020-9-14 21:1:44 Send:00 00 00 00 00 06 01 01 00 00 00 01 2020-9-14 21:1:49 Recv:01 11 11 01 01 11 01 01 01 01 +2020-9-14 21:15:54 Send:00 00 00 00 00 06 01 01 00 00 00 0A +2020-9-14 21:16:17 Send:00 01 00 00 00 06 01 01 00 00 00 01 +2020-9-15 9:28:0 Send:00 00 00 00 00 06 01 01 00 00 00 01 +2020-9-15 9:29:12 Send:00 01 00 00 00 06 01 01 00 00 00 09 +2020-9-15 9:29:32 Recv:00 01 00 00 00 05 01 01 02 00 00 +2020-9-15 9:29:41 Send:00 02 00 00 00 06 01 01 00 00 00 01 +2020-9-15 9:30:1 Recv:00 02 00 00 00 04 01 01 01 00 +2020-9-15 9:30:43 Send:00 03 00 00 00 06 01 01 00 00 00 01 +2020-9-15 9:31:3 Recv:00 03 00 00 00 04 01 01 01 00 +2020-9-15 9:33:28 Send:00 00 00 00 00 06 01 01 00 00 00 01 +2020-9-15 9:35:49 Send:00 00 00 00 00 06 01 01 00 00 00 0B +2020-9-15 9:36:25 Send:00 00 00 00 00 06 01 01 00 00 00 01 +2020-9-15 9:36:49 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E +2020-9-15 9:36:54 Send:00 01 00 00 00 06 01 01 00 00 00 01 +2020-9-15 10:19:14 Send:00 00 00 00 00 06 01 01 00 00 00 01 +2020-9-15 10:20:32 Send:00 01 00 00 00 06 01 01 00 00 00 01 +2020-9-15 10:20:35 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:20:47 Send:00 02 00 00 00 06 01 01 00 00 00 01 +2020-9-15 10:20:52 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:21:49 Send:00 00 00 00 00 06 01 01 00 00 00 01 +2020-9-15 10:21:51 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:21:55 Send:00 01 00 00 00 06 01 01 00 00 00 01 +2020-9-15 10:21:57 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:22:5 Send:00 02 00 00 00 06 01 03 00 00 00 01 +2020-9-15 10:22:7 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:22:37 Send:00 03 00 00 00 06 01 01 00 00 00 01 +2020-9-15 10:22:39 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:22:49 Send:00 04 00 00 00 06 01 03 00 00 00 01 +2020-9-15 10:22:51 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:23:40 Send:00 00 00 00 00 06 01 01 00 00 00 01 +2020-9-15 10:23:44 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:23:50 Send:00 01 00 00 00 06 01 01 00 00 00 01 +2020-9-15 10:23:54 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:23:58 Send:00 02 00 00 00 06 01 01 00 00 00 01 +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 diff --git a/Modbus_communication/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp index 7a49c0c..d5ce8f4 100644 --- a/Modbus_communication/Modbus_TCP/TCP_client.cpp +++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp @@ -149,7 +149,7 @@ void Printf_Register_date(UINT8 *Response_Message, UINT8 *Request_Message) printf("寄存器起始地址为%d \n", temp1); for (int i = 0; i < Response_Message[8]; i = i + 2) { - printf("寄存器第%d的值为:%02X %02X \n", temp1++, Response_Message[9 + i], Response_Message[10+i]); + printf("寄存器%d的值为:%02X %02X \n", temp1++, Response_Message[9 + i], Response_Message[10+i]); } } @@ -247,16 +247,20 @@ bool Analysis_Response_Message(UINT8 *Response_Message, UINT8 *Request_Message, void Clear_buf(SOCKET ClientSocket) { unsigned long bytesToRecv; - char temp[50]; + char temp[500]; do { + ioctlsocket(ClientSocket, FIONREAD, &bytesToRecv); - if (bytesToRecv > 50) + if (bytesToRecv != 0) { - recv(ClientSocket, temp, 50, 0); + if (bytesToRecv > 500) + { + recv(ClientSocket, temp, 500, 0); + } + else + recv(ClientSocket, temp, bytesToRecv, 0); } - else - recv(ClientSocket, temp, bytesToRecv, 0); } while (bytesToRecv != 0); } diff --git a/Modbus_communication/TCP_Master_test/1.ini b/Modbus_communication/TCP_Master_test/1.ini index 9ebe506..d259f96 100644 --- a/Modbus_communication/TCP_Master_test/1.ini +++ b/Modbus_communication/TCP_Master_test/1.ini @@ -41,13 +41,13 @@ Output = "00 00 00 00 00 06 09 03 00 00 00 7D" Input1 = 15 Input2 = 1 Input3 = "01" -Output = "00 00 00 00 00 08 09 03 00 00 00 01 01 01" +Output = "00 00 00 00 00 08 09 0F 00 00 00 01 01 01" [Example8] Input1 = 15 Input2 = 10 Input3 = "FF 03" -Output = "00 00 00 00 00 09 09 03 00 00 00 0A 02 FF 03" +Output = "00 00 00 00 00 09 09 0F 00 00 00 0A 02 FF 03" [Example9] Input1 = 15 @@ -70,6 +70,6 @@ Output = "00 00 00 00 00 11 09 10 00 00 00 05 0A FF FF FF FF FF FF FF FF FF FF" [Example12] Input1 = 16 Input2 = 123 -Input3 = "F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" +Input3 = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" Output = "00 00 00 00 00 FD 09 10 00 00 00 7B F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" diff --git a/Modbus_communication/TCP_Master_test/2.ini b/Modbus_communication/TCP_Master_test/2.ini index a44bbb6..1e31e70 100644 --- a/Modbus_communication/TCP_Master_test/2.ini +++ b/Modbus_communication/TCP_Master_test/2.ini @@ -1,5 +1,5 @@ [Example] -Number = 28 +Number = 27 [Example1] Input1 = "00 00 00 00 00 04 09 01 01 01" @@ -115,6 +115,7 @@ Input2 = "00 00 00 00 00 09 09 01 00 00 00 01" Input1 = "00 00 00 00 00 03 09 83 01" Input2 = "00 00 00 00 00 09 09 03 00 00 00 01" + [Example26] Input1 = "00 00 00 00 00 03 09 83 02" Input2 = "00 00 00 00 00 09 09 03 00 00 00 01"