CSDN下载地址:https://download.csdn.net/download/sns1991sns/88041637
gitee下载地址:https://gitee.com/linsns/snssharp
一、串口通信
首先用虚拟串口助手创建一对虚拟串口号
1、打开服务端,使用SnsSerialPort
IfaceConnector snsConnectorServer;
snsConnectorServer = new SnsSerialPort(comServer, baudrateServer);
snsConnectorServer.ConnectEventHandler += SnsConnectorServer_ConnectEventHandler;
snsConnectorServer.ReceiveEventHandler += SnsConnectorServer_ReceiveEventHandler;
bool isOpen = snsConnectorServer.Open();
其中,comServer=“COM2”,baudrateServer="9600",
SnsConnectorServer_ConnectEventHandler是服务端连接状态的响应函数,
SnsConnectorServer_ReceiveEventHandler是服务端数据接收的响应函数。
2、打开客户端,使用SnsSerialPort
IfaceConnector snsConnectorClient
snsConnectorClient = new SnsSerialPort(comClient, baudrateClient);
snsConnectorClient.ConnectEventHandler += SnsConnectorClient_ConnectEventHandler;
snsConnectorClient.ReceiveEventHandler += SnsConnectorClient_ReceiveEventHandler;
bool isOpen = snsConnectorClient.Open();
其中,comClient=“COM3”,baudrateClient="9600",
SnsConnectorClient_ConnectEventHandler是客户端连接状态的响应函数,
SnsConnectorClient_ReceiveEventHandler是客户端数据接收的响应函数。
二、网络TCP通信
1、打开服务端,使用SnsSocketTcpServer
IfaceConnector snsConnectorServer;
snsConnectorServer = new SnsSocketTcpServer(ip, port);
snsConnectorServer.ConnectEventHandler += SnsConnectorServer_ConnectEventHandler;
snsConnectorServer.ReceiveEventHandler += SnsConnectorServer_ReceiveEventHandler;
bool isOpen = snsConnectorServer.Open();
其中,ip是电脑网卡当前的ip地址,port="4000",
SnsConnectorServer_ConnectEventHandler是服务端连接状态的响应函数,
SnsConnectorServer_ReceiveEventHandler是服务端数据接收的响应函数。
2、打开客户端,使用SnsSocketTcpClient
IfaceConnector snsConnectorClient
snsConnectorClient = new SnsSocketTcpClient(ip, port);
snsConnectorClient.ConnectEventHandler += SnsConnectorClient_ConnectEventHandler;
snsConnectorClient.ReceiveEventHandler += SnsConnectorClient_ReceiveEventHandler;
bool isOpen = snsConnectorClient.Open();
其中,ip是服务端的ip地址,port="4000",也就是客户端所设置的ip和port必须和服务端一致,
SnsConnectorClient_ConnectEventHandler是客户端连接状态的响应函数,
SnsConnectorClient_ReceiveEventHandler是客户端数据接收的响应函数。
三、网络UDP通信
1、打开服务端,使用SnsSocketUdpServer
IfaceConnector snsConnectorServer;
snsConnectorServer = new SnsSocketUdpServer(ip, port);
snsConnectorServer.ConnectEventHandler += SnsConnectorServer_ConnectEventHandler;
snsConnectorServer.ReceiveEventHandler += SnsConnectorServer_ReceiveEventHandler;
bool isOpen = snsConnectorServer.Open();
其中,ip是电脑网卡当前的ip地址,port="4000",
SnsConnectorServer_ConnectEventHandler是服务端连接状态的响应函数,
SnsConnectorServer_ReceiveEventHandler是服务端数据接收的响应函数。
2、打开客户端,使用SnsSocketUdpClient
IfaceConnector snsConnectorClient
snsConnectorClient = new SnsSocketUdpClient(ip, port);
snsConnectorClient.ConnectEventHandler += SnsConnectorClient_ConnectEventHandler;
snsConnectorClient.ReceiveEventHandler += SnsConnectorClient_ReceiveEventHandler;
bool isOpen = snsConnectorClient.Open();
其中,ip是服务端的ip地址,port="4000",也就是客户端所设置的ip和port必须和服务端一致,
SnsConnectorClient_ConnectEventHandler是客户端连接状态的响应函数,
SnsConnectorClient_ReceiveEventHandler是客户端数据接收的响应函数。
由于Udp本身是不需要创建连接的,也就是无法识别到服务端与客户端是否连接成功,因此采用了心跳的模式来识别是否连接,3秒内未收到心跳信号则认为处于断开状态,因此识别精度是3秒。
效果:
技术交流
QQ群: 577276243