Пример #1
0
        private BaseCommand CreateCommand(List <byte> CommandBuffer)
        {
            byte[] cmdBytes = null;
            lock (CommandBuffer)
            {
                if (CommandBuffer.Count < 10)
                {
                    Logger.Instance().Error("CreateCommand() Error! CommandBuffer.Count<10");
                    return(null);
                }
                cmdBytes = new byte[CommandBuffer.Count];
                CommandBuffer.CopyTo(cmdBytes);
            }
            byte        msgID       = cmdBytes[1];
            byte        packageSize = cmdBytes[2];
            BaseCommand cmd         = null;

            //C9单包数据长度为6
            if (packageSize == 6)
            {
                cmd = CommandFactory.CreateCommand(msgID, true);
            }
            else
            {
                cmd = CommandFactory.CreateCommand(msgID, false);
            }
            if (cmd == null)
            {
                string msg = string.Format("CreateCommand() Error! MessageID={0}", msgID);
                Logger.Instance().Error(msg);
                return(null);
            }
            cmd.Direction = cmdBytes[0];
            cmd.Channel   = cmdBytes[2];
            //分析命令数据长度20180119改为两个字节了
            cmd.PayloadLength  = (ushort)(cmdBytes[4] << 8);
            cmd.PayloadLength += (ushort)cmdBytes[3];
            //20180119改为两个字节了
            cmd.PayloadLengthReverse  = (ushort)(cmdBytes[6] << 8);
            cmd.PayloadLengthReverse += (ushort)cmdBytes[5];

            //分离出最后4字节的Checksum
            cmd.Checksum  = (uint)(cmdBytes[cmdBytes.Length - 1] << 24);
            cmd.Checksum += (uint)(cmdBytes[cmdBytes.Length - 2] << 16);
            cmd.Checksum += (uint)(cmdBytes[cmdBytes.Length - 3] << 8);
            cmd.Checksum += (uint)(cmdBytes[cmdBytes.Length - 4]);
            //重新计算泵端传入的数据checksum,如果对应不上,记录日志,由上层应用酌情处理,但不能作超时处理
            uint checksum = CRC32.CalcCRC32Partial(cmdBytes, cmdBytes.Length - 4, CRC32.CRC32_SEED);

            checksum ^= CRC32.CRC32_SEED;
//在DUBUG状态下就不要去检查checksum
#if !DEBUG
            if ((cmd.Checksum ^ checksum) != 0)
            {
                cmd.ErrorMsg = "Checksum Error";
                string buffer2String = BytesToString(cmdBytes, 0, cmdBytes.Length);
                Logger.Instance().ErrorFormat("ProtocolEngine::CreateCommand() Checksum error, buffer={0}", buffer2String);
            }
#endif

            //将PayloadData提取出来,方便生成字段
            byte[] arrFieldsBuf = new byte[cmd.PayloadLength];
            int    index        = cmdBytes.Length - arrFieldsBuf.Length - 4;
            if (index <= 0)
            {
                string msg = string.Format("CreateCommand() Error! index<=0 cmdBytes.Length={0} arrFieldsBuf.Length={1}", cmdBytes.Length, arrFieldsBuf.Length);
                Logger.Instance().Error(msg);
                return(null);
            }
            Array.Copy(cmdBytes, index, arrFieldsBuf, 0, arrFieldsBuf.Length);
            //将字段填进命令
            cmd.SetBytes(arrFieldsBuf);
            return(cmd);
        }