private void SerialDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string text;

            switch (rxDataType)
            {
            case DataType.Text:
                text = port.ReadExisting();
                break;

            case DataType.Hex:
                var n    = port.BytesToRead;
                var data = new byte[n];
                port.Read(data, 0, n);
                var s = new StringBuilder();
                foreach (var b in data)
                {
                    s.Append("0123456789ABCDEF"[(b >> 4) & 0x0F])
                    .Append("0123456789ABCDEF"[b & 0x0F]).Append(' ');
                }
                text = s.ToString();
                break;

            case DataType.Escape:
                var t = port.ReadExisting();
                var r = new StringBuilder();
                foreach (var c in t)
                {
                    if (c < 0x20)
                    {
                        char ch;
                        if (Hex2TextData.TryGetValue(c, out ch))
                        {
                            r.Append('\\').Append(ch);
                        }
                        else
                        {
                            r.Append("\\x")
                            .Append("0123456789ABCDEF"[(c >> 4) & 0x0F])
                            .Append("0123456789ABCDEF"[c & 0x0F]);
                        }
                    }
                    else
                    {
                        r.Append(c);
                    }
                }

                text = r.ToString();
                break;

            default:
                port.ReadExisting();
                return;
            }

            ReceiveText.Dispatcher.Invoke(() =>
            {
                ReceiveText.AppendText(text);
                ReceiveText.ScrollToEnd();
            });
        }
Esempio n. 2
0
        private void ListenClientMsg(object cliento)
        {
            Socket client = cliento as Socket;

            try
            {
                while (true)
                {
                    byte[] result     = new byte[1024];
                    int    dataLength = client.Receive(result);
                    if (dataLength > 0)
                    {
                        byte[] MessageBytes = result.Take(dataLength).ToArray();
                        string Receivemeans = ReadMessage.Parser.readMessage(MessageBytes);
                        this.Dispatcher.Invoke(new Action(() => { ReceiveText.AppendText(Receivemeans + "\n"); }));

                        LogUtil.Logger.Info("【数据】" + ScaleConvertor.HexBytesToString(MessageBytes));
                        LogUtil.Logger.Info("【解析】" + Receivemeans);

                        #region parse
                        switch (MessageBytes[5])
                        {
                        case 0x01:    //呼唤小车
                        {
                            string name = "08 " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 02 01 01 01";

                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            //  sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)06:    //小车到达
                        {
                            string name             = "06 " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 07 01";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)03:    //小车出发
                        {
                            string name             = "06 " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 04 01";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)08:    //取消小车呼唤
                        {
                            string name             = "08 " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 09 02 02 01";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case 0x0A:    //获取货物库位信息
                        {
                            string name = "11 " + MessageBytes[3].ToString("X2") + " "
                                          + MessageBytes[4].ToString("X2") + " 0B 04 01 01 02 03 04 05 48 65 6C 6C 6F";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)12:    //入库完成指令0X0C
                        {
                            string name             = "08 " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 0D 04 01 13";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)13:    //出库指令0X0D
                        {
                            string name             = "07 " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 0F 02 01";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)14:    //出库指令0X0E
                        {
                            string name             = "07 " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 0F 02 01";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)16:    //出库完成指令0X10
                        {
                            string name             = "08 " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 11 02 01 13";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)18:    //码垛(整个托盘)完成指令0X12
                        {
                            string name             = "08 " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 13 01 01 13";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)20:    //请求启动或停止设备指令0X14
                        {
                            string name             = "09 " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 15 01 01 13 01";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)22:    //启动或停止设备指令0X16
                        {
                            string name             = "0B " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 17 01 01 13 01 02 01";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }

                        case (byte)24:    //警报指令0X19
                        {
                            string name             = "0B " + MessageBytes[3].ToString("X2") + " " + MessageBytes[4].ToString("X2") + " 19 01 01 13 01 02 01";
                            byte[] SendMessageBytes = ScaleConvertor.HexStringToHexByte(name);
                            sendMsgToClient(GetClientKey(client), SendMessageBytes);

                            break;
                        }



                        default: break;
                        }
                        #endregion
                    }
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                LogUtil.Logger.Info(ee.Message);
                RemoveClient(client);
            }
        }