Browse Source

添加

commom.cpp
common.h
main.cpp
main.h
TCP_client.cpp
TCP_client.h
Modbus_TCP
zcn1123 4 years ago
parent
commit
083271a049
9 changed files with 176 additions and 0 deletions
  1. +8
    -0
      Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj
  2. +22
    -0
      Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj.filters
  3. +42
    -0
      Modbus_communication/Modbus_TCP/TCP_client.cpp
  4. +8
    -0
      Modbus_communication/Modbus_TCP/TCP_client.h
  5. +52
    -0
      Modbus_communication/Modbus_TCP/common.cpp
  6. +17
    -0
      Modbus_communication/Modbus_TCP/common.h
  7. +9
    -0
      Modbus_communication/Modbus_TCP/main.cpp
  8. +6
    -0
      Modbus_communication/Modbus_TCP/main.h
  9. +12
    -0
      Modbus_communication/Modbus_communication.sln

+ 8
- 0
Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj View File

@@ -64,6 +64,14 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="common.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="TCP_client.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="common.h" />
<ClInclude Include="main.h" />
<ClInclude Include="TCP_client.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">


+ 22
- 0
Modbus_communication/Modbus_TCP/Modbus_TCP.vcxproj.filters View File

@@ -14,4 +14,26 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="common.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="TCP_client.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="common.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="TCP_client.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="main.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

+ 42
- 0
Modbus_communication/Modbus_TCP/TCP_client.cpp View File

@@ -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;
}

+ 8
- 0
Modbus_communication/Modbus_TCP/TCP_client.h View File

@@ -0,0 +1,8 @@
#ifndef __TCP_CLIENT_H
#define __TCP_CLIENT_H

#include "common.h"

int Tcp_client(void);

#endif

+ 52
- 0
Modbus_communication/Modbus_TCP/common.cpp View File

@@ -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 <string>& 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);
}
}
}

+ 17
- 0
Modbus_communication/Modbus_TCP/common.h View File

@@ -0,0 +1,17 @@
#ifndef __COMMON_H
#define __COMMON_H

#include <stdio.h>
#include <winsock2.h>
#include <WS2tcpip.h>
#include <vector>
#include <string>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;

bool InitSocket_Version(void);
bool Check_IP(char* IP);
void Input_IP(vector <string>& a);

#endif

+ 9
- 0
Modbus_communication/Modbus_TCP/main.cpp View File

@@ -0,0 +1,9 @@
#include "main.h"


int main()
{

getchar();
return 0;
}

+ 6
- 0
Modbus_communication/Modbus_TCP/main.h View File

@@ -0,0 +1,6 @@
#ifndef __MAIN_H
#define __MAIN_H

#include "TCP_client.h"

#endif

+ 12
- 0
Modbus_communication/Modbus_communication.sln View File

@@ -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


Loading…
Cancel
Save