以下为简单通讯测试源码,只提供参考
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("127.0.0.1", 4545);//端口默认4545
{
//建立通讯
var command = "00200001000000000000" + (char)0;
byte[] commandbyte = Encoding.Default.GetBytes(command);
tcpClient.Client.Send(commandbyte, SocketFlags.None);
byte[] byteCommand = new byte[1024];
int icommand= tcpClient.Client.Receive(byteCommand);
var outputCommandValue = System.Text.Encoding.ASCII.GetString(byteCommand,0,icommand);
Console.WriteLine(outputCommandValue);
}
{
//发送心跳
var heart = "00209999000000000000" + (char)0;
byte[] heartData = Encoding.Default.GetBytes(heart);
tcpClient.Client.Send(heartData);
byte[] byteheart = new byte[1024];
var iheartdata=tcpClient.Client.Receive(byteheart);
string outputHeartValue = System.Text.Encoding.ASCII.GetString(byteheart,0,iheartdata);
Console.WriteLine(outputHeartValue);
}
{
//发送订阅拧紧数据
var tightening = "00200060000000000000" + (char)0;
byte[] tighteningbyte = Encoding.Default.GetBytes(tightening);
tcpClient.Client.Send(tighteningbyte);
byte[] bytesTightening = new byte[1024];
var itightening=tcpClient.Client.Receive(bytesTightening);
var outputTighteningValue = System.Text.Encoding.ASCII.GetString(bytesTightening,0,itightening);
Console.WriteLine(outputTighteningValue);
}
tcp 建立建立连接后,需要发送开始通讯指令(和S7等私有协议一样,连接后有两次通讯握手,拧紧控制器只有一次),拧紧控制器未收到通讯指令15秒后会强制断开连接。
开始通讯指令代码
00200001000000000000+(char)0;
拧紧控制器确认收到开始指令后回回复允许通讯指令(回复指令为测试指令,每把控制器可能略有差异,主要构成部分,由消息长度0000 指令代码0000 以及后续信息为主,以下回复报文不准确只作为解读参考)
如下:
0057代表报文长度,0002代表功能码
如上《开始通讯报文指令》组成结构一致 0020 0001,拼接时一定要注意长度是否满足规范,否则将无法通讯,马头控制器通讯报文结尾需要NUL 为标示符
拧紧控制器允许通讯指令
00570002001 010000020003
以下为完整带重连机制拧紧枪扭矩角度获取方式
string communication = "00200001000000000000\0"; byte[] communicationbyte = Encoding.ASCII.GetBytes(communication); string heart = "00209999000000000000\0"; byte[] heartbyte = Encoding.ASCII.GetBytes(heart); string sub60 = "00200060000000000000\0"; byte[] sub60byte = Encoding.ASCII.GetBytes(sub60); string sub61 = "00200062000000000000\0"; byte[] sub62byte = Encoding.ASCII.GetBytes(sub61); var tcpClient = new TcpClient(); Task.Run(async delegate { try { IAsyncResult async2 = tcpClient.Client.BeginConnect(ip, port, null, null); if (!async2.AsyncWaitHandle.WaitOne(1500)) { tcpClient.Client.EndConnect(async2); Console.WriteLine("连接失败"); } else { tcpClient.Client.EndConnect(async2); IAsyncResult sendsync3 = tcpClient.Client.BeginSend(communicationbyte, 0, communicationbyte.Length, SocketFlags.None, null, null); tcpClient.Client.EndSend(sendsync3); await Task.Delay(1000); IAsyncResult sendsyncsub = tcpClient.Client.BeginSend(sub60byte, 0, sub60byte.Length, SocketFlags.None, null, null); tcpClient.Client.EndSend(sendsyncsub); } } catch (Exception ex) { LogHelper.Log("拧紧连接", ex.Message.ToString()); } }); Task.Run(async delegate { while (true) { if (tcpClient != null && tcpClient.Connected) try { LogHelper.Log("拧紧连接", tcpClient.Connected.ToString()); byte[] result = new byte[1024]; IAsyncResult resultasync = tcpClient.Client.BeginReceive(result, 0, result.Length, SocketFlags.None, delegate{}, null); int releg = tcpClient.Client.EndReceive(resultasync); string output = Encoding.Default.GetString(result, 0, releg); Console.WriteLine("扭矩角度MID0061报文:" + output); } catch (Exception ex) { LogHelper.Log("拧紧连接", ex.Message.ToString()); } else await Task.Delay(3000); } }); Task.Run(async delegate { while (true) { try { await Task.Delay(5000); IAsyncResult sendsync = tcpClient.Client.BeginSend(heartbyte, 0, heartbyte.Length, SocketFlags.None, null, null); tcpClient.Client.EndSend(sendsync); } catch (Exception) { if (tcpClient?.Connected ?? false) tcpClient?.Client?.Shutdown(SocketShutdown.Both); try { tcpClient = new TcpClient(); IAsyncResult async = tcpClient.Client.BeginConnect(ip, port, null, null); if (!async.AsyncWaitHandle.WaitOne(1500)) { tcpClient.Client.EndConnect(async); Console.WriteLine("连接失败"); } else { tcpClient.Client.EndConnect(async); IAsyncResult sendsync2 = tcpClient.Client.BeginSend(communicationbyte, 0, communicationbyte.Length, SocketFlags.None, null, null); tcpClient.Client.EndSend(sendsync2); Console.WriteLine("连接 communicationbyte"); await Task.Delay(1000); IAsyncResult sendsyncsub60byte = tcpClient.Client.BeginSend(sub60byte, 0, sub60byte.Length, SocketFlags.None, null, null); tcpClient.Client.EndSend(sendsyncsub60byte); Console.WriteLine("连接 sendsyncsub60byte"); } } catch (Exception ex) { LogHelper.Log("拧紧连接", ex.Message.ToString()); } } } });