diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt index 038f01e..35b98d4 100644 --- a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt +++ b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt @@ -52,3 +52,15 @@ 2020-9-7 18:47:2 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 2020-9-7 18:47:16 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 2020-9-7 18:47:30 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 +2020-9-8 14:42:47 Send:00 00 00 00 00 06 01 01 00 00 07 B0 +2020-9-8 14:45:56 Send:00 00 00 00 00 06 01 01 00 00 07 B0 +2020-9-8 14:46:36 Send:00 00 00 00 00 06 09 01 00 00 07 B0 +2020-9-8 14:51:59 Send:00 00 00 00 00 06 01 01 00 00 07 B0 +2020-9-8 14:51:59 Recv:00 00 00 00 00 F9 01 01 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 +2020-9-8 14:53:17 Send:00 00 00 00 00 06 01 01 00 00 07 B0 +2020-9-8 14:53:17 Recv:00 00 00 00 00 F9 01 01 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 +2020-9-8 14:55:11 Send:00 00 00 00 00 06 01 01 00 00 07 D0 +2020-9-8 14:55:43 Send:00 00 00 00 00 06 09 01 00 00 07 D0 +2020-9-8 14:55:47 Recv:00 00 00 00 00 FD 09 01 FB 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 +2020-9-8 14:56:20 Send:00 00 00 00 00 06 09 01 00 00 07 B0 +2020-9-8 14:56:23 Recv:00 00 00 00 00 F9 09 01 F7 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 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/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp index 76b1e4a..29861f1 100644 --- a/Modbus_communication/Modbus_TCP/TCP_client.cpp +++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp @@ -102,7 +102,7 @@ SOCKET Init_client(string IP, unsigned int Port_number) } cout << "连接TCP从站成功" << endl; TIMEVAL timeout; - timeout.tv_sec = 200; //ms + timeout.tv_sec = 20000; //ms timeout.tv_usec = 0; //us setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间 return ClientSocket; @@ -187,16 +187,16 @@ bool Analysis_Response_Message(UINT8 *Response_Message, UINT8 *Request_Message, Printf_Anomaly_date(Response_Message); return true; } - else if (Response_Message[6] == Request_Message[6] && Response_Message_len - 5 == Response_Message[5]) //判断是否是正常响应帧 + else if (Response_Message[6] == Request_Message[6]) //判断是否是正常响应帧 { - if (Response_Message[7] == 0x01) + if (Response_Message[7] == 0x01 && Response_Message_len - 6 == Response_Message[5]) Printf_Coil_date(Response_Message, Request_Message); - if (Response_Message[7] == 0x03) + if (Response_Message[7] == 0x03 && Response_Message_len - 6 == Response_Message[5]) Printf_Register_date(Response_Message, Request_Message); - if (Response_Message[7] == 0x10) - printf("成功写入从站线圈%d个", ((Request_Message[10] << 8) | Request_Message[11])); if (Response_Message[7] == 0x0F) - printf("成功写入从站寄存器%d个" ,((Request_Message[10] << 8) | Request_Message[11])); + printf("成功写入从站线圈%d个\n", ((Request_Message[10] << 8) | Request_Message[11])); + if (Response_Message[7] == 0x10) + printf("成功写入从站寄存器%d个\n" ,((Request_Message[10] << 8) | Request_Message[11])); return true; } return false; @@ -244,7 +244,7 @@ bool Tcp_client(string IP, unsigned int Port_number) printf("从站响应 :"); for (int i = 0; i < Response_Message_len; i++) { - printf("%02x ", Response_Message[i]); + printf("0x%02x, ", Response_Message[i]); } printf("\n"); if (Analysis_Response_Message(Response_Message, Request_Message, Response_Message_len)) diff --git a/Modbus_communication/Modbus_TCP/TCP_client.h b/Modbus_communication/Modbus_TCP/TCP_client.h index 9f5b5e8..dc7b35e 100644 --- a/Modbus_communication/Modbus_TCP/TCP_client.h +++ b/Modbus_communication/Modbus_TCP/TCP_client.h @@ -9,6 +9,7 @@ bool InitSocket_Version(void); bool Check_IP(char* IP); void Input_IP(string& IP, unsigned int *Port_number); bool Tcp_client(string IP, unsigned int Port_number); +bool Analysis_Response_Message(UINT8 *Response_Message, UINT8 *Request_Message, int Response_Message_len); #endif \ No newline at end of file diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp index 30da7d4..aae57fd 100644 --- a/Modbus_communication/Modbus_TCP/common.cpp +++ b/Modbus_communication/Modbus_TCP/common.cpp @@ -146,6 +146,8 @@ bool Check_Write_date(string Write_date, int Write_date_number) unsigned int Count_Write_date_number(int Function_code, unsigned int Operations_Number) { unsigned int Write_date_number = 0; + if (Function_code == 0x01 || Function_code == 0x03) + return Write_date_number; if (Function_code == 0x0F) //读线圈 { Write_date_number = Operations_Number / 8; @@ -154,7 +156,7 @@ unsigned int Count_Write_date_number(int Function_code, unsigned int Operations_ } if (Function_code == 0x10) Write_date_number = Operations_Number * 2; - return Write_date_number; + return Write_date_number +1; } /********************************************************************************************* @@ -227,7 +229,7 @@ int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len, int Func { if (Write_date.length() == 0) return Message_len; - Message[Message_len] = Count_Write_date_number(Function_code, Operations_Number); + Message[Message_len] = Count_Write_date_number(Function_code, Operations_Number)-1; Message_len++; const char *b = Write_date.c_str(); for (unsigned int i = 0; i + 3 < Write_date.length(); i = i + 3) diff --git a/Modbus_communication/Modbus_TCP/common.h b/Modbus_communication/Modbus_TCP/common.h index 790816b..2ab0dd5 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 0x01 //豸ID +#define DEVICE_ID 0x09 //豸ID @@ -22,7 +22,7 @@ bool Check_Write_date(string Write_date, int Write_date_number); unsigned int Count_Write_date_number(int Function_code, unsigned int Operations_Number); string Input_Write_date(int Function_code, unsigned int Operations_Number); void Crate_MBAP(UINT8 *Message, int Function_code, unsigned int Operations_Number); -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); int Crate_TCP_Message(UINT8 *Message, int Function_code, unsigned int Operations_Number, unsigned int Starting_address, string Write_date); void Log_Note(UINT8 *Message, int flage, int Message_len); diff --git a/Modbus_communication/Modbus_communication.sln b/Modbus_communication/Modbus_communication.sln index 38aa418..6775702 100644 --- a/Modbus_communication/Modbus_communication.sln +++ b/Modbus_communication/Modbus_communication.sln @@ -7,6 +7,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Modbus_RTU_Salve", "Modbus_ EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Modbus_TCP", "Modbus_TCP\Modbus_TCP.vcxproj", "{CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TCP_Master_test", "TCP_Master_test\TCP_Master_test.vcxproj", "{677F6F19-D890-4DE8-9EFD-08FE0E154AD0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -21,6 +23,10 @@ Global {CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}.Debug|Win32.Build.0 = Debug|Win32 {CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}.Release|Win32.ActiveCfg = Release|Win32 {CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}.Release|Win32.Build.0 = Release|Win32 + {677F6F19-D890-4DE8-9EFD-08FE0E154AD0}.Debug|Win32.ActiveCfg = Debug|Win32 + {677F6F19-D890-4DE8-9EFD-08FE0E154AD0}.Debug|Win32.Build.0 = Debug|Win32 + {677F6F19-D890-4DE8-9EFD-08FE0E154AD0}.Release|Win32.ActiveCfg = Release|Win32 + {677F6F19-D890-4DE8-9EFD-08FE0E154AD0}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Modbus_communication/TCP_Master_test/1.ini b/Modbus_communication/TCP_Master_test/1.ini new file mode 100644 index 0000000..9ebe506 --- /dev/null +++ b/Modbus_communication/TCP_Master_test/1.ini @@ -0,0 +1,75 @@ +[Example] +Number = 12 + +[Example1] +Input1 = 1 +Input2 = 1 +Input3 = "" +Output = "00 00 00 00 00 06 09 01 00 00 00 01" + +[Example2] +Input1 = 1 +Input2 = 100 +Input3 = "" +Output = "00 00 00 00 00 06 09 01 00 00 00 64" + +[Example3] +Input1 = 1 +Input2 = 2000 +Input3 = "" +Output = "00 00 00 00 00 06 09 01 00 00 07 D0" + +[Example4] +Input1 = 3 +Input2 = 1 +Input3 = "" +Output = "00 00 00 00 00 06 09 03 00 00 00 01" + +[Example5] +Input1 = 3 +Input2 = 10 +Input3 = "" +Output = "00 00 00 00 00 06 09 03 00 00 00 0A" + +[Example6] +Input1 = 3 +Input2 = 125 +Input3 = "" +Output = "00 00 00 00 00 06 09 03 00 00 00 7D" + +[Example7] +Input1 = 15 +Input2 = 1 +Input3 = "01" +Output = "00 00 00 00 00 08 09 03 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" + +[Example9] +Input1 = 15 +Input2 = 1968 +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" +Output = "00 00 00 00 00 FD 09 0F 00 00 07 BD 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" + +[Example10] +Input1 = 16 +Input2 = 1 +Input3 = "FF FF" +Output = "00 00 00 00 00 09 09 10 00 00 00 01 02 FF FF" + +[Example11] +Input1 = 16 +Input2 = 5 +Input3 = "FF FF FF FF FF FF FF FF FF FF" +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" +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 new file mode 100644 index 0000000..a44bbb6 --- /dev/null +++ b/Modbus_communication/TCP_Master_test/2.ini @@ -0,0 +1,128 @@ +[Example] +Number = 28 + +[Example1] +Input1 = "00 00 00 00 00 04 09 01 01 01" +Input2 = "00 00 00 00 00 06 09 01 00 00 00 01" + +[Example2] +Input1 = "00 00 00 00 00 10 09 01 0D 00 00 00 00 00 00 00 00 00 00 00 00 00" +Input2 = "00 00 00 00 00 06 09 01 00 00 00 64" + +[Example3] +Input1 = "00 00 00 00 00 FD 09 01 FA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" +Input2 = "00 00 00 00 00 06 09 01 00 00 07 D0" + + +[Example4] +Input1 = "00 00 00 00 00 05 09 03 02 FF FF" +Input2 = "00 00 00 00 00 06 09 03 00 00 00 01" + +[Example5] +Input1 = "00 00 00 00 00 17 09 03 14 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF" +Input2 = "00 00 00 00 00 06 09 03 00 00 00 0A" + +[Example6] +Input1 = "00 00 00 00 00 FD 09 03 FA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" +Input2 = "00 00 00 00 00 06 09 03 00 00 00 7D" + + + +[Example7] +Input1 = "00 00 00 00 00 06 09 0F 00 00 00 01" +Input2 = "00 00 00 00 00 08 09 0F 00 00 00 01 01 01" + +[Example8] +Input1 = "00 00 00 00 00 06 09 0F 00 00 00 0A" +Input2 = "00 00 00 00 00 09 09 0F 00 00 00 0A 02 FF 03" + +[Example9] +Input1 = "00 00 00 00 00 06 09 0F 00 00 07 B0" +Input2 = "00 00 00 00 00 FD 09 0F 00 00 07 B0 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" + + + + +[Example10] +Input1 = "00 00 00 00 00 06 09 10 00 00 00 01" +Input2 = "00 00 00 00 00 09 09 10 00 00 00 01 02 FF FF" + +[Example11] +Input1 = "00 00 00 00 00 06 09 10 00 00 00 05" +Input2 = "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 = "00 00 00 00 00 06 09 10 00 00 00 7B" +Input2 = "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" + + + +[Example13] +Input1 = "00 00 00 00 00 03 09 90 01" +Input2 = "00 00 00 00 00 09 09 10 00 00 00 01" + +[Example14] +Input1 = "00 00 00 00 00 03 09 90 02" +Input2 = "00 00 00 00 00 09 09 10 00 00 00 01" + +[Example15] +Input1 = "00 00 00 00 00 03 09 90 03" +Input2 = "00 00 00 00 00 09 09 10 00 00 00 01" + +[Example16] +Input1 = "00 00 00 00 00 03 09 90 04" +Input2 = "00 00 00 00 00 09 09 10 00 00 00 01" + + + +[Example17] +Input1 = "00 00 00 00 00 03 09 8F 01" +Input2 = "00 00 00 00 00 09 09 0F 00 00 00 01" + +[Example18] +Input1 = "00 00 00 00 00 03 09 8F 02" +Input2 = "00 00 00 00 00 09 09 0F 00 00 00 01" + +[Example19] +Input1 = "00 00 00 00 00 03 09 8F 03" +Input2 = "00 00 00 00 00 09 09 0F 00 00 00 01" + +[Example20] +Input1 = "00 00 00 00 00 03 09 8F 04" +Input2 = "00 00 00 00 00 09 09 0F 00 00 00 01" + + + +[Example21] +Input1 = "00 00 00 00 00 03 09 81 01" +Input2 = "00 00 00 00 00 09 09 01 00 00 00 01" + +[Example22] +Input1 = "00 00 00 00 00 03 09 81 02" +Input2 = "00 00 00 00 00 09 09 01 00 00 00 01" + +[Example23] +Input1 = "00 00 00 00 00 03 09 81 03" +Input2 = "00 00 00 00 00 09 09 01 00 00 00 01" + +[Example24] +Input1 = "00 00 00 00 00 03 09 81 04" +Input2 = "00 00 00 00 00 09 09 01 00 00 00 01" + + + +[Example25] +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" + +[Example27] +Input1 = "00 00 00 00 00 03 09 83 03" +Input2 = "00 00 00 00 00 09 09 03 00 00 00 01" + +[Example28] +Input1 = "00 00 00 00 00 03 09 83 04" +Input2 = "00 00 00 00 00 09 09 03 00 00 00 01" \ No newline at end of file diff --git a/Modbus_communication/TCP_Master_test/TCP_Master_test.vcxproj b/Modbus_communication/TCP_Master_test/TCP_Master_test.vcxproj new file mode 100644 index 0000000..ded46dc --- /dev/null +++ b/Modbus_communication/TCP_Master_test/TCP_Master_test.vcxproj @@ -0,0 +1,81 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {677F6F19-D890-4DE8-9EFD-08FE0E154AD0} + TCP_Master_test + + + + Application + true + v120 + MultiByte + + + Application + false + v120 + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + true + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + true + + + + + Level3 + MaxSpeed + true + true + true + + + true + true + true + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Modbus_communication/TCP_Master_test/TCP_Master_test.vcxproj.filters b/Modbus_communication/TCP_Master_test/TCP_Master_test.vcxproj.filters new file mode 100644 index 0000000..3634792 --- /dev/null +++ b/Modbus_communication/TCP_Master_test/TCP_Master_test.vcxproj.filters @@ -0,0 +1,42 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + 源文件 + + + 源文件 + + + + + 头文件 + + + 头文件 + + + 头文件 + + + \ No newline at end of file diff --git a/Modbus_communication/TCP_Master_test/TCP_Test.cpp b/Modbus_communication/TCP_Master_test/TCP_Test.cpp new file mode 100644 index 0000000..9197dc8 --- /dev/null +++ b/Modbus_communication/TCP_Master_test/TCP_Test.cpp @@ -0,0 +1,9 @@ +#include "TCP_Test_Code.h" + +int main() +{ + Generate_request_message_test(); + Analysis_response_message_test(); + getchar(); + return 0; +} \ No newline at end of file diff --git a/Modbus_communication/TCP_Master_test/TCP_Test_Code.cpp b/Modbus_communication/TCP_Master_test/TCP_Test_Code.cpp new file mode 100644 index 0000000..fbe649e --- /dev/null +++ b/Modbus_communication/TCP_Master_test/TCP_Test_Code.cpp @@ -0,0 +1,102 @@ +#include "TCP_Test_Code.h" + +/********************************************************************************************* +* Function Test 1: Generate request message +*********************************************************************************************/ +void Generate_request_message_test(void) +{ + char buf[LEN]; + char buf1[LEN]; + CString name = "Example"; + char s[10] = ""; + int Output, Input1, Input2, Input3; + + int number = GetPrivateProfileInt(name, "Number", 0, TESTPATH1); + for (int i = 1; i < number + 1; i++) + { + memset(buf, 0, sizeof(buf)); + memset(buf1, 0, sizeof(buf1)); + memset(s, 0, sizeof(s)); + _itoa_s(i, s, 10); + CString name1 = name + s; + Input1 = GetPrivateProfileInt(name1, "Input1", 0, TESTPATH1); + Input2 = GetPrivateProfileInt(name1, "Input2", 0, TESTPATH1); + Input3 = GetPrivateProfileString(name1, "Input3", "", buf, LEN, TESTPATH1); + Output = GetPrivateProfileString(name1, "Output", "", buf1, LEN, TESTPATH1); + + UINT8 test_Message[300]; + UINT8 Output_Message[300]; + string Write_date = buf; + string Act_Message = buf1; + const char *b = Act_Message.c_str(); + unsigned int Act_len = 0; + for (unsigned int i = 0; i + 3 < Act_Message.length(); i = i + 3) + { + sscanf_s(b + i, "%02X", (int *)(Output_Message + Act_len)); + Act_len++; + } + sscanf_s(b + Act_Message.length() - 2, "%02X", (int *)(Output_Message + Act_len)); + + int len = Crate_TCP_Message(test_Message, Input1, Input2, 0, Write_date); + for (int j = 0; j < len; j++) + { + printf("%02X ", test_Message[j]); + } + printf("\n"); + for (int m = 0; m < len; m++) + { + printf("%02X ", Output_Message[m]); + } + printf("\n"); + + } +} + +/********************************************************************************************* +* Function Test 2: Analysis response message +*********************************************************************************************/ +void Analysis_response_message_test(void) +{ + char buf[LEN]; + char buf1[LEN]; + CString name = "Example"; + char s[10] = ""; + int Input1, Input2; + int number = GetPrivateProfileInt(name, "Number", 0, TESTPATH2); + for (int i = 1; i < number + 1; i++) + { + memset(buf, 0, sizeof(buf)); + memset(buf1, 0, sizeof(buf1)); + memset(s, 0, sizeof(s)); + _itoa_s(i, s, 10); + CString name1 = name + s; + Input1 = GetPrivateProfileString(name1, "Input1", "", buf, LEN, TESTPATH2); + Input2 = GetPrivateProfileString(name1, "Input2", "", buf1, LEN, TESTPATH2); + + UINT8 Response_Message[300]; + string Act_Message = buf; + const char *b = Act_Message.c_str(); + unsigned int Act_len = 0; + for (unsigned int i = 0; i + 3 < Act_Message.length(); i = i + 3) + { + sscanf_s(b + i, "%02X", (int *)(Response_Message + Act_len)); + Act_len++; + } + sscanf_s(b + Act_Message.length() - 2, "%02X", (int *)(Response_Message + Act_len)); + + + UINT8 Request_Message[300]; + string Act_Message1 = buf1; + const char *a = Act_Message1.c_str(); + unsigned int Act_len1 = 0; + for (unsigned int i = 0; i + 3 < Act_Message1.length(); i = i + 3) + { + sscanf_s(a + i, "%02X", (int *)(Request_Message + Act_len1)); + Act_len1++; + } + sscanf_s(a + Act_Message1.length() - 2, "%02X", (int *)(Request_Message + Act_len1)); + Analysis_Response_Message(Response_Message, Request_Message, Act_len+1); + } +} + + diff --git a/Modbus_communication/TCP_Master_test/TCP_Test_Code.h b/Modbus_communication/TCP_Master_test/TCP_Test_Code.h new file mode 100644 index 0000000..3679a4a --- /dev/null +++ b/Modbus_communication/TCP_Master_test/TCP_Test_Code.h @@ -0,0 +1,16 @@ +#ifndef __TCP_TEST_CODE_H +#define __TCP_TEST_CODE_H + +#include +#include "../Modbus_TCP/common.h" +#include "../Modbus_TCP/TCP_client.h" +#include +#include + +#define TESTPATH1 "./1.ini" +#define TESTPATH2 "./2.ini" +#define LEN 65535 + +void Generate_request_message_test(void); +void Analysis_response_message_test(void); +#endif \ No newline at end of file