| @@ -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 | |||
| @@ -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)) | |||
| @@ -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 | |||
| @@ -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) | |||
| @@ -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); | |||
| @@ -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 | |||
| @@ -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" | |||
| @@ -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" | |||
| @@ -0,0 +1,81 @@ | |||
| <?xml version="1.0" encoding="utf-8"?> | |||
| <Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||
| <ItemGroup Label="ProjectConfigurations"> | |||
| <ProjectConfiguration Include="Debug|Win32"> | |||
| <Configuration>Debug</Configuration> | |||
| <Platform>Win32</Platform> | |||
| </ProjectConfiguration> | |||
| <ProjectConfiguration Include="Release|Win32"> | |||
| <Configuration>Release</Configuration> | |||
| <Platform>Win32</Platform> | |||
| </ProjectConfiguration> | |||
| </ItemGroup> | |||
| <PropertyGroup Label="Globals"> | |||
| <ProjectGuid>{677F6F19-D890-4DE8-9EFD-08FE0E154AD0}</ProjectGuid> | |||
| <RootNamespace>TCP_Master_test</RootNamespace> | |||
| </PropertyGroup> | |||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | |||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | |||
| <ConfigurationType>Application</ConfigurationType> | |||
| <UseDebugLibraries>true</UseDebugLibraries> | |||
| <PlatformToolset>v120</PlatformToolset> | |||
| <CharacterSet>MultiByte</CharacterSet> | |||
| </PropertyGroup> | |||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | |||
| <ConfigurationType>Application</ConfigurationType> | |||
| <UseDebugLibraries>false</UseDebugLibraries> | |||
| <PlatformToolset>v120</PlatformToolset> | |||
| <WholeProgramOptimization>true</WholeProgramOptimization> | |||
| <CharacterSet>MultiByte</CharacterSet> | |||
| </PropertyGroup> | |||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | |||
| <ImportGroup Label="ExtensionSettings"> | |||
| </ImportGroup> | |||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
| </ImportGroup> | |||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
| </ImportGroup> | |||
| <PropertyGroup Label="UserMacros" /> | |||
| <PropertyGroup /> | |||
| <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |||
| <ClCompile> | |||
| <WarningLevel>Level3</WarningLevel> | |||
| <Optimization>Disabled</Optimization> | |||
| <SDLCheck>true</SDLCheck> | |||
| <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |||
| </ClCompile> | |||
| <Link> | |||
| <GenerateDebugInformation>true</GenerateDebugInformation> | |||
| </Link> | |||
| </ItemDefinitionGroup> | |||
| <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |||
| <ClCompile> | |||
| <WarningLevel>Level3</WarningLevel> | |||
| <Optimization>MaxSpeed</Optimization> | |||
| <FunctionLevelLinking>true</FunctionLevelLinking> | |||
| <IntrinsicFunctions>true</IntrinsicFunctions> | |||
| <SDLCheck>true</SDLCheck> | |||
| </ClCompile> | |||
| <Link> | |||
| <GenerateDebugInformation>true</GenerateDebugInformation> | |||
| <EnableCOMDATFolding>true</EnableCOMDATFolding> | |||
| <OptimizeReferences>true</OptimizeReferences> | |||
| </Link> | |||
| </ItemDefinitionGroup> | |||
| <ItemGroup> | |||
| <ClCompile Include="..\Modbus_TCP\common.cpp" /> | |||
| <ClCompile Include="..\Modbus_TCP\TCP_client.cpp" /> | |||
| <ClCompile Include="TCP_Test.cpp" /> | |||
| <ClCompile Include="TCP_Test_Code.cpp" /> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ClInclude Include="..\Modbus_TCP\common.h" /> | |||
| <ClInclude Include="..\Modbus_TCP\TCP_client.h" /> | |||
| <ClInclude Include="TCP_Test_Code.h" /> | |||
| </ItemGroup> | |||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | |||
| <ImportGroup Label="ExtensionTargets"> | |||
| </ImportGroup> | |||
| </Project> | |||
| @@ -0,0 +1,42 @@ | |||
| <?xml version="1.0" encoding="utf-8"?> | |||
| <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||
| <ItemGroup> | |||
| <Filter Include="源文件"> | |||
| <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | |||
| <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | |||
| </Filter> | |||
| <Filter Include="头文件"> | |||
| <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | |||
| <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> | |||
| </Filter> | |||
| <Filter Include="资源文件"> | |||
| <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | |||
| <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | |||
| </Filter> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ClCompile Include="TCP_Test.cpp"> | |||
| <Filter>源文件</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="TCP_Test_Code.cpp"> | |||
| <Filter>源文件</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="..\Modbus_TCP\common.cpp"> | |||
| <Filter>源文件</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="..\Modbus_TCP\TCP_client.cpp"> | |||
| <Filter>源文件</Filter> | |||
| </ClCompile> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ClInclude Include="TCP_Test_Code.h"> | |||
| <Filter>头文件</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="..\Modbus_TCP\common.h"> | |||
| <Filter>头文件</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="..\Modbus_TCP\TCP_client.h"> | |||
| <Filter>头文件</Filter> | |||
| </ClInclude> | |||
| </ItemGroup> | |||
| </Project> | |||
| @@ -0,0 +1,9 @@ | |||
| #include "TCP_Test_Code.h" | |||
| int main() | |||
| { | |||
| Generate_request_message_test(); | |||
| Analysis_response_message_test(); | |||
| getchar(); | |||
| return 0; | |||
| } | |||
| @@ -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); | |||
| } | |||
| } | |||
| @@ -0,0 +1,16 @@ | |||
| #ifndef __TCP_TEST_CODE_H | |||
| #define __TCP_TEST_CODE_H | |||
| #include <stdio.h> | |||
| #include "../Modbus_TCP/common.h" | |||
| #include "../Modbus_TCP/TCP_client.h" | |||
| #include <windows.h> | |||
| #include <atlstr.h> | |||
| #define TESTPATH1 "./1.ini" | |||
| #define TESTPATH2 "./2.ini" | |||
| #define LEN 65535 | |||
| void Generate_request_message_test(void); | |||
| void Analysis_response_message_test(void); | |||
| #endif | |||