Browse Source

修改bug 10 0f报文缺少数据长度

Modbus_TCP
zcn1123 4 years ago
parent
commit
a2ea0aa4da
4 changed files with 43 additions and 7 deletions
  1. +36
    -2
      Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt
  2. +2
    -2
      Modbus_communication/Modbus_TCP/TCP_client.cpp
  3. +4
    -2
      Modbus_communication/Modbus_TCP/common.cpp
  4. +1
    -1
      Modbus_communication/Modbus_TCP/common.h

+ 36
- 2
Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt View File

@@ -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

+ 2
- 2
Modbus_communication/Modbus_TCP/TCP_client.cpp View File

@@ -120,13 +120,13 @@ void Clear_recv_buf(SOCKET clientSocket)
tmOut.tv_sec = 0; tmOut.tv_sec = 0;
tmOut.tv_usec = 0; tmOut.tv_usec = 0;
fd_set fds; fd_set fds;
FD_ZERO(&fds);
FD_SET(clientSocket, &fds);
int nRet; int nRet;
char tmp[2]; char tmp[2];
memset(tmp, 0, sizeof(tmp)); memset(tmp, 0, sizeof(tmp));
while (1) while (1)
{ {
FD_ZERO(&fds);
FD_SET(clientSocket, &fds);
nRet = select(FD_SETSIZE, &fds, NULL, NULL, &tmOut); nRet = select(FD_SETSIZE, &fds, NULL, NULL, &tmOut);
if (nRet == 0) if (nRet == 0)
break; break;


+ 4
- 2
Modbus_communication/Modbus_TCP/common.cpp View File

@@ -223,10 +223,12 @@ void Crate_MBAP(UINT8 *Message, int Function_code, unsigned int Operations_Numbe
* Message_len消息帧数组的起始位置 * Message_len消息帧数组的起始位置
* 输出 : 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) if (Write_date.length() == 0)
return Message_len; return Message_len;
Message[Message_len] = Count_Write_date_number(Function_code, Operations_Number);
Message_len++;
const char *b = Write_date.c_str(); const char *b = Write_date.c_str();
for (unsigned int i = 0; i + 3 < Write_date.length(); i = i + 3) 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[10] = Operations_Number >> 8;//操作数量H
Message[11] = Operations_Number;//操作数量L Message[11] = Operations_Number;//操作数量L


return HexStringtoByte(Message, Write_date, 12);
return HexStringtoByte(Message, Write_date, 12, Function_code, Operations_Number);
} }




+ 1
- 1
Modbus_communication/Modbus_TCP/common.h View File

@@ -11,7 +11,7 @@
#pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"ws2_32.lib")
using namespace std; using namespace std;


#define DEVICE_ID 0x09 //�구ID
#define DEVICE_ID 0x01 //�구ID








Loading…
Cancel
Save