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