/// <summary> /// 解析命令,返回处理后的命令列表 /// </summary> /// <param name="cmdData">被处理命令的buffer</param> /// <param name="safeCode">需要转义的转义码</param> /// <param name="cmdList">处理后的命令列表</param> private static void ParseCmd(ref CmdBuffer cmdData, byte safeCode, ref MemQueue <CmdTag> cmdList, string socketID) { CmdBuffer temp = new CmdBuffer(); byte sigByte = 0x00; UInt16 cmd_length = 0; sigByte = (byte)(safeCode - 0x01); //解析命令,如果接收的数据长度小于最短命令长度,即命令接收不完整 if (cmdData.Length < CommandCommon.CMD_MIN_LENGTH) { return; } int sigByteStartIndex = 0; lock (cmdData.Buffer) { // Debug.Print("Parse:{0}", FormatHelper.ByteArrayToHexString(cmdData.Buffer, cmdData.Length)); //查找协议头 sigByteStartIndex = Array.IndexOf(cmdData.Buffer, safeCode, 0, cmdData.Length); } //没有找到协议头 if (sigByteStartIndex < 0) { cmdData.Length = 0; return; } //数据长度不够,即最后一个字节为协议标识 if ((sigByteStartIndex + 1) == cmdData.Length) { cmdData.Buffer[0] = safeCode; cmdData.Length = 1; return; } //第一字节非协议头 if (sigByteStartIndex > 0) { cmdData.Length = cmdData.Length - sigByteStartIndex; if ((cmdData.Length <= 0) || (cmdData.Length >= CmdBuffer.BUFFER_MAX_LENGTH)) { cmdData.Length = 0; return; } Array.Copy(cmdData.Buffer, sigByteStartIndex, cmdData.Buffer, 0, cmdData.Length); } //查找协议尾 int sigByteEndIndex = 0; lock (cmdData.Buffer) { sigByteEndIndex = Array.IndexOf(cmdData.Buffer, safeCode, 1, cmdData.Length - 1); } //没有找到协议尾 if (sigByteEndIndex < 0) { return; } //包头包尾粘连 if (sigByteEndIndex == 1) { cmdData.Length -= 1; if (cmdData.Length <= 0) { cmdData.Length = 0; return; } if (cmdData.Length == 1) { cmdData.Buffer[0] = safeCode; cmdData.Length = 1; return; } Array.Copy(cmdData.Buffer, 1, cmdData.Buffer, 0, cmdData.Length); sigByteEndIndex = Array.IndexOf(cmdData.Buffer, safeCode, 1, cmdData.Length - 1); } //长度小于最短命令长度 if ((sigByteEndIndex + 1) < CommandCommon.CMD_MIN_LENGTH) { cmdData.Length = cmdData.Length - sigByteEndIndex - 1; if (cmdData.Length > 0) { Array.Copy(cmdData.Buffer, sigByteEndIndex + 1, cmdData.Buffer, 0, cmdData.Length); } else { cmdData.Length = 0; } return; } //当前命令超长 if ((sigByteEndIndex + 1) >= CommandCommon.CMD_MAX_LENGTH) { cmdData.Length = cmdData.Length - sigByteEndIndex - 1; if (cmdData.Length >= CmdBuffer.BUFFER_MAX_LENGTH) { cmdData.Length = 0; return; } if (cmdData.Length > 0) { Array.Copy(cmdData.Buffer, sigByteEndIndex + 1, cmdData.Buffer, 0, cmdData.Length); //递归调用 if (cmdData.Length >= CommandCommon.CMD_MIN_LENGTH) { ParseCmd(ref cmdData, safeCode, ref cmdList, socketID); } return; } if (cmdData.Length < 0) { cmdData.Length = 0; } return; } //拷贝命令数据 byte[] cmd_bytearray = new byte[sigByteEndIndex + 1]; Array.Copy(cmdData.Buffer, 0, cmd_bytearray, 0, cmd_bytearray.Length); cmdData.Length = cmdData.Length - cmd_bytearray.Length; if (cmdData.Length >= CmdBuffer.BUFFER_MAX_LENGTH) { cmdData.Length = 0; return; } //安全容错 if (cmdData.Length > 0) { Array.Copy(cmdData.Buffer, sigByteEndIndex + 1, cmdData.Buffer, 0, cmdData.Length); } if (cmdData.Length < 0) { cmdData.Length = 0; } //执行解析 //1:处理转义字符 byte[] parsed_bytearray; //调用时,除去头尾 UInt16 crc1 = ConvertSafeCodeToCmdSigByte(cmd_bytearray, safeCode, out parsed_bytearray); UInt16 crc2 = 0x00; // Debug.Print("Cmd:{0}", Utils.FormatHelper.ByteArrayToHexString( parsed_bytearray )); crc2 = parsed_bytearray[parsed_bytearray.Length - 3]; crc2 = (UInt16)((crc2 << 8) | parsed_bytearray[parsed_bytearray.Length - 2]); //2:校验crc if (crc1 != crc2) { //Debug.Print("CRC非法,计算值:0x{0:X4} 指令值:{1:X4}", crc1, crc2); //递归调用 if (cmdData.Length >= CommandCommon.CMD_MIN_LENGTH) { ParseCmd(ref cmdData, safeCode, ref cmdList, socketID); } return; } //长度 cmd_length = parsed_bytearray[2]; cmd_length = (UInt16)((cmd_length << 8) | parsed_bytearray[1]); //指令长度非法 if ((cmd_length < CommandCommon.CMD_MIN_LENGTH) || (cmd_length > CommandCommon.CMD_MAX_LENGTH)) { //Debug.Print("指令长度与协议长度不匹配,指令长度:{0} 数据:{1}", cmd_length, parsed_bytearray.Length); //递归调用 if (cmdData.Length >= CommandCommon.CMD_MIN_LENGTH) { ParseCmd(ref cmdData, safeCode, ref cmdList, socketID); } return; } //3:开始解析成命令 CmdTag cmd = new CmdTag(); cmd = cmd.Parse(parsed_bytearray); cmd.SocketID = socketID; cmdList.Add(cmd); //加入至命令组 ////递归调用 //if (cmdData.Length >= Common.CMD_MIN_LENGTH) //{ // ParseCmd(ref cmdData, safeCode, ref cmdList,socketID); //} }
/// <summary> /// 解析平台间的通信协议 /// </summary> /// <param name="cmdData"></param> /// <param name="cmdList"></param> /// <param name="socketID">数据源SocketID</param> public static void ParsePlatFormCmd(ref CmdBuffer cmdData, ref MemQueue <CmdTag> cmdList, string socketID) { ParseCmd(ref cmdData, CommandCommon.CMD_PLATFORM_SAFE_CODE, ref cmdList, socketID); }
/// <summary> /// 解析设备间通信协议 /// </summary> /// <param name="cmdData"></param> /// <param name="cmdList"></param> public static void ParseDeviceCmd(ref CmdBuffer cmdData, ref MemQueue <CmdTag> cmdList, string socketID) { ParseCmd(ref cmdData, CommandCommon.CMD_DEVICE_SAFE_CODE, ref cmdList, socketID); }
public SocketBuffer() { LastDataReceived = DateTime.Now; Buffer = new CmdBuffer(); Socket = new IOCPSocket(); }