新增对hexcal协议的解析处理

This commit is contained in:
zhengxuan.zhang
2023-10-08 16:48:24 +08:00
parent 0242b65e68
commit a1e45a2ef9
+152 -14
View File
@@ -76,17 +76,24 @@ namespace HexcalMC
{
//启动服务器,并获取数据,解析
m_tcpIpServer = new TcpIpServer("100.0.0.1", Convert.ToString(1234));
//启动监听
if (m_tcpIpServer.StartListen())
try
{
//绑定两个事件 OnRaisedStatus 和OnRaisedMessage
m_tcpIpServer.OnRaisedMessage += ReceiveMessage;
m_tcpIpServer.OnRaisedStatus += ReceiveStatus;
DebugDfn.AddLogText("L2服务端启动成功 ");
//启动监听
if (m_tcpIpServer.StartListen())
{
//绑定两个事件 OnRaisedStatus 和OnRaisedMessage
m_tcpIpServer.OnRaisedMessage += ReceiveMessage;
m_tcpIpServer.OnRaisedStatus += ReceiveStatus;
DebugDfn.AddLogText("TCP服务端启动成功 ");
}
else
{
MessageBox.Show("TCP服务端启动失败,请检查网络连接,重新打开软件", "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
catch (Exception ex)
{
MessageBox.Show("L2系统服务端连接失败,请检查网络连接,重新打开软件", "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
DebugDfn.AddLogText("启动TCP服务端异常" + ex.ToString());
}
}
@@ -101,10 +108,8 @@ namespace HexcalMC
{
case "172.19.153.80": //L2系统
//解析数据,更新L2Receive
// L2Receive = L2Protocol.ParsePacket(Msg);
//todo 更新页面
//解析处理数据
ParseHexcalPacket(Msg);
break;
case "127.0.0.1": //模拟长宽系统
@@ -113,6 +118,140 @@ namespace HexcalMC
}
}
private void ParseHexcalPacket(string Msg) //编写一个Hexcal协议解析函数
{
//根据不同的指令进行解析,3个层次,不合法,故障,最后是正常
//判断Msg的长度,如果不是指定长度,直接返回
if (Msg.Length != 8) return;
//判断是否含有故障ERROR字样
if (Msg.Contains("ERROR")) return;
//指令解析
string[] tokens = Msg.Split(',');
foreach (string token in tokens)
{
Console.WriteLine(token);
}
//可能涉及到 将指令提取并单独处理的问题
if (string.Equals("CMMTYP", Msg))
{
SendMsgToHexcal("CMMTYP MA 19617, FDC V15.00, 6 6 2 , 0");
}
else if (string.Equals("VERSION", Msg)) //版本号
{
SendMsgToHexcal("00-000-000-00000 FDC V51.04.0000 DATE: 12/21/22 TIME: 12:50:55");
}
else if (string.Equals("^B", Msg)) //查询状态
{
//todo 启动指令
}
else if (string.Equals("SHOW MAXSTROKESW", Msg)) //最大行程
{
SendMsgToHexcal("MAXSTROKESW 233.200000,346.500000,15.100000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("00000003", Msg)) //最小行程
{
SendMsgToHexcal("MINSTROKESW -68.800000,-55.500000,-286.900000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("SHOW MAXVEL", Msg)) //最大速度
{
SendMsgToHexcal("MAXVEL 300.000000,300.000000,300.000000,0.000000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("SHOW MAXACC", Msg)) //最大加速度
{
SendMsgToHexcal(
"MAXACC 300.000000,300.000000,300.000000,0.000000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("SHOW TEMPCOMPTYPE", Msg)) //温度补偿,温度补偿 >1 表示支持温度补偿
{
SendMsgToHexcal("TEMPCOMPTYPE 1");
}
else if (string.Equals("READTP", Msg))
{
SendMsgToHexcal("READTP 0.000000");
}
else if (string.Equals("SHOW SENSWKP", Msg))
{
SendMsgToHexcal("X_ SENSWKP 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
}
else if (string.Equals("SHOW X_SENSAXIS", Msg))
{
SendMsgToHexcal("X_SENSAXIS 6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
}
else if (string.Equals("SHOW Y_SENSAXIS", Msg))
{
SendMsgToHexcal("Y_SENSAXIS 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
}
else if (string.Equals("SHOW Z_SENSAXIS", Msg))
{
SendMsgToHexcal("Z_SENSAXIS 7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
}
else if (string.Equals("SHOW MOVPAR", Msg)) //获取速度
{
SendMsgToHexcal(
"MOVPAR 300.000000,300.000000,300.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.00000 0,0.000000");
}
else if (string.Equals("MOVPAR 300.0,300.0,300.0,0.0,0.0,0.0", Msg)) //设置速度 xyz 轴的速度
{
SendMsgToHexcal("%");
}
else if (string.Equals("SHOW MAXVEL", Msg))
{
SendMsgToHexcal("MAXVEL 300.000000,300.000000,300.000000,0.000000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("SHOW ACCEL", Msg)) //获取加速
{
SendMsgToHexcal(
"ACCEL 1000.000000,1000.000000,1000.000000,0.000000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("ACCEL 1000.0,1000.0,1000.0", Msg)) //设置加速度
{
SendMsgToHexcal("%");
}
else if (string.Equals(
"PRBPIN 0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000",
Msg))
{
SendMsgToHexcal("%");
}
else if (string.Equals("ENABLE TEMP", Msg))
{
SendMsgToHexcal("");
}
else if (string.Equals("WKPPAR 20,0.0000115,20", Msg))
{
SendMsgToHexcal("");
}
else if (string.Equals("SCLTMP", Msg))
{
SendMsgToHexcal("");
}
else if (string.Equals("MOVABS 167.553848,-55.400002,-208.548203,0.000000", Msg)) //移动指令
{
SendMsgToHexcal("%");
}
else if (string.Equals("GETPOS", Msg)) //获取位置
{
SendMsgToHexcal("POS 167.553898,-55.400421,-208.548678,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("SHOW ESTOP", Msg))
{
SendMsgToHexcal("ESTOP FALSE");
}
else if (string.Equals("", Msg))
{
}
else
{
//todo 未知指令
}
}
private void ReceiveStatus(TcpIpServer.EnumTcpIpServer iType, string Msg)
{
//记录到日志
@@ -283,8 +422,7 @@ namespace HexcalMC
//时间栏
//获取当前时间,构造形如 精确到秒,例如 2023-10-08 16:01:23
rle_timer.Text = "当前时间: "+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
rle_timer.Text = "当前时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
#endregion