/// <summary> /// 从数据帧提取数据 /// </summary> /// <param name="data"></param> /// <returns></returns> public static FrameStruct DeFrameData(byte[] data) { FrameStruct frameData = new FrameStruct(); frameData.validity = false; try { byte[] dat = ParseData.UnPackData(data); //去掉头尾和转义字节 //检查CRC校验码 UInt16 wCrc1 = Crc16.GetCRC16(dat, 0, dat.Length - 2); UInt16 wCrc2 = DataConvert.BytestoUInt16(dat, dat.Length - 2); wCrc2 = DataConvert.InvWord(wCrc2); if (wCrc1 == wCrc2) { int i = 0; UInt16 dataLen = DataConvert.BytestoUInt16(dat, i); dataLen = DataConvert.InvWord(dataLen); i = i + 2; frameData.srcPort = dat[i]; i++; frameData.srcLen = dat[i]; i++; frameData.srcAddr = new byte[frameData.srcLen]; Array.Copy(dat, i, frameData.srcAddr, 0, frameData.srcLen); i = i + frameData.srcLen; frameData.dstPort = dat[i]; i++; frameData.dstLen = dat[i]; i++; frameData.dstAddr = new byte[frameData.dstLen]; Array.Copy(dat, i, frameData.dstAddr, 0, frameData.dstLen); i = i + frameData.dstLen; frameData.optType = dat[i]; i++; frameData.cmd = dat[i]; i++; int iLen = dataLen - i; frameData.data = new byte[iLen]; Array.Copy(dat, i, frameData.data, 0, iLen); frameData.validity = true; } } catch (System.Exception ex) { throw ex; } return frameData; }
private void SendPackage() { //生成并发送数据包 configRecord record = GetCurRecord(); FrameStruct fsData = new FrameStruct(); fsData.srcPort = Convert.ToByte(record.SrcID, 16); if (record.SrcAddr.Length > 0) { fsData.srcAddr = DataConvert.StrToHexByte(record.SrcAddr); fsData.srcLen = (byte)fsData.srcAddr.Length; } else fsData.srcLen = 0; fsData.dstPort = Convert.ToByte(record.DstID, 16); if (record.DstAddr.Length > 0) { fsData.dstAddr = DataConvert.StrToHexByte(record.DstAddr); fsData.dstLen = (byte)fsData.dstAddr.Length; } else fsData.dstLen = 0; fsData.optType = Convert.ToByte(record.OpType, 16); fsData.cmd = Convert.ToByte(record.Cmd, 16); fsData.data = DataConvert.StrToHexByte(record.Data); //------------------------------------------------ byte[] data = ParseData.MakeFrameData(fsData); string outText = ""; if (chkCom.Checked) { //串口发送 //tcpComm.BroadcastData(ClientLevel.None, data); if (serialPort.IsOpen) { outText = string.Format("[{0} {1}] COM发送 \r\n{2}\r\n", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString(), ByteToHexStr(data)); serialPort.Write(data, 0, data.Length); } } else { //网络发送 outText = string.Format("[{0} {1}] TCP发送 \r\n{2}\r\n", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString(), ByteToHexStr(data)); if (ServerMode) { tcpServer.BroadcastData(ClientLevel.None, data); } else { tcpClient.Send(data); } } SafeOutText(outText); }
//生成数据帧 //static public byte[] MakeFrameData(byte srcPort, byte srcLen, byte[] srcAddr, // byte dstPort, byte dstLen, byte[] dstAddr, // byte optType, byte cmd, byte[] data) public static byte[] MakeFrameData(FrameStruct frameData) { byte[] dat = null; try { UInt16 dataLen = (UInt16)(8 + frameData.srcLen + frameData.dstLen + frameData.data.Length); //dataLen = DataConvert.InvWord(dataLen); int i = 0; dat = new byte[dataLen + 2]; //数据长度 + CRC长度 DataConvert.UInt16toBytes(DataConvert.InvWord(dataLen), dat, i); i = i + 2; // Source dat[i] = frameData.srcPort; i++; dat[i] = frameData.srcLen; i++; if (frameData.srcLen > 0) { Array.Copy(frameData.srcAddr, 0, dat, i, frameData.srcAddr.Length); i = i + frameData.srcLen; } // Destination dat[i] = frameData.dstPort; i++; dat[i] = frameData.dstLen; i++; if (frameData.dstLen > 0) { Array.Copy(frameData.dstAddr, 0, dat, i, frameData.dstAddr.Length); i = i + frameData.dstLen; } // Command dat[i] = frameData.optType; i++; dat[i] = frameData.cmd; i++; Array.Copy(frameData.data, 0, dat, i, frameData.data.Length); //添加CRC校验码 UInt16 wCRC16 = Crc16.GetCRC16(dat, 0, dataLen); //计算从信息长度到数据结束的CRC校验 wCRC16 = DataConvert.InvWord(wCRC16); DataConvert.UInt16toBytes(wCRC16, dat, dataLen); //添加CRC16校验 } catch (System.Exception ex) { throw ex; } byte[] outData = PackData(dat); //加头尾,转义处理 return outData; }