diff --git a/HexcalMC/Hexcal/TcpIpServer.cs b/HexcalMC/Hexcal/TcpIpServer.cs index d52857c..066c27f 100644 --- a/HexcalMC/Hexcal/TcpIpServer.cs +++ b/HexcalMC/Hexcal/TcpIpServer.cs @@ -27,12 +27,12 @@ namespace HexcalMC.Hexcal private readonly Dictionary _dictSocket = new Dictionary(); private readonly Dictionary _dictThread = new Dictionary(); - private Socket _mWatchSocket; - - private Thread _mWatchThread; // 负责监听客户端连接请求的线程; private readonly string _strServerIp = "127.0.0.1"; //服务器的IP地址 private readonly string _strServerPort = "8080"; //端口号 + private Socket _mWatchSocket; + + private Thread _mWatchThread; // 负责监听客户端连接请求的线程; /// /// 使用模式,默认=1,接收任意数据显示;2=前两个字节为数据长度 @@ -134,7 +134,7 @@ namespace HexcalMC.Hexcal { if (_dictSocket.Values.ToArray()[i].Poll(10, SelectMode.SelectRead)) { - //DictSocket.Remove(DictSocket.Keys.ToArray()[i]); + RemoveSocketClient(_dictSocket.Keys.ToArray()[i]); } } @@ -159,6 +159,12 @@ namespace HexcalMC.Hexcal } } + public event EventHandler DataReceived; + protected virtual void OnDataReceivedByte(byte[] data) + { + // 触发事件 + DataReceived?.Invoke(this, data); + } //接收线程 private void ReceiveThread(object sokObj) { @@ -188,28 +194,39 @@ namespace HexcalMC.Hexcal try { - if (UseMode == 1) + switch (UseMode) { - if (length > 0) - { - // TcpIpDfn.SongLiang_arrMsgRec = arrMsgRec; //510 - string strData = Encoding.Default.GetString(arrMsgRec); // 将接受到的字节数据转化成字符串; - //strData = strData.Substring(0, length); - //RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData.Replace("\0", "<0>")); - RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData.Replace("\0", ".")); - } - } - else if (UseMode == 2) - { - if (length > 0 && arrMsgRec.Length > 2) - { - // TcpIpDfn.SongLiang_arrMsgRec = arrMsgRec; //510 - string strData = - Encoding.Default.GetString(arrMsgRec, 2, - arrMsgRec.Length - 2); // 将接受到的字节数据转化成字符串; + case 1: + if (length > 0) + { + string strData = Encoding.Default.GetString(arrMsgRec); // 将接受到的字节数据转化成字符串; + //strData = strData.Substring(0, length); + //RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData.Replace("\0", "<0>")); + RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData.Replace("\0", ".")); + } - RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData); - } + ; + break; + + case 2: + if (length > 0 && arrMsgRec.Length > 2) + { + string strData = + Encoding.Default.GetString(arrMsgRec, 2, + arrMsgRec.Length - 2); // 将接受到的字节数据转化成字符串; + + RaisedMessage(sokClient.RemoteEndPoint.ToString(), strData); + } + + break; + case 3: + if (length > 0) + { + + OnDataReceivedByte(arrMsgRec); + } + + break; } } catch (Exception ex) diff --git a/HexcalMC/MainFrom.cs b/HexcalMC/MainFrom.cs index 07a5d98..c0c5e01 100644 --- a/HexcalMC/MainFrom.cs +++ b/HexcalMC/MainFrom.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.IO; +using System.Text; using System.Windows.Forms; using ACS.SPiiPlusNET; using HexcalMC.Base; @@ -98,6 +99,7 @@ namespace HexcalMC { //启动服务器,并获取数据,解析 _mTcpIpServer = new TcpIpServer("100.0.0.1", Convert.ToString(1234)); + _mTcpIpServer.UseMode = 1; // try { //启动监听 @@ -106,6 +108,7 @@ namespace HexcalMC //绑定两个事件 OnRaisedStatus 和OnRaisedMessage _mTcpIpServer.OnRaisedMessage += ReceiveMessage; _mTcpIpServer.OnRaisedStatus += ReceiveStatus; + _mTcpIpServer.DataReceived += ReceiveByte; DebugDfn.AddLogText("TCP服务端启动成功 "); } else @@ -119,156 +122,164 @@ namespace HexcalMC } } + private void ReceiveByte(object sender, byte[] e) + { + DebugDfn.AddLogText("接收到" + BitConverter.ToString(e)); + } + private void ReceiveMessage(string clientIp, string msg) //接收的内容 { //打印ClientIP 和 Msg DebugDfn.AddLogText("接收到" + clientIp + ": " + msg); //根据源地址的不同,执行不同处理 - //如果 ClientIP 来自L2系统的话,执行解析 - switch (clientIp) + string sourceIp = clientIp.Split(':')[0]; + switch (sourceIp) { - case "172.19.153.80": //L2系统 + case "100.0.0.1": //L2系统 //解析处理数据 ParseHexcalPacket(msg); - break; - - case "127.0.0.1": //模拟长宽系统 - break; } } private void ParseHexcalPacket(string msg) //编写一个Hexcal协议解析函数 { - //根据不同的指令进行解析,3个层次,不合法,故障,最后是正常 - //判断Msg的长度,如果不是指定长度,直接返回 - - if (msg.Length != 8) return; + DebugDfn.AddLogText("正在解析" + msg); //判断是否含有故障ERROR字样 if (msg.Contains("ERROR")) return; - //指令解析 + //去除Msg中\r\n + msg = msg.Replace("\r\n", ""); - string[] tokens = msg.Split(','); - foreach (string token in tokens) + + if (msg.Contains("\x02") || msg.Contains("\u0002")) { - Console.WriteLine(token); + // 发送 "READY" 字符串作为响应 + byte[] response = Encoding.ASCII.GetBytes("READY"); + SendMsgToHexcal("READY"); } - //可能涉及到 将指令提取并单独处理的问题 - if (string.Equals("CMMTYP", msg)) + if (msg.Contains("\x03") || msg.Contains("\u0003")) + { + // 发送 "READY" 字符串作为响应 + byte[] response = Encoding.ASCII.GetBytes("READY"); + SendMsgToHexcal("READY"); + } + + else if (msg.Contains("CMMTYP")) { SendMsgToHexcal("CMMTYP MA 19617, FDC V15.00, 6 6 2 , 0"); } - else if (string.Equals("VERSION", msg)) //版本号 + else if (msg.Contains("VERSION")) //版本号 { SendMsgToHexcal("00-000-000-00000 FDC V51.04.0000 DATE: 12/21/22 TIME: 12:50:55"); } - else if (string.Equals("^B", msg)) //查询状态 + else if (msg.Contains("^B")) //查询状态, READY或BUSY { - //todo 启动指令 + SendMsgToHexcal("READY"); } - else if (string.Equals("SHOW MAXSTROKESW", msg)) //最大行程 + else if (msg.Contains("SHOW MAXSTROKESW")) //最大行程 { SendMsgToHexcal("MAXSTROKESW 233.200000,346.500000,15.100000,0.000000,0.000000,0.000000,0.000000"); } - else if (string.Equals("00000003", msg)) //最小行程 + else if (msg.Contains("SHOW MINSTROKESW")) //最小行程 { SendMsgToHexcal("MINSTROKESW -68.800000,-55.500000,-286.900000,0.000000,0.000000,0.000000,0.000000"); } - else if (string.Equals("SHOW MAXVEL", msg)) //最大速度 + else if (msg.Contains("SHOW MAXVEL")) //最大速度 { 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)) //最大加速度 + else if (msg.Contains("SHOW MAXACC")) //最大加速度 { 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 表示支持温度补偿 + else if (msg.Contains("SHOW TEMPCOMPTYPE")) //温度补偿,温度补偿 >1 表示支持温度补偿 { SendMsgToHexcal("TEMPCOMPTYPE 1"); } - else if (string.Equals("READTP", msg)) + else if (msg.Contains("READTP")) { SendMsgToHexcal("READTP 0.000000"); } - else if (string.Equals("SHOW SENSWKP", msg)) + else if (msg.Contains("SHOW SENSWKP")) { 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)) + else if (msg.Contains("SHOW X_SENSAXIS")) { 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)) + else if (msg.Contains("SHOW Y_SENSAXIS")) //查询Y轴行程 { 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)) + else if (msg.Contains("SHOW Z_SENSAXIS")) //查询Z轴行程 { 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)) //获取速度 + else if (msg.Contains("SHOW ESTOP")) //查询急停状态 + { + SendMsgToHexcal("ESTOP FALSE"); + } + else if (msg.Contains("CMHWST")) + { + SendMsgToHexcal("CMHWST 8257,0,1792,0"); + } + else if (msg.Contains("SHOW MOVPAR")) //查询速度 { 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)) + + else if (msg.Contains("SHOW MAXVEL")) //查询最大速度 { 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)) //获取加速 + else if (msg.Contains("SHOW ACCEL")) //查询加速度 { 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)) //设置加速度 + + + else if (msg.Contains("MOVPAR 300.0,300.0,300.0,0.0,0.0,0.0")) //设置速度 xyz 轴的速度 { 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)) + else if (msg.Contains("ACCEL")) //设置加速度 { 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)) //移动指令 + else if (msg.Contains("PRBPIN")) //设置侧头偏置 { SendMsgToHexcal("%"); } - else if (string.Equals("GETPOS", msg)) //获取位置 + else if (msg.Contains("ENABLE TEMP")) //设置温度补偿 + { + SendMsgToHexcal("%"); + } + else if (msg.Contains("WKPPAR")) + { + SendMsgToHexcal("%"); + } + else if (msg.Contains("SCLTMP")) + { + SendMsgToHexcal("%"); + } + else if (msg.Contains("MOVABS")) //移动指令 + { + SendMsgToHexcal("%"); + } + else if (msg.Contains("GETPOS")) //获取位置 { 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)) - { - } - //todo 未知指令 } private void ReceiveStatus(TcpIpServer.EnumTcpIpServer iType, string msg) @@ -280,7 +291,8 @@ namespace HexcalMC private void SendMsgToHexcal(string msg) { //发送数据 - _mTcpIpServer.SendMessageToAllClients(msg); + DebugDfn.AddLogText("回复" + msg); + _mTcpIpServer.SendMessageToAllClients(msg += "\r\n"); } #endregion hexcal软件相关 diff --git a/HexcalMC/bin/x64/Debug/HexcalMC.exe b/HexcalMC/bin/x64/Debug/HexcalMC.exe index 2b73412..630fccf 100644 Binary files a/HexcalMC/bin/x64/Debug/HexcalMC.exe and b/HexcalMC/bin/x64/Debug/HexcalMC.exe differ diff --git a/HexcalMC/bin/x64/Debug/HexcalMC.pdb b/HexcalMC/bin/x64/Debug/HexcalMC.pdb index 62432a2..d978b20 100644 Binary files a/HexcalMC/bin/x64/Debug/HexcalMC.pdb and b/HexcalMC/bin/x64/Debug/HexcalMC.pdb differ