From a2ea0aa4dade029343671fd49d328059e3b036c0 Mon Sep 17 00:00:00 2001 From: zcn1123 <2363211205@qq.com> Date: Sat, 5 Sep 2020 20:09:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug=2010=200f=E6=8A=A5?= =?UTF-8?q?=E6=96=87=E7=BC=BA=E5=B0=91=E6=95=B0=E6=8D=AE=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Modbus_TCP/Modbus_TCP_log.txt | 38 ++++++++++++++++++- .../Modbus_TCP/TCP_client.cpp | 4 +- Modbus_communication/Modbus_TCP/common.cpp | 6 ++- Modbus_communication/Modbus_TCP/common.h | 2 +- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt index 2cd8041..9ee522e 100644 --- a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt +++ b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt @@ -1,2 +1,36 @@ -2020-9-1 14:23:15 Send:00 00 00 00 00 06 09 01 00 00 00 09 -2020-9-1 14:23:17 Recv:00 00 00 00 00 05 10 01 02 FF 01 +2020-9-5 19:23:6 Send:00 00 00 00 00 08 01 10 00 00 00 01 02 FF FF +2020-9-5 19:23:30 Recv:00 00 00 00 00 06 01 10 00 00 00 01 +2020-9-5 19:24:29 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-5 19:24:50 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-5 19:26:22 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-5 19:26:31 Recv:00 00 00 00 00 05 01 03 02 FF FF +2020-9-5 19:27:11 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-5 19:27:17 Recv:00 00 00 00 00 05 01 03 02 FF FF +2020-9-5 19:27:38 Send:00 00 00 00 00 08 01 10 00 01 00 01 02 11 11 +2020-9-5 19:27:44 Recv:00 00 00 00 00 06 01 10 00 01 00 01 +2020-9-5 19:28:20 Send:00 00 00 00 00 06 01 03 00 01 00 01 +2020-9-5 19:28:55 Send:00 00 00 00 00 06 01 03 00 01 00 01 +2020-9-5 19:30:38 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-5 19:30:38 Recv:00 00 00 00 00 05 01 03 02 FF FF +2020-9-5 19:30:42 Send:00 00 00 00 00 06 01 03 00 01 00 01 +2020-9-5 19:30:42 Recv:00 00 00 00 00 05 01 03 02 11 FF +2020-9-5 19:31:1 Send:00 00 00 00 00 08 01 10 00 03 00 01 02 11 11 +2020-9-5 19:31:1 Recv:00 00 00 00 00 06 01 10 00 03 00 01 +2020-9-5 19:31:8 Send:00 00 00 00 00 06 01 03 00 03 00 01 +2020-9-5 19:31:49 Send:00 00 00 00 00 06 01 03 00 03 00 01 +2020-9-5 19:32:50 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-5 19:33:17 Send:00 00 00 00 00 08 01 10 00 02 00 01 02 11 11 +2020-9-5 19:38:12 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-5 19:38:12 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E +2020-9-5 19:38:24 Send:00 00 00 00 00 06 01 01 00 00 00 01 +2020-9-5 19:38:24 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E +2020-9-5 19:38:37 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-5 19:38:56 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-5 19:38:56 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E +2020-9-5 19:39:5 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-5 19:39:5 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E +2020-9-5 19:40:28 Send:00 00 00 00 00 06 01 03 00 00 00 0A +2020-9-5 19:40:28 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E +2020-9-5 19:40:41 Send:00 00 00 00 00 06 01 03 00 00 00 0A +2020-9-5 19:40:52 Send:00 00 00 00 00 06 01 03 00 00 00 0A +2020-9-5 19:40:52 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 a5ed0da..304aa7f 100644 --- a/Modbus_communication/Modbus_TCP/TCP_client.cpp +++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp @@ -120,13 +120,13 @@ void Clear_recv_buf(SOCKET clientSocket) tmOut.tv_sec = 0; tmOut.tv_usec = 0; fd_set fds; - FD_ZERO(&fds); - FD_SET(clientSocket, &fds); int nRet; char tmp[2]; memset(tmp, 0, sizeof(tmp)); while (1) { + FD_ZERO(&fds); + FD_SET(clientSocket, &fds); nRet = select(FD_SETSIZE, &fds, NULL, NULL, &tmOut); if (nRet == 0) break; diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp index d6b7668..30da7d4 100644 --- a/Modbus_communication/Modbus_TCP/common.cpp +++ b/Modbus_communication/Modbus_TCP/common.cpp @@ -223,10 +223,12 @@ void Crate_MBAP(UINT8 *Message, int Function_code, unsigned int Operations_Numbe * Message_len消息帧数组的起始位置 * 输出 : Message_len 消息帧的长度 *********************************************************************************************/ -int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len) +int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len, int Function_code, unsigned int Operations_Number) { if (Write_date.length() == 0) return Message_len; + Message[Message_len] = Count_Write_date_number(Function_code, Operations_Number); + Message_len++; const char *b = Write_date.c_str(); for (unsigned int i = 0; i + 3 < Write_date.length(); i = i + 3) { @@ -254,7 +256,7 @@ int Crate_TCP_Message(UINT8 *Message, int Function_code, unsigned int Operations Message[10] = Operations_Number >> 8;//操作数量H Message[11] = Operations_Number;//操作数量L - return HexStringtoByte(Message, Write_date, 12); + return HexStringtoByte(Message, Write_date, 12, Function_code, Operations_Number); } diff --git a/Modbus_communication/Modbus_TCP/common.h b/Modbus_communication/Modbus_TCP/common.h index b3c237b..790816b 100644 --- a/Modbus_communication/Modbus_TCP/common.h +++ b/Modbus_communication/Modbus_TCP/common.h @@ -11,7 +11,7 @@ #pragma comment(lib,"ws2_32.lib") using namespace std; -#define DEVICE_ID 0x09 //豸ID +#define DEVICE_ID 0x01 //豸ID