public void DecodeInternal() { while (RunDecodeThread) { if (Decode) { List <Original> list = RxQueue.PopAll(); if (list != null && list.Count > 0) { foreach (var o in list) { OriginalBytes obytes = o as OriginalBytes; if (o != null) { LaserProtocol lp = laserProtocol.DePackage(obytes.Data); byte[] data = lp.Body; byte markHead = data[0]; byte type = GetMsgType(); byte[] appData = new byte[data.Length - 2]; Array.Copy(data, 1, appData, 0, data.Length - 2); LaserBasePackage bp = new LaserBasePackage(markHead, type, appData); List <LaserBaseResponse> responseList = Decoder.Decode(bp, obytes); if (responseList != null && responseList.Count > 0) { RxMsgQueue.Push(responseList); } LogHelper.GetLogger <LaserProtocolFactory>().Error(string.Format("接受到的原始数据为: {0}", ByteHelper.Byte2ReadalbeXstring(obytes.Data))); } } } } Thread.Sleep(10); } }
public byte[] EnPackage(LaserBasePackage bp) { if (bp.AppData == null) { LogHelper.GetLogger <LaserProtocol>().Error("通信层待编码数据为空,丢弃。"); return(null); } byte[] enData = new byte[bp.AppData.Length + 2]; enData[0] = bp.MarkHead; Array.Copy(bp.AppData, 0, enData, 1, bp.AppData.Length); byte oddCheck = 0x00; for (int i = 1; i < enData.Length - 2; i++) { oddCheck ^= enData[i]; } enData[enData.Length - 1] = oddCheck; return(enData); }
public virtual List <LaserBaseResponse> Decode(LaserBasePackage bp, OriginalBytes obytes) { if (obytes.Data.Length != 6) { LogHelper.GetLogger <LaserBaseResponse>().Error(string.Format("消息类型为 : {0} 长度不足!", obytes.Data[1])); return(null); } byte oddCheck = obytes.Data[1]; for (int i = 2; i < obytes.Data.Length - 2; i++) { oddCheck ^= obytes.Data[i]; } if (oddCheck != obytes.Data[obytes.Data.Length - 2]) { LogHelper.GetLogger <LaserBaseResponse>().Error(string.Format("消息类型为 : {0} 的奇偶校验位错误!", obytes.Data[1])); return(null); } else { return(Decode(bp, obytes)); } }