public byte[] PackData(MsgId msgId, string terminalId, byte[] data) { int pos = 0; int data_len = data.Length; byte[] arr = new byte[15 + data_len]; arr[0] = 0x7E; arr[1] = Convert.ToByte((((int)msgId) & 0xFF00) >> 8); //消息ID,低字节 arr[2] = Convert.ToByte(((int)msgId) & 0x00FF); //消息ID高字节 // 消息体长度 arr[3] = Convert.ToByte((data_len & 0xFF00) >> 8); arr[4] = Convert.ToByte(data_len & 0x00FF); //终端号码 terminalId.PadLeft(12, '0'); arr[5] = Convert.ToByte(terminalId.Substring(0 * 2, 2), 16); arr[6] = Convert.ToByte(terminalId.Substring(1 * 2, 2), 16); arr[7] = Convert.ToByte(terminalId.Substring(2 * 2, 2), 16); arr[8] = Convert.ToByte(terminalId.Substring(3 * 2, 2), 16); arr[9] = Convert.ToByte(terminalId.Substring(4 * 2, 2), 16); arr[10] = Convert.ToByte(terminalId.Substring(5 * 2, 2), 16); // 消息流水号 Int16 serialNo = ProtocolUtil.GetSerialNo(); arr[11] = Convert.ToByte((serialNo & 0xFF00) >> 8); //流水号,低字节 arr[12] = Convert.ToByte(serialNo & 0x00FF); //流水号高字节 //数据体填充 Array.Copy(data, 0, arr, 13, data_len); pos += 12 + data_len; // 校验码 arr[++pos] = ProtocolUtil.XorByByte(arr, 1, 12 + data_len); //标识位置 arr[++pos] = 0x7E; return(ProtocolUtil.Transfer(arr)); }
/// <summary> /// 组装U盘参数 /// </summary> /// <param name="result"></param> /// <param name="cmd"></param> /// <param name="data"></param> public void PackData(ref byte[] result, byte[] cmd, byte[] data) { byte[] encryptData = AesUtil.Encrypt(data, ProtocolConstant.USB_DOG_PASSWORD); int len = encryptData.Length; result = new byte[len + 6]; result[0] = 0xAA; //1.帧首 result[1] = cmd[0]; //2.命令 string hex = len.ToString("x4"); //int转成16进制字符串 result[2] = Convert.ToByte(hex.Substring(0, 2), 16); //16进制字符串(数字化)转字节 3.长度 result[3] = Convert.ToByte(hex.Substring(2, 2), 16); for (int i = 0; i < encryptData.Length; i++) { result[4 + i] = encryptData[i]; } //4.异或校检 byte xor = ProtocolUtil.XorByByte(result, 1, 3 + len); result[result.Length - 2] = xor; //5.协议尾 result[result.Length - 1] = 0xCC; }
public byte[] Parser(byte[] source) { byte[] response = new byte[0]; byte[] buffer = ProtocolUtil.UnTransfer(source); MsgId msgId = ProtocolUtil.BytesToMsgId(buffer, 0); Int16 data_len = BitConverter.ToInt16(buffer, 2); data_len = IPAddress.NetworkToHostOrder(data_len); string terminalId = ProtocolUtil.BcdToString(buffer, 4, 6); Int16 serialNo = BitConverter.ToInt16(buffer, 10); serialNo = IPAddress.NetworkToHostOrder(serialNo); byte xor = buffer[12 + data_len]; //数据体 byte[] data = new byte[data_len]; Array.Copy(buffer, 12, data, 0, data_len); byte calcXor = ProtocolUtil.XorByByte(buffer, 0, 12 + data_len); TcpFrameBean frameBean = new TcpFrameBean(); frameBean.DataBody = data; frameBean.SerialNo = serialNo; frameBean.TerminalId = terminalId; frameBean.MsgId = msgId; frameBean.DeviceType = (DeviceType)buffer[4]; if (xor != calcXor) { logger.Error("校验码不符合预期:" + ProtocolUtil.BytesToString(source)); frameBean.AppendErrorMsg("校验码不符合预期"); byte[] d = MakerTCPFrame.GetInstance().Make8001Frame(serialNo, MsgId.X0001, CommResponse.MistakeMsg); response = MakerTCPFrame.GetInstance().PackData(MsgId.X8001, frameBean.TerminalId, d); return(response); } switch (msgId) { case MsgId.X0001: //设备通用应答 break; case MsgId.X0002: //心跳OK logger.Info("收到心跳:" + ProtocolUtil.BytesToString(source)); WriteLogFile("收到心跳:" + ProtocolUtil.BytesToString(source)); HandleHeartBeat(ref response, frameBean); break; case MsgId.X0008: //开始训练 logger.Info("收到开始训练请求:" + ProtocolUtil.BytesToString(source)); WriteLogFile("收到开始训练请求:" + ProtocolUtil.BytesToString(source)); HandleStartPrictice(ref response, frameBean); break; case MsgId.X0009: //训练结果上报 logger.Info("收到训练结果上报:" + ProtocolUtil.BytesToString(source)); WriteLogFile("收到训练结果上报:" + ProtocolUtil.BytesToString(source)); byte[] dd = MakerTCPFrame.GetInstance().Make8001Frame(serialNo, MsgId.X0001, CommResponse.Success); response = MakerTCPFrame.GetInstance().PackData(MsgId.X8001, frameBean.TerminalId, dd); HandlePricticeResult(ref response, frameBean); break; case MsgId.X000A: //请求使用者信息 logger.Info("收到请求使用者信息:" + ProtocolUtil.BytesToString(source)); WriteLogFile("收到请求使用者信息:" + ProtocolUtil.BytesToString(source)); HandleRequestUserInfo(ref response, frameBean); break; case MsgId.X0007: //请求照片数据OK logger.Info("收到请求照片数据OK:" + ProtocolUtil.BytesToString(source)); WriteLogFile("收到请求照片数据OK:" + ProtocolUtil.BytesToString(source)); HandleRequestImageData(ref response, frameBean); break; default: logger.Error("收到未知消息:" + ProtocolUtil.BytesToString(source)); WriteLogFile("收到未知消息:" + ProtocolUtil.BytesToString(source)); frameBean.AppendErrorMsg("未知的消息ID"); byte[] d = MakerTCPFrame.GetInstance().Make8001Frame(serialNo, MsgId.X0001, CommResponse.UnSupport); response = MakerTCPFrame.GetInstance().PackData(MsgId.X8001, frameBean.TerminalId, d); break; } logger.Info("响应的报文:" + ProtocolUtil.BytesToString(response)); WriteLogFile("响应的报文:" + ProtocolUtil.BytesToString(response)); return(response); }
/// <summary> /// 接收数据的监听方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnPortDataReceived(Object sender, SerialDataReceivedEventArgs e) { try { Thread.Sleep(50); isReceive = true;//收到数据,取消重发 byte[] buffer = null;; int len = serialPort.BytesToRead; byte[] receiveData = new byte[len]; serialPort.Read(receiveData, 0, len); //Console.WriteLine("收到的数据:" + ProtocolUtil.ByteToStringOk(receiveData)); int offset = 0; Int32 datalen = 0; if (len > 0 && receiveData[0] == 0xAA)//第一包数据 { datalen = Convert.ToInt32((receiveData[2].ToString("X2") + receiveData[3].ToString("X2")), 16); //Console.WriteLine("数据的长度:" + datalen); buffer = new byte[datalen + 6]; for (int i = 0; i < receiveData.Length; i++) { buffer[i] = receiveData[i]; } offset = receiveData.Length; } else { return; } while (buffer != null && buffer[buffer.Length - 1] != 0xCC) { Thread.Sleep(50); int len2 = serialPort.BytesToRead; if (len2 <= 0) { return; } serialPort.Read(buffer, offset, len2); offset += len2; if (offset > buffer.Length) { return; } } //下面是完整数据 if (buffer != null) { byte[] data = new byte[datalen + 3]; Array.Copy(buffer, 1, data, 0, data.Length); if (buffer[buffer.Length - 2] == ProtocolUtil.XorByByte(data)) { object result = null;//用于存放uuid的鉴权加密 new ParserUSBDogFrame().Parser(ref result, buffer); string b = ProtocolUtil.BytesToString((byte[])result); //Console.WriteLine("解密通讯加密后的数据:" + b); byte[] uuidBytes = null; uuidBytes = AesUtil.Decrypt((byte[])result, ProtocolConstant.USB_DOG_AUTH_PASSWORD); //Console.WriteLine("解密鉴权加密后的数据:" + ProtocolUtil.ByteToStringOk(uuidBytes)); string strUUID = System.Text.Encoding.ASCII.GetString(uuidBytes); if (strUUID == Get_UUID()) { //MessageBoxX.Info("激活成功"); //todo 全局变量 ProtocolConstant.USB_SUCCESS = 1; MessageBoxX.Info(LanguageUtils.ConvertLanguage("激活成功", "Activated successfully")); //Console.WriteLine("激活成功"); logger.Debug("激活成功"); } else { //MessageBox.Show("激活失败"); //Console.WriteLine("激活失败"); logger.Debug("激活失败"); } } else { //Console.WriteLine("校验失败"); logger.Debug("校验失败"); } } } catch (Exception ex) { } finally { //收到消息后至空串口并关闭 SerialPortUtil.ClosePort(ref serialPort); Dispatcher.Invoke(new Action(() => { this.Close(); })); } }