diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj new file mode 100644 index 0000000..6d25ad6 --- /dev/null +++ b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj @@ -0,0 +1,81 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757} + Modbus_TCP + + + + Application + true + v120 + MultiByte + + + Application + false + v120 + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + false + false + _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/Modbus_TCP/Modbus_TCP.vcxproj.filters b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj.filters new file mode 100644 index 0000000..cd74da4 --- /dev/null +++ b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj.filters @@ -0,0 +1,39 @@ + + + + + {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/Modbus_TCP/Modbus_TCP_log.txt b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt new file mode 100644 index 0000000..038f01e --- /dev/null +++ b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt @@ -0,0 +1,54 @@ +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 +2020-9-7 18:40:38 Send:00 00 00 00 00 06 01 01 00 00 00 01 +2020-9-7 18:40:38 Recv:00 00 00 00 00 04 01 01 01 00 +2020-9-7 18:40:51 Send:00 00 00 00 00 06 01 03 00 00 00 64 +2020-9-7 18:40:51 Recv:00 00 00 00 00 CB 01 03 C8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 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-7 18:41:20 Send:00 00 00 00 00 06 01 03 00 63 00 01 +2020-9-7 18:41:20 Recv:00 00 00 00 00 05 01 03 02 00 00 +2020-9-7 18:41:34 Send:00 00 00 00 00 06 01 03 00 63 00 03 +2020-9-7 18:41:34 Recv:00 00 00 00 00 09 01 03 06 00 00 00 00 00 00 +2020-9-7 18:41:55 Send:00 00 00 00 00 06 01 03 00 63 00 03 +2020-9-7 18:41:55 Recv:00 00 00 00 00 09 01 03 06 00 00 00 00 00 00 +2020-9-7 18:43:7 Send:00 00 00 00 00 06 01 03 00 00 00 01 +2020-9-7 18:44:25 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 +2020-9-7 18:44:25 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 +2020-9-7 18:45:27 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 +2020-9-7 18:46:53 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 +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 diff --git a/Modbus_communication/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp new file mode 100644 index 0000000..76b1e4a --- /dev/null +++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp @@ -0,0 +1,261 @@ +#include "TCP_client.h" + + +/********************************************************************************************* +* 功能    : 检测socket版本 +* 描述   : 检测socket版本是否一致 +* 输入 : 无 +* 输出 : true socket版本一致 +* false socket版本不一致 +*********************************************************************************************/ +bool InitSocket_Version(void) +{ + WORD sockVersion = MAKEWORD(2, 2);//使用winsocket2.2版本 + WSADATA wsaData; + if (WSAStartup(sockVersion, &wsaData) != 0) + { + return false; + } + return true; +} + +/********************************************************************************************* +* 功能    : IP地址有效性检测 +* 描述   : 检测输入的IP地址是否合法 +* 输入 : IP 输入的IP地址 +* 输出 : true IP地址合法 +* false IP地址非法 +*********************************************************************************************/ +bool Check_IP(string IP) +{ + int s[4]; + string ip = IP; + if (ip.length() < 7 || ip.length() > 15) //长度判定 + return false; + if (sscanf_s(IP.c_str(), "%d.%d.%d.%d", &s[0], &s[1], &s[2], &s[3]) != 4) //IPV4格式正确 + { + return false; + } + string newip = to_string(s[0]) + "." + to_string(s[1]) + "." + to_string(s[2]) + "." + to_string(s[3]); + if (ip != newip) //前导0 + return false; + if ((s[0] & 0xffffff00) || (s[1] & 0xffffff00) || (s[2] & 0xffffff00) || (s[3] & 0xffffff00)) //判断每一段大小是否符合要求 + { + return false; + } + return true; +} + +/********************************************************************************************* +* 功能    : 获取从站IP地址和端口号 +* 描述   : 终端输入从站IP地址和端口号 +* 输入 : IP 地址 (IPV4) +* *Port_number 端口号(1-65535) +* 输出 : 无 +*********************************************************************************************/ +void Input_IP(string& IP, unsigned int *Port_number) +{ + int i = 1; + do + { + if (i == 1) + { + cout << "请输入从站IP:"; + i = 0; + } + else + cout << "IP地址格式不正确,请重新输入从站IP:"; + cin >> IP; + } while (!Check_IP(IP)); + + i = 1; + cout << "IP地址输格式入正确,请输入从站端口号:"; + do + { + if (i != 1) + cout << "请重新输入从站端口号:"; + cin >> *Port_number; + i = 0; + } while (*Port_number == 0 || *Port_number > 65535); //端口不能为0 ,端口号范围0---65535 + +} + +/********************************************************************************************* +* 功能    : 初始化客户端 +* 描述   : 根据终端输入从站IP地址和端口号连接对应的服务器,设置超时时间 +* 输入 : IP 地址 (IPV4) +* Port_number 端口号(1-65535) +* 输出 : ClientSocket 连接成功的套接字 +*********************************************************************************************/ +SOCKET Init_client(string IP, unsigned int Port_number) +{ + if (InitSocket_Version() == 0) + return INVALID_SOCKET; + SOCKET ClientSocket = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in Serversock_in; + Serversock_in.sin_addr.S_un.S_addr = inet_addr(IP.c_str()); + Serversock_in.sin_family = AF_INET; + Serversock_in.sin_port = htons(Port_number); + while (SOCKET_ERROR == connect(ClientSocket, (SOCKADDR*)&Serversock_in, sizeof(SOCKADDR))) + { + cout << "尝试连接TCP从站失败" << endl; + } + cout << "连接TCP从站成功" << endl; + TIMEVAL timeout; + timeout.tv_sec = 200; //ms + timeout.tv_usec = 0; //us + setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间 + return ClientSocket; +} + +/********************************************************************************************* +* 功能    : 清除接收缓冲区中的数据 +* 描述   : 每一次发送请求前清空缓存区数据 +* 输入 : ClientSocket 客户端套接字 +* 输出 : 无 +*********************************************************************************************/ +void Clear_recv_buf(SOCKET clientSocket) +{ + struct timeval tmOut; + tmOut.tv_sec = 0; + tmOut.tv_usec = 0; + fd_set 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; + recv(clientSocket, tmp, 1, 0); + } +} + +void Printf_Coil_date(UINT8 *Response_Message, UINT8 *Request_Message) +{ + printf("从站设备ID %02X 功能码为 %02X\n", Response_Message[6], Response_Message[7]); + unsigned int temp1 = ((Request_Message[8] << 8) | Request_Message[9])+1; + unsigned int Number = ((Request_Message[10] << 8) | Request_Message[11]); + printf("线圈起始地址为%d \n",temp1); + unsigned int temp = temp1; + for (int i = 0; i < Response_Message[8]; i++) + { + unsigned int temp2 = temp + 7; + if (temp2 > temp1 + Number - 1) + temp2 = temp1 + Number - 1; + printf("线圈第%d --- %d的状态为:%02X \n", temp2, temp, Response_Message[9+i]); + temp = temp + 8; + } +} + +void Printf_Register_date(UINT8 *Response_Message, UINT8 *Request_Message) +{ + printf("从站设备ID %02X 功能码为 %02X\n", Response_Message[6], Response_Message[7]); + unsigned int temp1 = ((Request_Message[8] << 8) | Request_Message[9]) + 1; + unsigned int Number = ((Request_Message[10] << 8) | Request_Message[11]); + 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]); + } + +} +void Printf_Anomaly_date(UINT8 *Response_Message) +{ + UINT8 a = Response_Message[8]; + printf("从站设备ID %02X 功能码为 %02X\n", Response_Message[6], Response_Message[7]); + switch (a) + { + case 0x01: printf("%02X : 从站设备不支持此功能码",a); break; + case 0x02: printf("%02X : 指定的数据地址在从站设备中不存在",a); break; + case 0x03: printf("%02X : 指定的数据超过范围或者不允许使用",a); break; + case 0x04: printf("%02X : 从站设备处理响应的过程中,出现未知错误等",a); break; + default: printf("Unkown Other Error!!!!!"); + } +} + + + + +bool Analysis_Response_Message(UINT8 *Response_Message, UINT8 *Request_Message, int Response_Message_len) +{ + if (Response_Message[7] == Request_Message[7] + 0x80 && Response_Message_len == 9)//先处理异常响应 + { + Printf_Anomaly_date(Response_Message); + return true; + } + else if (Response_Message[6] == Request_Message[6] && Response_Message_len - 5 == Response_Message[5]) //判断是否是正常响应帧 + { + if (Response_Message[7] == 0x01) + Printf_Coil_date(Response_Message, Request_Message); + if (Response_Message[7] == 0x03) + 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])); + return true; + } + return false; +} + +/********************************************************************************************* +* 功能    : 运行客户端 +* 描述   : 根据终端输入从站IP地址和端口号连接对应的服务器并且生成发送请求等待响应 +* 输入 : IP 地址 (IPV4) +* Port_number 端口号(1-65535) +* 输出 : false 执行出错 true 执行成功 +*********************************************************************************************/ +bool Tcp_client(string IP, unsigned int Port_number) +{ + SOCKET ClientSocket = Init_client(IP, Port_number); + if (INVALID_SOCKET == ClientSocket) + { + cout << "初始化主站失败" << endl; + return false; + } + UINT8 Request_Message[260]; + UINT8 Response_Message[260]; + while (true) + { + + int Function_code = Input_Function_code(); + unsigned int Operations_Number = Input_Operations_Number(Function_code); + unsigned int Starting_address = Input_Starting_address(); + string Write_date = Input_Write_date(Function_code, Operations_Number); + int Request_Message_len = Crate_TCP_Message(Request_Message, Function_code, Operations_Number, Starting_address, Write_date); + printf("主站请求 :"); + for (int i = 0; i < Request_Message_len; i++) + { + printf("%02x ", Request_Message[i]); + } + printf("\n"); + Log_Note(Request_Message, 1, Request_Message_len); + Clear_recv_buf(ClientSocket); + send(ClientSocket, (char*)Request_Message, Request_Message_len, 0); + memset(Response_Message, 0, 260); + int Response_Message_len = recv(ClientSocket, (char*)Response_Message, 260, 0); + if (Response_Message_len > 0) + { + Log_Note(Response_Message, 0, Response_Message_len); + printf("从站响应 :"); + for (int i = 0; i < Response_Message_len; i++) + { + printf("%02x ", Response_Message[i]); + } + printf("\n"); + if (Analysis_Response_Message(Response_Message, Request_Message, Response_Message_len)) + printf("响应报文异常\n\n"); + } + else + cout << "响应超时" << endl; + } + //关闭套接字 + closesocket(ClientSocket); + //关闭服务 + WSACleanup(); + return true; +} diff --git a/Modbus_communication/Modbus_TCP/TCP_client.h b/Modbus_communication/Modbus_TCP/TCP_client.h new file mode 100644 index 0000000..9f5b5e8 --- /dev/null +++ b/Modbus_communication/Modbus_TCP/TCP_client.h @@ -0,0 +1,14 @@ +#ifndef __TCP_CLIENT_H +#define __TCP_CLIENT_H + +#include "common.h" + + + +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); + + +#endif \ No newline at end of file diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp new file mode 100644 index 0000000..30da7d4 --- /dev/null +++ b/Modbus_communication/Modbus_TCP/common.cpp @@ -0,0 +1,311 @@ +#include "common.h" + + +/********************************************************************************************* +* 功能    : 选择功能码 +* 描述   : 在功能码 0x01 0x03 0x0F 0x10中选择一个功能码 +* 输入 : 无 +* 输出 : Function_code 选择的功能码 +*********************************************************************************************/ +int Input_Function_code(void) +{ + int Function_code; + cin >> hex; + int i = 1; + printf("功能码 : 0x01 0x03 0x0F 0x10 \n"); + printf("说明 : 读线圈 读寄存器 写多个线圈 写多个寄存器\n"); + do + { + if (i == 1) + { + i = 0; + cout << "请输入选择的功能码:"; + } + else + cout << "输入功能码错误,请重新选择的功能码"; + cin >> Function_code; + cin.clear(); + cin.sync(); + } while (Function_code != 0x01 && Function_code != 0x03 && Function_code != 0x0F && Function_code != 0x10); + return Function_code; +} + +/********************************************************************************************* +* 功能    : 输入操作起始地址码 +* 描述   : 起始地址码,最大值65535 +* 输入 : 无 +* 输出 : Starting_address 操作起始地址码 +*********************************************************************************************/ +unsigned int Input_Starting_address(void) +{ + unsigned int Starting_address; + int i = 1; + cin >> dec; + do + { + if (i == 1) + { + i = 0; + cout << "请输入操作(十进制)起始地址:"; + } + else + cout << "请重新输入操作(十进制)起始地址"; + cin >> Starting_address; + cin.clear(); + cin.sync(); + } while (Starting_address > 65535 || Starting_address < 0); + return Starting_address; +} + +/********************************************************************************************* +* 功能    : 输入操作数量 +* 描述   : 操作数量,0x01 1-200 0x03 1-125 0x0xF 1-1968 0x10 1-123 +* 输入 : Function_code 选择的功能码类型 +* 输出 : Operations_Number 对应功能码类型的操作数量 +*********************************************************************************************/ +unsigned int Input_Operations_Number(int Function_code) +{ + unsigned int Operations_Number; + int i = 1; + bool flage = true; + printf(" 功能码 0x01 0x03 0x0F 0x10\n"); + printf("操作数量 1-2000 1-125 1-1968 1-123\n"); + cin >> dec; + do + { + if (i == 1) + { + i = 0; + cout << "请输入操作(十进制)数量:"; + } + else + cout << "请重新输入操作(十进制)数量"; + cin >> Operations_Number; + cin.clear(); + cin.sync(); + if (Function_code == 0x01 && Operations_Number >= 1 && Operations_Number <= 2000) + flage = false; + if (Function_code == 0x03 && Operations_Number >= 1 && Operations_Number <= 125) + flage = false; + if (Function_code == 0x0F && Operations_Number >= 1 && Operations_Number <= 1968) + flage = false; + if (Function_code == 0x10 && Operations_Number >= 1 && Operations_Number <= 123) + flage = false; + } while (flage); + return Operations_Number; +} + +/********************************************************************************************* +* 功能    : 检测写入数据 +* 描述   : 检测写入数据格式是否正确 +* 输入 : Write_date 写入的数据 Write_date_number写入数据的组数 +* 输出 : true 写入数据格式正确 +* false 写入数据格式错误 +*********************************************************************************************/ +bool Check_Write_date(string Write_date, int Write_date_number) +{ + int Write_date_len = 2 * Write_date_number + Write_date_number - 1; + if (Write_date.length() != Write_date_len) + return false; + int Space_number = 0; + int j = -1; + for (unsigned int i = 0; i < Write_date.length(); i++) + { + if (Write_date[i] == 0x20)//空格位置判定 + { + if (i - j == 3) + { + Space_number++; + j = i; + continue; + } + else + return false; + } + if ((Write_date[i] >= 0x30 && Write_date[i] <= 0x39)) // 1-9 + continue; + else if ((Write_date[i] >= 0x41 && Write_date[i] <= 0x5A)) //A-Z + continue; + else if ((Write_date[i] >= 0x61 && Write_date[i] <= 0x7A)) //a-z + continue; + else + return false; + } + if (Space_number != Write_date_number - 1) + return false; + return true; +} + + +/********************************************************************************************* +* 功能    : 计算写入数据的字节数 +* 描述   : 通过对应的功能码和操作数量计算对应的数据字节数 +* 输入 : Function_code 选择的功能码类型 Operations_Number对应功能码类型的操作数量 +* 输出 : 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 == 0x0F) //读线圈 + { + Write_date_number = Operations_Number / 8; + if (Operations_Number % 8) + Write_date_number++; + } + if (Function_code == 0x10) + Write_date_number = Operations_Number * 2; + return Write_date_number; +} + +/********************************************************************************************* +* 功能    : 输入写入数据 +* 描述   : 根据写入数量和功能码类型输入相应的数据 +* 输入 : Function_code 选择的功能码类型 Operations_Number对应功能码类型的操作数量 +* 输出 : Write_date 写入的数据 +*********************************************************************************************/ +string Input_Write_date(int Function_code, unsigned int Operations_Number) +{ + int i = 1; + string Write_date; + unsigned int Write_date_number = Count_Write_date_number(Function_code, Operations_Number); + if (Function_code == 0x01 || Function_code == 0x03) + return Write_date;//读线圈/寄存器不需要输入写入数据 + + printf("请输入N组写入数据(2位16进制为一组,空格间隔)例如:00 00 01 00 20 05\n"); + do + { + if (i == 1) + { + i = 0; + printf("请输入%d组写入数据 :", Write_date_number); + } + else + printf("请重新输入%d组写入数据 :", Write_date_number); + getline(cin, Write_date); + cin.clear(); + cin.sync(); + } while (!Check_Write_date(Write_date, Write_date_number)); + + return Write_date; +} + +/********************************************************************************************* +* 功能    : 生成MBAP报头 +* 描述 : MBAP报文头的包括的内容: +* +-------------+---------+--------+--------+------------------------------+ +* | 域 | 长度 | 客户机 | 服务器 | 描述 | +* +-------------+---------+--------+--------+------------------------------+ +* |事务元标识符 | 2个字节 | 启动 | 复制 |请求/响应事务处理的识别码 | +* +-------------+---------+--------+--------+------------------------------+ +* |协议标识符 | 2个字节 | 启动 | 复制 |0=MODBUS 协议 | +* +-------------+---------+--------+--------+------------------------------+ +* | 长度 | 2个字节 | 启动 | 启动 |以下字节的数量 | +* +-------------+---------+--------+--------+------------------------------+ +* |单元标识符 | 1个字节 | 启动 | 复制 |连接的远程从站的识别码 | +* 输入 : 无 +* 输出 : MBAP报文头内容 +*********************************************************************************************/ +void Crate_MBAP(UINT8 *Message, int Function_code, unsigned int Operations_Number) +{ + Message[0] = 0x00; + Message[1] = 0x00; + Message[2] = 0x00; + Message[3] = 0x00; + Message[4] = 0x00; + Message[5] = Count_Write_date_number(Function_code, Operations_Number) + 0x06; + Message[6] = DEVICE_ID; +} + +/********************************************************************************************* +* 功能    : 字符串转UINT8类型 +* 描述   : 根据写入数量和功能码类型输入相应的数据 +* 输入 : *Message 消息帧存放的数组 Write_date 要转换的字符串 +* Message_len消息帧数组的起始位置 +* 输出 : 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) + { + sscanf_s(b + i, "%02X", (int *)(Message + Message_len)); + Message_len++; + } + sscanf_s(b + Write_date.length() - 2, "%02X", (int *)(Message + Message_len)); + return ++Message_len; +} + +/********************************************************************************************* +* 功能    : 生成TCP模式下的消息帧 +* 描述   : 根据要写入的数据生成消息帧 +* 输入 : *Message 消息帧存放的数组 Write_date 写入数据的字符串 +* Function_code 功能码 Operations_Number 操作数量 Starting_address起始地址 +* 输出 : 消息帧的总长度 +*********************************************************************************************/ +int Crate_TCP_Message(UINT8 *Message, int Function_code, unsigned int Operations_Number, unsigned int Starting_address, string Write_date) +{ + + Crate_MBAP(Message, Function_code, Operations_Number); + Message[7] = Function_code;//功能码 + Message[8] = Starting_address >> 8;//起始地址H + Message[9] = Starting_address;//起始地址L + Message[10] = Operations_Number >> 8;//操作数量H + Message[11] = Operations_Number;//操作数量L + + return HexStringtoByte(Message, Write_date, 12, Function_code, Operations_Number); + +} + +/********************************************************************************************* +* 功能    : 日志记录 +* 描述   : 记录每次通信的请求和响应报文 +* 输入 : *Message 消息帧存放的数组 flage = 1 请求报文 flage = 其他 响应报文 +* Message_len 报文数据长度 +* 输出 : 无 +*********************************************************************************************/ + +void Log_Note(UINT8 *Message, int flage, int Message_len) +{ + FILE *fp = NULL; + time_t timep; + struct tm *p; + time(&timep); + p = gmtime(&timep); + string Recv_str = to_string(1900 + p->tm_year) + "-" + to_string(1 + p->tm_mon) + "-" + to_string(p->tm_mday) + + " " + to_string(8 + p->tm_hour) + ":" + to_string(p->tm_min) + ":" + to_string(p->tm_sec) + " Recv:"; + string Send_str= to_string(1900 + p->tm_year) + "-" + to_string(1 + p->tm_mon) + "-" + to_string(p->tm_mday) + + " " + to_string(8 + p->tm_hour) + ":" + to_string(p->tm_min) + ":" + to_string(p->tm_sec) + " Send:"; + + /* 打开文件用于读写 */ + if ((fp = fopen("Modbus_TCP_log.txt", "a+")) == NULL) + { + printf("打开文件失败"); + return ; + } + + + if (flage == 1) + { + fwrite(Send_str.c_str(), Send_str.length(), 1, fp); + for (int i = 0; i < Message_len; i++) + { + fprintf(fp, "%02X ", Message[i]); + } + fprintf(fp,"\n"); + } + else + { + fwrite(Recv_str.c_str(), Recv_str.length(), 1, fp); + for (int i = 0; i < Message_len; i++) + { + fprintf(fp, "%02X ", Message[i]); + } + fprintf(fp, "\n"); + } + fclose(fp); + fp = NULL; +} diff --git a/Modbus_communication/Modbus_TCP/common.h b/Modbus_communication/Modbus_TCP/common.h new file mode 100644 index 0000000..790816b --- /dev/null +++ b/Modbus_communication/Modbus_TCP/common.h @@ -0,0 +1,30 @@ +#ifndef __COMMON_H +#define __COMMON_H + +#include +#include +#include +#include +#include +#include +#include +#pragma comment(lib,"ws2_32.lib") +using namespace std; + +#define DEVICE_ID 0x01 //豸ID + + + +int Input_Function_code(void); +unsigned int Input_Starting_address(void); +unsigned int Input_Operations_Number(int Function_code); +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 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); + + +#endif \ No newline at end of file diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp new file mode 100644 index 0000000..c417351 --- /dev/null +++ b/Modbus_communication/Modbus_TCP/main.cpp @@ -0,0 +1,11 @@ +#include "main.h" + +int main() +{ + string IP; + unsigned int Port; + Input_IP(IP,&Port); + Tcp_client(IP,Port); + getchar(); + return 0; +} \ No newline at end of file diff --git a/Modbus_communication/Modbus_TCP/main.h b/Modbus_communication/Modbus_TCP/main.h new file mode 100644 index 0000000..4bac59f --- /dev/null +++ b/Modbus_communication/Modbus_TCP/main.h @@ -0,0 +1,6 @@ +#ifndef __MAIN_H +#define __MAIN_H + +#include "TCP_client.h" + +#endif \ No newline at end of file diff --git a/Modbus_communication/Modbus_communication.sln b/Modbus_communication/Modbus_communication.sln index b8f423a..38aa418 100644 --- a/Modbus_communication/Modbus_communication.sln +++ b/Modbus_communication/Modbus_communication.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 12.0.30501.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Modbus_RTU_Salve", "Modbus_RTU_Salve\Modbus_RTU_Salve.vcxproj", "{8C67F4EB-BDBE-4605-9291-5A535AC44AE2}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Modbus_TCP", "Modbus_TCP\Modbus_TCP.vcxproj", "{CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -15,6 +17,10 @@ Global {8C67F4EB-BDBE-4605-9291-5A535AC44AE2}.Debug|Win32.Build.0 = Debug|Win32 {8C67F4EB-BDBE-4605-9291-5A535AC44AE2}.Release|Win32.ActiveCfg = Release|Win32 {8C67F4EB-BDBE-4605-9291-5A535AC44AE2}.Release|Win32.Build.0 = Release|Win32 + {CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}.Debug|Win32.ActiveCfg = Debug|Win32 + {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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE