// 本文使用的服务器为Modbus 仿真服务器Modbus Salve
<1>.同步方法
byte[] buffer = new byte[1024];
Socket clinetSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 502);
clinetSocket.Connect(ipEnd);
// 000020-Rx:00 86 00 00 00 06 01 03 00 00 00 0A
/* 00 86 通信标识符
* 00 00 Modbus 协议
* 00 06 后面字节个数
* 01 站号
* 03 功能码
* 00 00 起始地址
* 00 0A 操作数量
*/
byte[] sendMsg = { 0x00, 0x86, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x00, 0x00, 0x0A };
clinetSocket.Send(sendMsg, sendMsg.Length, SocketFlags.None);
// System.Threading.Thread.Sleep(200);
int mlength = clinetSocket