From eb4172f45a9723e2ce18fb5fd5944fbf866c958a Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Sat, 29 Aug 2020 08:37:13 +0800
Subject: [PATCH 01/16] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E9=A1=B9=E7=9B=AEModbu?=
=?UTF-8?q?s=5FTCP?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Modbus_TCP/Modbus_TCP.vcxproj | 71 +++++++++++++++++++
.../Modbus_TCP/Modbus_TCP.vcxproj.filters | 17 +++++
2 files changed, 88 insertions(+)
create mode 100644 Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj
create mode 100644 Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj.filters
diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj
new file mode 100644
index 0000000..f34ee8f
--- /dev/null
+++ b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj
@@ -0,0 +1,71 @@
+
+
+
+
+ Debug
+ Win32
+
+
+ Release
+ Win32
+
+
+
+ {CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}
+ Modbus_TCP
+
+
+
+ Application
+ true
+ v120
+ MultiByte
+
+
+ Application
+ false
+ v120
+ true
+ MultiByte
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Level3
+ Disabled
+ true
+
+
+ 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..03c8628
--- /dev/null
+++ b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj.filters
@@ -0,0 +1,17 @@
+
+
+
+
+ {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
From 083271a04967f9be5030f3d475fd2f950314fd06 Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Sat, 29 Aug 2020 08:55:04 +0800
Subject: [PATCH 02/16] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20commom.cpp=20common.?=
=?UTF-8?q?h=20main.cpp=20main.h=20TCP=5Fclient.cpp=20TCP=5Fclient.h?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Modbus_TCP/Modbus_TCP.vcxproj | 8 +++
.../Modbus_TCP/Modbus_TCP.vcxproj.filters | 22 ++++++++
.../Modbus_TCP/TCP_client.cpp | 42 +++++++++++++++
Modbus_communication/Modbus_TCP/TCP_client.h | 8 +++
Modbus_communication/Modbus_TCP/common.cpp | 52 +++++++++++++++++++
Modbus_communication/Modbus_TCP/common.h | 17 ++++++
Modbus_communication/Modbus_TCP/main.cpp | 9 ++++
Modbus_communication/Modbus_TCP/main.h | 6 +++
Modbus_communication/Modbus_communication.sln | 12 +++++
9 files changed, 176 insertions(+)
create mode 100644 Modbus_communication/Modbus_TCP/TCP_client.cpp
create mode 100644 Modbus_communication/Modbus_TCP/TCP_client.h
create mode 100644 Modbus_communication/Modbus_TCP/common.cpp
create mode 100644 Modbus_communication/Modbus_TCP/common.h
create mode 100644 Modbus_communication/Modbus_TCP/main.cpp
create mode 100644 Modbus_communication/Modbus_TCP/main.h
diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj
index f34ee8f..d78837d 100644
--- a/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj
+++ b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj
@@ -64,6 +64,14 @@
+
+
+
+
+
+
+
+
diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj.filters b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj.filters
index 03c8628..cd74da4 100644
--- a/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj.filters
+++ b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj.filters
@@ -14,4 +14,26 @@
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/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp
new file mode 100644
index 0000000..419b5ad
--- /dev/null
+++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp
@@ -0,0 +1,42 @@
+#include "TCP_client.h"
+
+SOCKET Init_client(void)
+{
+ 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("10.10.0.105");
+ serversock_in.sin_family = AF_INET;
+ serversock_in.sin_port = htons(3000);
+ if (SOCKET_ERROR == connect(clientSocket, (SOCKADDR*)&serversock_in, sizeof(SOCKADDR)))
+ {
+ cout << "Ӵվʧ" << endl;
+ return INVALID_SOCKET;
+ }
+ return clientSocket;
+}
+
+int Tcp_client(void)
+{
+
+ SOCKET clientSocket = Init_client();
+ if (INVALID_SOCKET == clientSocket)
+ {
+ cout << "ʼվʧ" << endl;
+ return 0;
+ }
+ while (true)
+ {
+ char receiveBuf[100];
+ memset(receiveBuf, 0, 100);
+ recv(clientSocket, receiveBuf, 100, 0);
+ printf("%s\n", receiveBuf);
+ send(clientSocket, "hello,this is client", strlen("hello,this is client") + 1, 0);
+ }
+ //ر
+ closesocket(clientSocket);
+ //رշ
+ WSACleanup();
+ return 0;
+}
diff --git a/Modbus_communication/Modbus_TCP/TCP_client.h b/Modbus_communication/Modbus_TCP/TCP_client.h
new file mode 100644
index 0000000..e65650c
--- /dev/null
+++ b/Modbus_communication/Modbus_TCP/TCP_client.h
@@ -0,0 +1,8 @@
+#ifndef __TCP_CLIENT_H
+#define __TCP_CLIENT_H
+
+#include "common.h"
+
+int Tcp_client(void);
+
+#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..4ce27e0
--- /dev/null
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -0,0 +1,52 @@
+#include "common.h"
+
+
+bool InitSocket_Version(void)
+{
+ WORD sockVersion = MAKEWORD(2, 2);//ʹwinsocket2.2汾
+ WSADATA wsaData;
+ if (WSAStartup(sockVersion, &wsaData) != 0)
+ {
+ return false;
+ }
+ return true;
+}
+
+bool Check_IP(char* IP)
+{
+ int s[4];
+ string ip = IP;
+ if (ip.length() < 7 || ip.length() > 15) //ж
+ return false;
+ if (sscanf_s(IP, "%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;
+}
+
+void Input_IP(vector & a)
+{
+ cout << "վIP" << endl;
+ char IP[100];
+ while (cin >> IP) // ctrl + Z + Enter
+ {
+ if (Check_IP(IP))
+ {
+ a.push_back(IP);
+ memset(IP, 0, 100);
+ }
+ else
+ {
+ cout << "IPʽ" << endl;
+ memset(IP, 0, 100);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Modbus_communication/Modbus_TCP/common.h b/Modbus_communication/Modbus_TCP/common.h
new file mode 100644
index 0000000..f163f25
--- /dev/null
+++ b/Modbus_communication/Modbus_TCP/common.h
@@ -0,0 +1,17 @@
+#ifndef __COMMON_H
+#define __COMMON_H
+
+#include
+#include
+#include
+#include
+#include
+#include
+#pragma comment(lib,"ws2_32.lib")
+using namespace std;
+
+bool InitSocket_Version(void);
+bool Check_IP(char* IP);
+void Input_IP(vector & a);
+
+#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..f82d79a
--- /dev/null
+++ b/Modbus_communication/Modbus_TCP/main.cpp
@@ -0,0 +1,9 @@
+#include "main.h"
+
+
+int main()
+{
+
+ 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 437b87b..25bb5c7 100644
--- a/Modbus_communication/Modbus_communication.sln
+++ b/Modbus_communication/Modbus_communication.sln
@@ -3,7 +3,19 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30501.0
MinimumVisualStudioVersion = 10.0.40219.1
+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
+ Release|Win32 = Release|Win32
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {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
EndGlobalSection
From 60e3dd008445b4f08a8d67a0ce8dfb3091763cd1 Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Sat, 29 Aug 2020 14:50:29 +0800
Subject: [PATCH 03/16] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=BE=93=E5=85=A5IP?=
=?UTF-8?q?=E5=92=8C=E7=AB=AF=E5=8F=A3=EF=BC=8C=E8=BF=9E=E6=8E=A5=E4=BB=8E?=
=?UTF-8?q?=E7=AB=99=EF=BC=8C=E5=92=8C=E4=BB=8E=E7=AB=99=E9=80=9A=E4=BF=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Modbus_TCP/TCP_client.cpp | 31 ++++----
Modbus_communication/Modbus_TCP/TCP_client.h | 2 +-
Modbus_communication/Modbus_TCP/common.cpp | 72 +++++++++++++------
Modbus_communication/Modbus_TCP/common.h | 5 +-
Modbus_communication/Modbus_TCP/main.cpp | 13 +++-
5 files changed, 81 insertions(+), 42 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp
index 419b5ad..2c11a15 100644
--- a/Modbus_communication/Modbus_TCP/TCP_client.cpp
+++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp
@@ -1,34 +1,33 @@
#include "TCP_client.h"
-SOCKET Init_client(void)
+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("10.10.0.105");
- serversock_in.sin_family = AF_INET;
- serversock_in.sin_port = htons(3000);
- if (SOCKET_ERROR == connect(clientSocket, (SOCKADDR*)&serversock_in, sizeof(SOCKADDR)))
+ 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 << "Ӵվʧ" << endl;
- return INVALID_SOCKET;
+ cout << "TCPվʧ" << endl;
}
- return clientSocket;
+ cout << "TCPվɹ" << endl;
+ return ClientSocket;
}
-int Tcp_client(void)
+int Tcp_client(string IP, unsigned int Port_number)
{
-
- SOCKET clientSocket = Init_client();
+ SOCKET clientSocket = Init_client(IP, Port_number);
if (INVALID_SOCKET == clientSocket)
{
cout << "ʼվʧ" << endl;
- return 0;
+ return false;
}
while (true)
{
- char receiveBuf[100];
+ char receiveBuf[260];
memset(receiveBuf, 0, 100);
recv(clientSocket, receiveBuf, 100, 0);
printf("%s\n", receiveBuf);
@@ -38,5 +37,5 @@ int Tcp_client(void)
closesocket(clientSocket);
//رշ
WSACleanup();
- return 0;
+ return false;
}
diff --git a/Modbus_communication/Modbus_TCP/TCP_client.h b/Modbus_communication/Modbus_TCP/TCP_client.h
index e65650c..8e40027 100644
--- a/Modbus_communication/Modbus_TCP/TCP_client.h
+++ b/Modbus_communication/Modbus_TCP/TCP_client.h
@@ -3,6 +3,6 @@
#include "common.h"
-int Tcp_client(void);
+int 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
index 4ce27e0..d35802b 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -1,9 +1,15 @@
-#include "common.h"
-
+#include "common.h"
+/*********************************************************************************************
+* 功能 : 检测socket版本
+* 描述 : 检测socket版本是否一致
+* 输入 : 无
+* 输出 : true socket版本一致
+* false socket版本不一致
+*********************************************************************************************/
bool InitSocket_Version(void)
{
- WORD sockVersion = MAKEWORD(2, 2);//ʹwinsocket2.2汾
+ WORD sockVersion = MAKEWORD(2, 2);//使用winsocket2.2版本
WSADATA wsaData;
if (WSAStartup(sockVersion, &wsaData) != 0)
{
@@ -12,41 +18,63 @@ bool InitSocket_Version(void)
return true;
}
-bool Check_IP(char* IP)
+/*********************************************************************************************
+* 功能 : 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) //ж
+ if (ip.length() < 7 || ip.length() > 15) //长度判定
return false;
- if (sscanf_s(IP, "%d.%d.%d.%d", &s[0], &s[1], &s[2], &s[3]) != 4) //IPV4ʽȷ
+ 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
+ if (ip != newip) //前导0
return false;
- if ((s[0] & 0xffffff00) || (s[1] & 0xffffff00) || (s[2] & 0xffffff00) || (s[3] & 0xffffff00)) //жÿһδСǷҪ
+ if ((s[0] & 0xffffff00) || (s[1] & 0xffffff00) || (s[2] & 0xffffff00) || (s[3] & 0xffffff00)) //判断每一段大小是否符合要求
{
return false;
}
return true;
}
-void Input_IP(vector & a)
+/*********************************************************************************************
+* 功能 : 获取从站IP地址和端口号
+* 描述 : 终端输入从站IP地址和端口号
+* 输入 : IP 地址 (IPV4)
+* *Port_number 端口号(1-65535)
+* 输出 : 无
+*********************************************************************************************/
+void Input_IP(string& IP, unsigned int *Port_number)
{
- cout << "վIP" << endl;
- char IP[100];
- while (cin >> IP) // ctrl + Z + Enter
+ int i = 1;
+ do
{
- if (Check_IP(IP))
- {
- a.push_back(IP);
- memset(IP, 0, 100);
- }
- else
+ if (i == 1)
{
- cout << "IPʽ" << endl;
- memset(IP, 0, 100);
+ cout << "请输入从站IP:";
+ i = 0;
}
- }
-}
\ No newline at end of file
+ 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
+
+}
diff --git a/Modbus_communication/Modbus_TCP/common.h b/Modbus_communication/Modbus_TCP/common.h
index f163f25..8d7c125 100644
--- a/Modbus_communication/Modbus_TCP/common.h
+++ b/Modbus_communication/Modbus_TCP/common.h
@@ -10,8 +10,9 @@
#pragma comment(lib,"ws2_32.lib")
using namespace std;
-bool InitSocket_Version(void);
+
bool Check_IP(char* IP);
-void Input_IP(vector & a);
+bool InitSocket_Version(void);
+void Input_IP(string& IP, unsigned int *Port_number);
#endif
\ No newline at end of file
diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp
index f82d79a..bf05931 100644
--- a/Modbus_communication/Modbus_TCP/main.cpp
+++ b/Modbus_communication/Modbus_TCP/main.cpp
@@ -1,9 +1,20 @@
#include "main.h"
+string input()
+{
+ string str;
+ cout << "stringݣ" << endl;
+ getline(cin, str, '\n');
+ cout << str << endl;
+ return str;
+}
int main()
{
-
+ string IP;
+ unsigned int Port_number;
+ Input_IP(IP, &Port_number);
+ Tcp_client(IP, Port_number);
getchar();
return 0;
}
\ No newline at end of file
From 3811aa4723c554d1835da6fa281c85940164f124 Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Sat, 29 Aug 2020 16:12:31 +0800
Subject: [PATCH 04/16] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=8A=9F=E8=83=BD?=
=?UTF-8?q?=E7=A0=81=E9=80=89=E6=8B=A9=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Modbus_communication/Modbus_TCP/common.cpp | 29 +++++++++++++++
Modbus_communication/Modbus_TCP/main.cpp | 41 +++++++++++++++-------
2 files changed, 57 insertions(+), 13 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp
index d35802b..a4fdd9e 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -78,3 +78,32 @@ void Input_IP(string& IP, unsigned int *Port_number)
} while (*Port_number == 0 || *Port_number > 65535); //端口不能为0 ,端口号范围0---65535
}
+
+/*********************************************************************************************
+* 功能 : 选择功能码
+* 描述 : 在功能码 0x01 0x03 0x0F 0x10中选择一个功能码
+* 输入 : 无
+* 输出 : 选择的功能码
+*********************************************************************************************/
+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;
+}
diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp
index bf05931..bd2a329 100644
--- a/Modbus_communication/Modbus_TCP/main.cpp
+++ b/Modbus_communication/Modbus_TCP/main.cpp
@@ -1,20 +1,35 @@
#include "main.h"
-string input()
-{
- string str;
- cout << "stringݣ" << endl;
- getline(cin, str, '\n');
- cout << str << endl;
- return str;
-}
-
int main()
{
- string IP;
- unsigned int Port_number;
- Input_IP(IP, &Port_number);
- Tcp_client(IP, Port_number);
+ //string IP;
+ //unsigned int Port_number;
+ //Input_IP(IP, &Port_number);
+ //Tcp_client(IP, Port_number);
+
+ int Function_code;
+ while (1)
+ {
+ 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);
+ printf("%02X", Function_code);
+ getchar();
+ }
getchar();
return 0;
}
\ No newline at end of file
From 4918cfde7c47d923001325a5e9b12ddff8db4f8b Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Sun, 30 Aug 2020 13:52:24 +0800
Subject: [PATCH 05/16] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BE=93=E5=85=A5?=
=?UTF-8?q?=E6=93=8D=E4=BD=9C=E6=95=B0=E9=87=8F=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Modbus_communication/Modbus_TCP/common.cpp | 66 +++++++++++++++++++++-
Modbus_communication/Modbus_TCP/main.cpp | 28 +++++----
2 files changed, 83 insertions(+), 11 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp
index a4fdd9e..aaabf5a 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -83,7 +83,7 @@ void Input_IP(string& IP, unsigned int *Port_number)
* 功能 : 选择功能码
* 描述 : 在功能码 0x01 0x03 0x0F 0x10中选择一个功能码
* 输入 : 无
-* 输出 : 选择的功能码
+* 输出 : Function_code 选择的功能码
*********************************************************************************************/
int Input_Function_code(void)
{
@@ -107,3 +107,67 @@ int Input_Function_code(void)
} 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;
+ printf("请输入操作(十进制)起始地址 \n");
+ 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");
+ 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;
+}
\ No newline at end of file
diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp
index bd2a329..b25b3c6 100644
--- a/Modbus_communication/Modbus_TCP/main.cpp
+++ b/Modbus_communication/Modbus_TCP/main.cpp
@@ -6,28 +6,36 @@ int main()
//unsigned int Port_number;
//Input_IP(IP, &Port_number);
//Tcp_client(IP, Port_number);
-
- int Function_code;
+ int Function_code = 0x01;
+ unsigned int Operations_Number;
while (1)
{
- cin >> hex;
int i = 1;
- printf(" 0x01 0x03 0x0F 0x10 \n");
- printf("˵ Ȧ Ĵ дȦ дĴ\n");
+ bool flage = true;
+ printf(" 0x01 0x03 0x0F 0x10\n");
+ printf(" 1-2000 1-125 1-1968 1-123\n");
do
{
if (i == 1)
{
i = 0;
- cout << "ѡĹ룺";
+ cout << "(ʮ)";
}
else
- cout << "빦ѡĹ";
- cin >> Function_code;
+ cout << "(ʮ)";
+ cin >> Operations_Number;
cin.clear();
cin.sync();
- } while (Function_code != 0x01 && Function_code != 0x03 && Function_code != 0x0F && Function_code != 0x10);
- printf("%02X", Function_code);
+ 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);
+ printf("%02X", Operations_Number);
getchar();
}
getchar();
From 42f28420661821a191d260d5365aa24769242134 Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Sun, 30 Aug 2020 17:46:23 +0800
Subject: [PATCH 06/16] =?UTF-8?q?=E5=AE=8C=E6=88=90Write=5FData=E6=A8=A1?=
=?UTF-8?q?=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Modbus_communication/Modbus_TCP/common.cpp | 78 ++++++++++++++++++++++
Modbus_communication/Modbus_TCP/main.cpp | 70 ++++++++++++++-----
2 files changed, 131 insertions(+), 17 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp
index aaabf5a..9056633 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -170,4 +170,82 @@ unsigned int Input_Operations_Number(int Function_code)
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;
+ }
+ 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 写入的数据
+*********************************************************************************************/
+string Write_date(int Function_code, unsigned int Operations_Number)
+{
+ int i = 1;
+ string Write_date;
+ unsigned int Write_date_number;
+ if (Function_code == 0x01 || Function_code == 0x03)
+ return Write_date;//读线圈不需要输入写入数据
+ 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;
+ 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;
}
\ No newline at end of file
diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp
index b25b3c6..72a9431 100644
--- a/Modbus_communication/Modbus_TCP/main.cpp
+++ b/Modbus_communication/Modbus_TCP/main.cpp
@@ -1,4 +1,38 @@
#include "main.h"
+//#include
+
+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;
+ }
+ 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;
+}
int main()
{
@@ -6,35 +40,37 @@ int main()
//unsigned int Port_number;
//Input_IP(IP, &Port_number);
//Tcp_client(IP, Port_number);
- int Function_code = 0x01;
- unsigned int Operations_Number;
+ int Function_code = 0x0F;
+ unsigned int Operations_Number = 30;
+ unsigned int Write_date_number ;
while (1)
{
+ if (Function_code == 0x01 || Function_code == 0x03)
+ return 0;//ȦҪı
int i = 1;
- bool flage = true;
- printf(" 0x01 0x03 0x0F 0x10\n");
- printf(" 1-2000 1-125 1-1968 1-123\n");
+ string Write_date;
+ 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;
+ printf("Nдݣ2λ16Ϊһ飬ո磺00 00 01 00 20 05\n");
do
{
if (i == 1)
{
i = 0;
- cout << "(ʮ)";
+ printf("%dд ", Write_date_number);
}
else
- cout << "(ʮ)";
- cin >> Operations_Number;
+ printf("%dд ",Write_date_number);
+ getline(cin, Write_date);
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);
+ } while (!Check_Write_date(Write_date, Write_date_number));
printf("%02X", Operations_Number);
getchar();
}
From cdf5978b0e04ae0d8786e8745674e55782396f4c Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Mon, 31 Aug 2020 08:34:24 +0800
Subject: [PATCH 07/16] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=9F=E6=88=90MBAP?=
=?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=9C=AA=E5=AE=8C=E5=96=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Modbus_communication/Modbus_TCP/common.cpp | 22 ++++++
Modbus_communication/Modbus_TCP/main.cpp | 83 ++++------------------
2 files changed, 36 insertions(+), 69 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp
index 9056633..5928588 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -211,6 +211,7 @@ bool Check_Write_date(string Write_date, int Write_date_number)
return true;
}
+
/*********************************************************************************************
* 功能 : 输入写入数据
* 描述 : 根据写入数量和功能码类型输入相应的数据
@@ -248,4 +249,25 @@ string Write_date(int Function_code, unsigned int Operations_Number)
} while (!Check_Write_date(Write_date, Write_date_number));
return Write_date;
+}
+
+/*********************************************************************************************
+* 功能 : 生成MBAP报头
+* 描述 : MBAP报文头的包括的内容:
+* +-------------+---------+--------+--------+------------------------------+
+* | 域 | 长度 | 客户机 | 服务器 | 描述 |
+* +-------------+---------+--------+--------+------------------------------+
+* |事务元标识符 | 2个字节 | 启动 | 复制 |请求/响应事务处理的识别码 |
+* +-------------+---------+--------+--------+------------------------------+
+* |协议标识符 | 2个字节 | 启动 | 复制 |0=MODBUS 协议 |
+* +-------------+---------+--------+--------+------------------------------+
+* | 长度 | 2个字节 | 启动 | 启动 |以下字节的数量 |
+* +-------------+---------+--------+--------+------------------------------+
+* |单元标识符 | 1个字节 | 启动 | 复制 |连接的远程从站的识别码 |
+* 输入 : 无
+* 输出 : MBAP报文头内容
+*********************************************************************************************/
+void Crate_MBAP()
+{
+
}
\ No newline at end of file
diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp
index 72a9431..b63786d 100644
--- a/Modbus_communication/Modbus_TCP/main.cpp
+++ b/Modbus_communication/Modbus_TCP/main.cpp
@@ -1,79 +1,24 @@
#include "main.h"
//#include
-
-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;
- }
- 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;
-}
+#define DEVICE_ID 0x10
int main()
{
- //string IP;
- //unsigned int Port_number;
- //Input_IP(IP, &Port_number);
- //Tcp_client(IP, Port_number);
- int Function_code = 0x0F;
- unsigned int Operations_Number = 30;
- unsigned int Write_date_number ;
- while (1)
+ int Operations_Number = 4;
+ UINT8 Message[260];
+ Message[0] = 0x00;
+ Message[1] = 0x00;
+ Message[2] = 0x00;
+ Message[3] = 0x00;
+
+ Message[4] = 0x00;
+ Message[5] = Operations_Number;
+ Message[6] = DEVICE_ID;
+ for (int i = 0; i < 7; i++)
{
- if (Function_code == 0x01 || Function_code == 0x03)
- return 0;//ȦҪı
- int i = 1;
- string Write_date;
- 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;
- 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));
- printf("%02X", Operations_Number);
- getchar();
+ printf("%02X", Message[i]);
}
+
getchar();
return 0;
}
\ No newline at end of file
From 2cd2fae78fd35d734d1c8214ffb6355963ae12fa Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Mon, 31 Aug 2020 10:34:26 +0800
Subject: [PATCH 08/16] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=86=99=E5=85=A5?=
=?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=B7=BB=E5=8A=A0?=
=?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=86=99=E5=85=A5=E6=95=B0=E6=8D=AE=E5=AD=97?=
=?UTF-8?q?=E8=8A=82=E6=95=B0=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Modbus_communication/Modbus_TCP/common.cpp | 34 +++++++++++++++-------
1 file changed, 24 insertions(+), 10 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp
index 9056633..5b4fa3e 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -171,6 +171,7 @@ unsigned int Input_Operations_Number(int Function_code)
} while (flage);
return Operations_Number;
}
+
/*********************************************************************************************
* 功能 : 检测写入数据
* 描述 : 检测写入数据格式是否正确
@@ -212,18 +213,14 @@ bool Check_Write_date(string Write_date, int Write_date_number)
}
/*********************************************************************************************
-* 功能 : 输入写入数据
-* 描述 : 根据写入数量和功能码类型输入相应的数据
-* 输入 : Function_code 选择的功能码类型 Operations_Number对应 功能码类型的操作数量
-* 输出 : Write_date 写入的数据
+* 功能 : 计算写入数据的字节数
+* 描述 : 通过对应的功能码和操作数量计算对应的数据字节数
+* 输入 : Function_code 选择的功能码类型 Operations_Number对应功能码类型的操作数量
+* 输出 : Write_date_number 写入数据的字节数
*********************************************************************************************/
-string Write_date(int Function_code, unsigned int Operations_Number)
+unsigned int Count_Write_date_number(int Function_code, unsigned int Operations_Number)
{
- int i = 1;
- string Write_date;
- unsigned int Write_date_number;
- if (Function_code == 0x01 || Function_code == 0x03)
- return Write_date;//读线圈不需要输入写入数据
+ unsigned int Write_date_number = 0;
if (Function_code == 0x0F) //读线圈
{
Write_date_number = Operations_Number / 8;
@@ -232,6 +229,23 @@ string Write_date(int Function_code, unsigned int Operations_Number)
}
if (Function_code == 0x10)
Write_date_number = Operations_Number * 2;
+ return Write_date_number;
+}
+
+/*********************************************************************************************
+* 功能 : 输入写入数据
+* 描述 : 根据写入数量和功能码类型输入相应的数据
+* 输入 : Function_code 选择的功能码类型 Operations_Number对应功能码类型的操作数量
+* 输出 : Write_date 写入的数据
+*********************************************************************************************/
+string 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
{
From 44058e02ecf29afbfe72f7425e7fa5045c2e4d49 Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Mon, 31 Aug 2020 10:55:00 +0800
Subject: [PATCH 09/16] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=8A=A5=E6=96=87MBAP?=
=?UTF-8?q?=E7=94=9F=E6=88=90=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Modbus_communication/Modbus_TCP/common.cpp | 10 ++++++++--
Modbus_communication/Modbus_TCP/common.h | 3 +++
Modbus_communication/Modbus_TCP/main.cpp | 9 +++++++--
3 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp
index 4750310..68ad345 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -281,7 +281,13 @@ string Write_date(int Function_code, unsigned int Operations_Number)
* 输入 : 无
* 输出 : MBAP报文头内容
*********************************************************************************************/
-void Crate_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;
}
\ No newline at end of file
diff --git a/Modbus_communication/Modbus_TCP/common.h b/Modbus_communication/Modbus_TCP/common.h
index 8d7c125..7f9b972 100644
--- a/Modbus_communication/Modbus_TCP/common.h
+++ b/Modbus_communication/Modbus_TCP/common.h
@@ -10,6 +10,9 @@
#pragma comment(lib,"ws2_32.lib")
using namespace std;
+#define DEVICE_ID 0x10 //豸ID
+
+
bool Check_IP(char* IP);
bool InitSocket_Version(void);
diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp
index b63786d..828c3f5 100644
--- a/Modbus_communication/Modbus_TCP/main.cpp
+++ b/Modbus_communication/Modbus_TCP/main.cpp
@@ -1,6 +1,10 @@
#include "main.h"
//#include
#define DEVICE_ID 0x10
+void test(UINT8 *Message)
+{
+ Message[7] = 0x10;
+}
int main()
{
@@ -14,9 +18,10 @@ int main()
Message[4] = 0x00;
Message[5] = Operations_Number;
Message[6] = DEVICE_ID;
- for (int i = 0; i < 7; i++)
+ test(Message);
+ for (int i = 0; i < 8; i++)
{
- printf("%02X", Message[i]);
+ printf("%02X ", Message[i]);
}
getchar();
From d4c77b908f075d4ea81414a5b1312e10993ff5c9 Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Mon, 31 Aug 2020 13:49:00 +0800
Subject: [PATCH 10/16] =?UTF-8?q?=E5=AE=8C=E6=88=90TCP=5FMessage=E7=94=9F?=
=?UTF-8?q?=E6=88=90=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Modbus_communication/Modbus_TCP/common.cpp | 42 +++++++++++++++++++++-
Modbus_communication/Modbus_TCP/main.cpp | 24 +++----------
2 files changed, 46 insertions(+), 20 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp
index 68ad345..55c16d9 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -290,4 +290,44 @@ void Crate_MBAP(UINT8 *Message, int Function_code, unsigned int Operations_Numbe
Message[4] = 0x00;
Message[5] = Count_Write_date_number(Function_code, Operations_Number) + 0x06;
Message[6] = DEVICE_ID;
-}
\ No newline at end of file
+}
+
+/*********************************************************************************************
+* 功能 : 字符串转UINT8类型
+* 描述 : 根据写入数量和功能码类型输入相应的数据
+* 输入 : *Message 消息帧存放的数组 Write_date 要转换的字符串
+* Message_len消息帧数组的起始位置
+* 输出 : Message_len 消息帧的长度
+*********************************************************************************************/
+int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len)
+{
+ const char *b = Write_date.c_str();
+ for (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);
+
+}
diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp
index 828c3f5..0dba8b0 100644
--- a/Modbus_communication/Modbus_TCP/main.cpp
+++ b/Modbus_communication/Modbus_TCP/main.cpp
@@ -1,28 +1,14 @@
#include "main.h"
-//#include
+
#define DEVICE_ID 0x10
-void test(UINT8 *Message)
-{
- Message[7] = 0x10;
-}
int main()
{
- int Operations_Number = 4;
UINT8 Message[260];
- Message[0] = 0x00;
- Message[1] = 0x00;
- Message[2] = 0x00;
- Message[3] = 0x00;
-
- Message[4] = 0x00;
- Message[5] = Operations_Number;
- Message[6] = DEVICE_ID;
- test(Message);
- for (int i = 0; i < 8; i++)
- {
- printf("%02X ", Message[i]);
- }
+ int Function_code = 0x10;
+ unsigned int Operations_Number = 5;
+ unsigned int Starting_address = 13;
+ string a = "1F 00 01 02 03 04 05 06 07 06";
getchar();
return 0;
From 78ee81c1ad8ecc08972df37f3a1556f94777ccdc Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Mon, 31 Aug 2020 20:09:05 +0800
Subject: [PATCH 11/16] =?UTF-8?q?=E6=95=B4=E5=90=88=E5=8A=9F=E8=83=BD?=
=?UTF-8?q?=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Modbus_TCP/TCP_client.cpp | 212 ++++++++++++++++--
Modbus_communication/Modbus_TCP/TCP_client.h | 8 +-
Modbus_communication/Modbus_TCP/common.cpp | 86 +------
Modbus_communication/Modbus_TCP/common.h | 13 +-
Modbus_communication/Modbus_TCP/main.cpp | 12 +-
5 files changed, 223 insertions(+), 108 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp
index 2c11a15..d518220 100644
--- a/Modbus_communication/Modbus_TCP/TCP_client.cpp
+++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp
@@ -1,5 +1,92 @@
-#include "TCP_client.h"
+#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)
@@ -11,31 +98,124 @@ SOCKET Init_client(string IP, unsigned int Port_number)
Serversock_in.sin_port = htons(Port_number);
while (SOCKET_ERROR == connect(ClientSocket, (SOCKADDR*)&Serversock_in, sizeof(SOCKADDR)))
{
- cout << "TCPվʧ" << endl;
+ cout << "尝试连接TCP从站失败" << endl;
}
- cout << "TCPվɹ" << endl;
+ cout << "连接TCP从站成功" << endl;
+ TIMEVAL timeout;
+ timeout.tv_sec = 2000; //ms
+ timeout.tv_usec = 0; //us
+ setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间
return ClientSocket;
}
-int Tcp_client(string IP, unsigned int Port_number)
+/*********************************************************************************************
+* 功能 : 清除接收缓冲区中的数据
+* 描述 : 每一次发送请求前清空缓存区数据
+* 输入 : ClientSocket 客户端套接字
+* 输出 : 无
+*********************************************************************************************/
+void Clear_recv_buf(SOCKET clientSocket)
{
- SOCKET clientSocket = Init_client(IP, Port_number);
- if (INVALID_SOCKET == clientSocket)
+ struct timeval tmOut;
+ tmOut.tv_sec = 0;
+ tmOut.tv_usec = 0;
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(clientSocket, &fds);
+ int nRet;
+ char tmp[2];
+ memset(tmp, 0, sizeof(tmp));
+ while (1)
{
- cout << "ʼվʧ" << endl;
+ nRet = select(FD_SETSIZE, &fds, NULL, NULL, &tmOut);
+ if (nRet == 0)
+ break;
+ recv(clientSocket, tmp, 1, 0);
+ }
+}
+
+void Printf_Coil_date(UINT8 *Response_Message)
+{
+
+}
+
+void Printf_Register_date(UINT8 *Response_Message)
+{
+
+}
+void Printf_Anomaly_date(UINT8 *Response_Message)
+{
+
+}
+
+k
+
+
+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)//先处理异常响应
+ {
+
+ return true;
+ }
+ else if (Response_Message[6] == Request_Message[6] && Response_Message_len - 5 == Response_Message[5]) //判断是否是正常响应帧
+ {
+
+ 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)
{
- char receiveBuf[260];
- memset(receiveBuf, 0, 100);
- recv(clientSocket, receiveBuf, 100, 0);
- printf("%s\n", receiveBuf);
- send(clientSocket, "hello,this is client", strlen("hello,this is client") + 1, 0);
+
+ 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 Message_len = Crate_TCP_Message(Request_Message, Function_code, Operations_Number, Starting_address, Write_date);
+ printf("主站请求 :");
+ for (int i = 0; i < Message_len; i++)
+ {
+ printf("%02x ", Request_Message[i]);
+ }
+ printf("\n");
+ Clear_recv_buf(ClientSocket);
+ send(ClientSocket, (char*)Request_Message, Message_len, 0);
+ memset(Response_Message, 0, 260);
+ int ret = recv(ClientSocket, (char*)Response_Message, 260, 0);
+ if (ret > 0)
+ {
+ printf("从站响应 :");
+ for (int i = 0; i < ret; i++)
+ {
+ printf("%02x ", Response_Message[i]);
+ }
+ printf("\n");
+ }
+ else
+ cout << "响应超时" << endl;
}
- //ر
- closesocket(clientSocket);
- //رշ
+ //关闭套接字
+ closesocket(ClientSocket);
+ //关闭服务
WSACleanup();
- return false;
+ return true;
}
diff --git a/Modbus_communication/Modbus_TCP/TCP_client.h b/Modbus_communication/Modbus_TCP/TCP_client.h
index 8e40027..9f5b5e8 100644
--- a/Modbus_communication/Modbus_TCP/TCP_client.h
+++ b/Modbus_communication/Modbus_TCP/TCP_client.h
@@ -3,6 +3,12 @@
#include "common.h"
-int Tcp_client(string IP, unsigned int Port_number);
+
+
+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
index 55c16d9..8c5c83a 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -1,83 +1,5 @@
#include "common.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
-
-}
/*********************************************************************************************
* 功能 : 选择功能码
@@ -118,7 +40,7 @@ unsigned int Input_Starting_address(void)
{
unsigned int Starting_address;
int i = 1;
- printf("请输入操作(十进制)起始地址 \n");
+ cin >> dec;
do
{
if (i == 1)
@@ -148,6 +70,7 @@ unsigned int Input_Operations_Number(int Function_code)
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)
@@ -194,6 +117,7 @@ bool Check_Write_date(string Write_date, int Write_date_number)
{
Space_number++;
j = i;
+ continue;
}
else
return false;
@@ -239,7 +163,7 @@ unsigned int Count_Write_date_number(int Function_code, unsigned int Operations_
* 输入 : Function_code 选择的功能码类型 Operations_Number对应功能码类型的操作数量
* 输出 : Write_date 写入的数据
*********************************************************************************************/
-string Write_date(int Function_code, unsigned int Operations_Number)
+string Input_Write_date(int Function_code, unsigned int Operations_Number)
{
int i = 1;
string Write_date;
@@ -301,6 +225,8 @@ void Crate_MBAP(UINT8 *Message, int Function_code, unsigned int Operations_Numbe
*********************************************************************************************/
int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len)
{
+ if (Write_date.length() == 0)
+ return Message_len;
const char *b = Write_date.c_str();
for (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 7f9b972..5799a60 100644
--- a/Modbus_communication/Modbus_TCP/common.h
+++ b/Modbus_communication/Modbus_TCP/common.h
@@ -14,8 +14,15 @@ using namespace std;
-bool Check_IP(char* IP);
-bool InitSocket_Version(void);
-void Input_IP(string& IP, unsigned int *Port_number);
+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);
+
#endif
\ No newline at end of file
diff --git a/Modbus_communication/Modbus_TCP/main.cpp b/Modbus_communication/Modbus_TCP/main.cpp
index 0dba8b0..c417351 100644
--- a/Modbus_communication/Modbus_TCP/main.cpp
+++ b/Modbus_communication/Modbus_TCP/main.cpp
@@ -1,15 +1,11 @@
#include "main.h"
-#define DEVICE_ID 0x10
-
int main()
{
- UINT8 Message[260];
- int Function_code = 0x10;
- unsigned int Operations_Number = 5;
- unsigned int Starting_address = 13;
- string a = "1F 00 01 02 03 04 05 06 07 06";
-
+ string IP;
+ unsigned int Port;
+ Input_IP(IP,&Port);
+ Tcp_client(IP,Port);
getchar();
return 0;
}
\ No newline at end of file
From 740d52c58564e0ae7463c3dfe5bd518fc1dfe0aa Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Tue, 1 Sep 2020 14:33:27 +0800
Subject: [PATCH 12/16] =?UTF-8?q?=E6=95=B4=E5=90=88=E5=8A=9F=E8=83=BD?=
=?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?=
=?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=93=8D=E5=BA=94?=
=?UTF-8?q?=E6=8A=A5=E6=96=87=E8=A7=A3=E6=9E=90=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Modbus_TCP/Modbus_TCP.vcxproj | 4 +-
.../Modbus_TCP/Modbus_TCP_log.txt | 2 +
.../Modbus_TCP/TCP_client.cpp | 62 +++++++++++++++----
Modbus_communication/Modbus_TCP/common.cpp | 44 ++++++++++++-
Modbus_communication/Modbus_TCP/common.h | 4 +-
5 files changed, 102 insertions(+), 14 deletions(-)
create mode 100644 Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt
diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj
index d78837d..6d25ad6 100644
--- a/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj
+++ b/Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj
@@ -43,7 +43,9 @@
Level3
Disabled
- true
+ false
+ false
+ _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)
true
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..2cd8041
--- /dev/null
+++ b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt
@@ -0,0 +1,2 @@
+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
diff --git a/Modbus_communication/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp
index d518220..94a8ead 100644
--- a/Modbus_communication/Modbus_TCP/TCP_client.cpp
+++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp
@@ -101,10 +101,10 @@ SOCKET Init_client(string IP, unsigned int Port_number)
cout << "尝试连接TCP从站失败" << endl;
}
cout << "连接TCP从站成功" << endl;
- TIMEVAL timeout;
- timeout.tv_sec = 2000; //ms
- timeout.tv_usec = 0; //us
- setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间
+ //TIMEVAL timeout;
+ //timeout.tv_sec = 2000; //ms
+ //timeout.tv_usec = 0; //us
+ //setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间
return ClientSocket;
}
@@ -134,33 +134,69 @@ void Clear_recv_buf(SOCKET clientSocket)
}
}
-void Printf_Coil_date(UINT8 *Response_Message)
+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)
+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!!!!!");
+ }
}
-k
+
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;
@@ -197,18 +233,22 @@ bool Tcp_client(string IP, unsigned int Port_number)
printf("%02x ", Request_Message[i]);
}
printf("\n");
+ Log_Note(Request_Message, 1, Message_len);
Clear_recv_buf(ClientSocket);
send(ClientSocket, (char*)Request_Message, Message_len, 0);
memset(Response_Message, 0, 260);
int ret = recv(ClientSocket, (char*)Response_Message, 260, 0);
if (ret > 0)
{
+ Log_Note(Response_Message, 0, ret);
printf("从站响应 :");
for (int i = 0; i < ret; i++)
{
printf("%02x ", Response_Message[i]);
}
printf("\n");
+ if (Analysis_Response_Message(Response_Message, Request_Message, ret))
+ printf("响应报文异常\n\n");
}
else
cout << "响应超时" << endl;
diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp
index 8c5c83a..8a20b62 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -228,7 +228,7 @@ int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len)
if (Write_date.length() == 0)
return Message_len;
const char *b = Write_date.c_str();
- for (int i = 0; i + 3 < Write_date.length(); i = i + 3)
+ for (unsigned int i = 0; i + 3 < Write_date.length(); i = i + 3)
{
sscanf_s(b + i, "%02X", (int *)(Message + Message_len));
Message_len++;
@@ -257,3 +257,45 @@ int Crate_TCP_Message(UINT8 *Message, int Function_code, unsigned int Operations
return HexStringtoByte(Message, Write_date, 12);
}
+
+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
index 5799a60..b3c237b 100644
--- a/Modbus_communication/Modbus_TCP/common.h
+++ b/Modbus_communication/Modbus_TCP/common.h
@@ -7,10 +7,11 @@
#include
#include
#include
+#include
#pragma comment(lib,"ws2_32.lib")
using namespace std;
-#define DEVICE_ID 0x10 //豸ID
+#define DEVICE_ID 0x09 //豸ID
@@ -23,6 +24,7 @@ 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
From 98db3bc5b20ea62cb1d75bb56e8e8d5fc1948e4e Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Tue, 1 Sep 2020 14:49:45 +0800
Subject: [PATCH 13/16] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86?=
=?UTF-8?q?=E5=8F=98=E9=87=8F=E5=90=8D=E7=A7=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Modbus_communication/Modbus_TCP/TCP_client.cpp | 18 +++++++++---------
Modbus_communication/Modbus_TCP/common.cpp | 8 ++++++++
2 files changed, 17 insertions(+), 9 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp
index 94a8ead..2a0a9a2 100644
--- a/Modbus_communication/Modbus_TCP/TCP_client.cpp
+++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp
@@ -226,28 +226,28 @@ bool Tcp_client(string IP, unsigned int Port_number)
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 Message_len = Crate_TCP_Message(Request_Message, Function_code, Operations_Number, Starting_address, Write_date);
+ int Request_Message_len = Crate_TCP_Message(Request_Message, Function_code, Operations_Number, Starting_address, Write_date);
printf("主站请求 :");
- for (int i = 0; i < Message_len; i++)
+ for (int i = 0; i < Request_Message_len; i++)
{
printf("%02x ", Request_Message[i]);
}
printf("\n");
- Log_Note(Request_Message, 1, Message_len);
+ Log_Note(Request_Message, 1, Request_Message_len);
Clear_recv_buf(ClientSocket);
- send(ClientSocket, (char*)Request_Message, Message_len, 0);
+ send(ClientSocket, (char*)Request_Message, Request_Message_len, 0);
memset(Response_Message, 0, 260);
- int ret = recv(ClientSocket, (char*)Response_Message, 260, 0);
- if (ret > 0)
+ int Response_Message_len = recv(ClientSocket, (char*)Response_Message, 260, 0);
+ if (Response_Message_len > 0)
{
- Log_Note(Response_Message, 0, ret);
+ Log_Note(Response_Message, 0, Response_Message_len);
printf("从站响应 :");
- for (int i = 0; i < ret; i++)
+ for (int i = 0; i < Response_Message_len; i++)
{
printf("%02x ", Response_Message[i]);
}
printf("\n");
- if (Analysis_Response_Message(Response_Message, Request_Message, ret))
+ if (Analysis_Response_Message(Response_Message, Request_Message, Response_Message_len))
printf("响应报文异常\n\n");
}
else
diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp
index 8a20b62..d6b7668 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -258,6 +258,14 @@ int Crate_TCP_Message(UINT8 *Message, int Function_code, unsigned int Operations
}
+/*********************************************************************************************
+* 功能 : 日志记录
+* 描述 : 记录每次通信的请求和响应报文
+* 输入 : *Message 消息帧存放的数组 flage = 1 请求报文 flage = 其他 响应报文
+* Message_len 报文数据长度
+* 输出 : 无
+*********************************************************************************************/
+
void Log_Note(UINT8 *Message, int flage, int Message_len)
{
FILE *fp = NULL;
From cee30de1d3ebe934dd4ec6d838adcdce5f4edb4e Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Tue, 1 Sep 2020 14:50:48 +0800
Subject: [PATCH 14/16] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=A5=E6=94=B6?=
=?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=94=9F=E6=95=88?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Modbus_communication/Modbus_TCP/TCP_client.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp
index 2a0a9a2..a5ed0da 100644
--- a/Modbus_communication/Modbus_TCP/TCP_client.cpp
+++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp
@@ -101,10 +101,10 @@ SOCKET Init_client(string IP, unsigned int Port_number)
cout << "尝试连接TCP从站失败" << endl;
}
cout << "连接TCP从站成功" << endl;
- //TIMEVAL timeout;
- //timeout.tv_sec = 2000; //ms
- //timeout.tv_usec = 0; //us
- //setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间
+ TIMEVAL timeout;
+ timeout.tv_sec = 2000; //ms
+ timeout.tv_usec = 0; //us
+ setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间
return ClientSocket;
}
From a2ea0aa4dade029343671fd49d328059e3b036c0 Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Sat, 5 Sep 2020 20:09:48 +0800
Subject: [PATCH 15/16] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug=2010=200f=E6=8A=A5?=
=?UTF-8?q?=E6=96=87=E7=BC=BA=E5=B0=91=E6=95=B0=E6=8D=AE=E9=95=BF=E5=BA=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Modbus_TCP/Modbus_TCP_log.txt | 38 ++++++++++++++++++-
.../Modbus_TCP/TCP_client.cpp | 4 +-
Modbus_communication/Modbus_TCP/common.cpp | 6 ++-
Modbus_communication/Modbus_TCP/common.h | 2 +-
4 files changed, 43 insertions(+), 7 deletions(-)
diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt
index 2cd8041..9ee522e 100644
--- a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt
+++ b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt
@@ -1,2 +1,36 @@
-2020-9-1 14:23:15 Send:00 00 00 00 00 06 09 01 00 00 00 09
-2020-9-1 14:23:17 Recv:00 00 00 00 00 05 10 01 02 FF 01
+2020-9-5 19:23:6 Send:00 00 00 00 00 08 01 10 00 00 00 01 02 FF FF
+2020-9-5 19:23:30 Recv:00 00 00 00 00 06 01 10 00 00 00 01
+2020-9-5 19:24:29 Send:00 00 00 00 00 06 01 03 00 00 00 01
+2020-9-5 19:24:50 Send:00 00 00 00 00 06 01 03 00 00 00 01
+2020-9-5 19:26:22 Send:00 00 00 00 00 06 01 03 00 00 00 01
+2020-9-5 19:26:31 Recv:00 00 00 00 00 05 01 03 02 FF FF
+2020-9-5 19:27:11 Send:00 00 00 00 00 06 01 03 00 00 00 01
+2020-9-5 19:27:17 Recv:00 00 00 00 00 05 01 03 02 FF FF
+2020-9-5 19:27:38 Send:00 00 00 00 00 08 01 10 00 01 00 01 02 11 11
+2020-9-5 19:27:44 Recv:00 00 00 00 00 06 01 10 00 01 00 01
+2020-9-5 19:28:20 Send:00 00 00 00 00 06 01 03 00 01 00 01
+2020-9-5 19:28:55 Send:00 00 00 00 00 06 01 03 00 01 00 01
+2020-9-5 19:30:38 Send:00 00 00 00 00 06 01 03 00 00 00 01
+2020-9-5 19:30:38 Recv:00 00 00 00 00 05 01 03 02 FF FF
+2020-9-5 19:30:42 Send:00 00 00 00 00 06 01 03 00 01 00 01
+2020-9-5 19:30:42 Recv:00 00 00 00 00 05 01 03 02 11 FF
+2020-9-5 19:31:1 Send:00 00 00 00 00 08 01 10 00 03 00 01 02 11 11
+2020-9-5 19:31:1 Recv:00 00 00 00 00 06 01 10 00 03 00 01
+2020-9-5 19:31:8 Send:00 00 00 00 00 06 01 03 00 03 00 01
+2020-9-5 19:31:49 Send:00 00 00 00 00 06 01 03 00 03 00 01
+2020-9-5 19:32:50 Send:00 00 00 00 00 06 01 03 00 00 00 01
+2020-9-5 19:33:17 Send:00 00 00 00 00 08 01 10 00 02 00 01 02 11 11
+2020-9-5 19:38:12 Send:00 00 00 00 00 06 01 03 00 00 00 01
+2020-9-5 19:38:12 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E
+2020-9-5 19:38:24 Send:00 00 00 00 00 06 01 01 00 00 00 01
+2020-9-5 19:38:24 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E
+2020-9-5 19:38:37 Send:00 00 00 00 00 06 01 03 00 00 00 01
+2020-9-5 19:38:56 Send:00 00 00 00 00 06 01 03 00 00 00 01
+2020-9-5 19:38:56 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E
+2020-9-5 19:39:5 Send:00 00 00 00 00 06 01 03 00 00 00 01
+2020-9-5 19:39:5 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E
+2020-9-5 19:40:28 Send:00 00 00 00 00 06 01 03 00 00 00 0A
+2020-9-5 19:40:28 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E
+2020-9-5 19:40:41 Send:00 00 00 00 00 06 01 03 00 00 00 0A
+2020-9-5 19:40:52 Send:00 00 00 00 00 06 01 03 00 00 00 0A
+2020-9-5 19:40:52 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E
diff --git a/Modbus_communication/Modbus_TCP/TCP_client.cpp b/Modbus_communication/Modbus_TCP/TCP_client.cpp
index a5ed0da..304aa7f 100644
--- a/Modbus_communication/Modbus_TCP/TCP_client.cpp
+++ b/Modbus_communication/Modbus_TCP/TCP_client.cpp
@@ -120,13 +120,13 @@ void Clear_recv_buf(SOCKET clientSocket)
tmOut.tv_sec = 0;
tmOut.tv_usec = 0;
fd_set fds;
- FD_ZERO(&fds);
- FD_SET(clientSocket, &fds);
int nRet;
char tmp[2];
memset(tmp, 0, sizeof(tmp));
while (1)
{
+ FD_ZERO(&fds);
+ FD_SET(clientSocket, &fds);
nRet = select(FD_SETSIZE, &fds, NULL, NULL, &tmOut);
if (nRet == 0)
break;
diff --git a/Modbus_communication/Modbus_TCP/common.cpp b/Modbus_communication/Modbus_TCP/common.cpp
index d6b7668..30da7d4 100644
--- a/Modbus_communication/Modbus_TCP/common.cpp
+++ b/Modbus_communication/Modbus_TCP/common.cpp
@@ -223,10 +223,12 @@ void Crate_MBAP(UINT8 *Message, int Function_code, unsigned int Operations_Numbe
* Message_len消息帧数组的起始位置
* 输出 : Message_len 消息帧的长度
*********************************************************************************************/
-int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len)
+int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len, int Function_code, unsigned int Operations_Number)
{
if (Write_date.length() == 0)
return Message_len;
+ Message[Message_len] = Count_Write_date_number(Function_code, Operations_Number);
+ Message_len++;
const char *b = Write_date.c_str();
for (unsigned int i = 0; i + 3 < Write_date.length(); i = i + 3)
{
@@ -254,7 +256,7 @@ int Crate_TCP_Message(UINT8 *Message, int Function_code, unsigned int Operations
Message[10] = Operations_Number >> 8;//操作数量H
Message[11] = Operations_Number;//操作数量L
- return HexStringtoByte(Message, Write_date, 12);
+ return HexStringtoByte(Message, Write_date, 12, Function_code, Operations_Number);
}
diff --git a/Modbus_communication/Modbus_TCP/common.h b/Modbus_communication/Modbus_TCP/common.h
index b3c237b..790816b 100644
--- a/Modbus_communication/Modbus_TCP/common.h
+++ b/Modbus_communication/Modbus_TCP/common.h
@@ -11,7 +11,7 @@
#pragma comment(lib,"ws2_32.lib")
using namespace std;
-#define DEVICE_ID 0x09 //豸ID
+#define DEVICE_ID 0x01 //豸ID
From e370d242f2c83f64b38af9e9ab5e162c9a376197 Mon Sep 17 00:00:00 2001
From: zcn1123 <2363211205@qq.com>
Date: Mon, 7 Sep 2020 19:21:18 +0800
Subject: [PATCH 16/16] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=A5=E6=94=B6?=
=?UTF-8?q?=E5=BB=B6=E6=97=B6=E4=B8=BA200ms?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Modbus_TCP/Modbus_TCP_log.txt | 18 ++++++++++++++++++
Modbus_communication/Modbus_TCP/TCP_client.cpp | 2 +-
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt
index 9ee522e..038f01e 100644
--- a/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt
+++ b/Modbus_communication/Modbus_TCP/Modbus_TCP_log.txt
@@ -34,3 +34,21 @@
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
index 304aa7f..76b1e4a 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 = 2000; //ms
+ timeout.tv_sec = 200; //ms
timeout.tv_usec = 0; //us
setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间
return ClientSocket;