/*
TCP Echo server example in winsock
Live Server on port 8888
*/
#include<stdio.h>
#include<winsock2.h>
#include "mbconfig.h"
#pragma comment(lib, "ws2_32.lib") //Winsock Library
int main(int argc, char* argv[])
{
WSADATA wsa;
SOCKET master, new_socket, client_socket[30], s;
struct sockaddr_in server, address;
int max_clients = 30, activity, addrlen, i, valread;
char* message = "ECHO Daemon v1.0 \r\n";
int sendLen = 0;
//size of our receive buffer, this is string length.
int MAXRECV = 1024;
//set of socket descriptors
fd_set readfds;
//1 extra for null character, string termination
//char* buffer;
//char* sendBuf;
//buffer = (char*)malloc((MAXRECV + 1) * sizeof(char));
//sendBuf = (char*)malloc((MAXRECV + 1) * sizeof(char));
uint8_t buffer[50];
uint8_t sendBuf[50];
for (i = 0; i < 30; i++)
{
client_socket[i] = 0;
}
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//Create a socket
if ((master = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(502);
//Bind
if (bind(master, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done");
//Listen to incoming connections
listen(master, 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
addrlen = sizeof(struct sockaddr_in);
while (TRUE)
{
//clear the socket fd set
FD_ZERO(&readfds);
//add master socket to fd set
FD_SET(master, &readfds);
//add child sockets to fd set
for (i = 0; i < max_clients; i++)
{
s = client_socket[i];
if (s > 0)
{
FD_SET(s, &readfds);
}
}
//wait for an activity on any of the sockets, timeout is NULL , so wait indefinitely
activity = select(0, &readfds, NULL, NULL, NULL);
if (activity == SOCKET_ERROR)
{
printf("select call failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
//If something happened on the master socket , then its an incoming connection
if (FD_ISSET(master, &readfds))
{
if ((new_socket = accept(master, (struct sockaddr*)&address, (int*)&addrlen)) < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
//inform user of socket number - used in send and receive commands
printf("New connection , socket fd is %d , ip is : %s , port : %d \n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
//send new connection greeting message
if (send(new_socket, message, strlen(message), 0) != strlen(message))
{
perror("send failed");
}
puts("Welcome message sent successfully");
//add new socket to array of sockets
for (i = 0; i < max_clients; i++)
{
if (client_socket[i] == 0)
{
client_socket[i] = new_socket;
printf("Adding to list of sockets at index %d \n", i);
break;
}
}
}
//else its some IO operation on some other socket :)
for (i = 0; i < max_clients; i++)
{
s = client_socket[i];
//if client presend in read sockets
if (FD_ISSET(s, &readfds))
{
//get details of the client
getpeername(s, (struct sockaddr*)&address, (int*)&addrlen);
//Check if it was for closing , and also read the incoming message
//recv does not place a null terminator at the end of the string (whilst printf %s assumes there is one).
valread = recv(s, buffer, MAXRECV, 0);
if (valread == SOCKET_ERROR)
{
int error_code = WSAGetLastError();
if (error_code == WSAECONNRESET)
{
//Somebody disconnected , get his details and print
printf("Host disconnected unexpectedly , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
//Close the socket and mark as 0 in list for reuse
closesocket(s);
client_socket[i] = 0;
}
else
{
printf("recv failed with error code : %d", error_code);
}
}
if (valread == 0)
{
//Somebody disconnected , get his details and print
printf("Host disconnected , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
//Close the socket and mark as 0 in list for reuse
closesocket(s);
client_socket[i] = 0;
}
//Echo back the message that came in
else
{
//add null character, if you want to use with printf/puts or other string handling functions
buffer[valread] = '\0';
/*printf("%s:%d - %s \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port), buffer);
send(s, buffer, valread, 0);*/
sendLen = ParsingClientAccessCommand(buffer, sendBuf);
send(s, sendBuf, sendLen, 0);
printf("接收到的数据长度:%d ,发送的数据长度:%d\n", valread, sendLen);
}
}
}
}
closesocket(s);
WSACleanup();
return 0;
}
Modbus TCP客户端和服务器示例
4星 · 超过85%的资源 需积分: 0 177 浏览量
更新于2020-09-12
14
收藏 14.98MB RAR 举报
**Modbus TCP客户端和服务器示例**
在工业自动化和物联网(IoT)领域,Modbus是一种广泛使用的通信协议,它允许设备之间进行简单的数据交换。本文将深入探讨Modbus TCP,这是一种在网络环境中运行的Modbus变体,尤其适用于TCP/IP网络。我们将重点介绍如何使用开源的Modbus协议栈在Visual Studio 2019环境下实现Modbus TCP客户端和服务器的示例。
了解Modbus TCP的基础知识是至关重要的。Modbus TCP是Modbus协议的网络版本,它将经典的串行通信模式转换为TCP/IP协议,使得远程设备可以透过以太网或互联网进行通信。Modbus协议基于主从架构,其中一个是主设备(通常是控制器),负责发起请求,而其他设备作为从设备响应这些请求。
在实现Modbus TCP客户端时,主要任务是构造并发送Modbus请求,然后解析返回的响应。请求通常包括功能码、寄存器地址和数据长度等信息。客户端可以读取或写入从设备的寄存器,如输入寄存器、保持寄存器和离散输入。VS2019中,你可以使用C#或C++等编程语言,并利用开源的Modbus库来简化这个过程。
对于Modbus TCP服务器,它的核心职责是接收客户端的请求,执行相应的操作,并返回结果。服务器需要维护一个内部状态机来处理不同类型的Modbus请求,并确保与多个客户端的并发通信。在VS2019中,创建服务器端程序时,你需要设置监听特定端口,接收连接,解析接收到的Modbus报文,并构建响应报文。
在压缩包中的"TCPApplication"可能包含了实现客户端和服务器的源代码。这个项目可能分为两部分:一部分是实现Modbus TCP客户端的类,另一部分是实现Modbus TCP服务器的类。客户端类可能包含连接到服务器、发送请求和解析响应的方法;服务器类则可能包括启动监听、接收连接、处理请求和发送响应的函数。
为了确保代码的正确性,开发者需要对Modbus协议有深入理解,包括各种功能码的含义、数据类型的映射以及异常处理。此外,进行充足的测试是非常必要的,确保客户端和服务器之间的通信准确无误,尤其是在面对不同设备、不同数据类型的交互时。
总结一下,实现Modbus TCP客户端和服务器示例需要掌握以下几点:
1. 理解Modbus协议和Modbus TCP的基本原理。
2. 使用VS2019进行C#或C++编程,利用开源的Modbus库。
3. 客户端如何构造和发送请求,以及解析响应。
4. 服务器如何监听连接,处理请求并构建响应。
5. 进行详尽的测试,确保通信的可靠性和兼容性。
通过实践这些示例,开发者不仅可以加深对Modbus TCP的理解,还能提升在工业自动化和物联网领域的应用开发能力。