Пример #1
0
        /// <summary>
        /// 将接收到的命令转换成原始命令
        /// </summary>
        /// <param name="cmd">需要转换的命令</param>
        /// <param name="safeCode">转义码</param>
        /// <param name="sourceCmd">转换后的命令</param>
        /// <returns>校验CRC</returns>
        private static UInt16 ConvertSafeCodeToCmdSigByte(byte[] cmd, byte safeCode, out byte[] sourceCmd)
        {
            UInt16 crc = 0;

            byte[] _mycmd = new byte[cmd.Length];
            int    length = 1, i = 1;
            byte   sigByte = 0;

            sigByte = (byte)(safeCode - 1);

            _mycmd[0] = cmd[0];

            while (i < (cmd.Length - 1))
            {
                _mycmd[length] = cmd[i];

                if (cmd[i] == sigByte)
                {
                    if (cmd[i + 1] == 0x01)
                    {
                        // length -= 1;
                        _mycmd[length] = safeCode;
                        length        += 1;
                        i = i + 2;
                        continue;
                    }
                    if (cmd[i + 1] == 0x02)
                    {
                        //length -= 1;
                        // _mycmd[length] = (byte)(safeCode + 0x01);
                        length += 1;
                        i       = i + 2;
                        continue;
                    }
                }

                i++;
                length += 1;
            }

            length   += 1;
            sourceCmd = new byte[length];
            Array.Copy(_mycmd, 0, sourceCmd, 0, length);
            sourceCmd[length - 1] = cmd[i];

            crc = FormatHelper.CRC16(sourceCmd, 0, sourceCmd.Length - 3);

            return(crc);
        }
Пример #2
0
        /// <summary>
        /// 生成命令
        /// </summary>
        /// <param name="cmdTag">指令结构体</param>
        /// <param name="safeCode">安全码,即命令头尾</param>
        /// <returns>相关命令</returns>
        private static byte[] BuildCmd(CmdTag cmdTag, byte safeCode)
        {
            byte[] cmd      = new byte[CommandCommon.CMD_MIN_LENGTH + cmdTag.Param.Length];
            int    cmdIndex = 0;
            int    i        = 0;
            byte   sigByte  = 0x00;

            sigByte = (byte)(safeCode - 0x01);

            //开始组合
            //1.起始标识位
            cmd[0]    = safeCode;
            cmdIndex += 1;

            //2.长度
            cmd[cmdIndex] = (byte)(cmd.Length & 0x00FF);
            cmdIndex     += 1;
            cmd[cmdIndex] = (byte)((cmd.Length & 0xFF00) >> 8);
            cmdIndex     += 1;

            //生成命令类
            cmd[cmdIndex] = (byte)(cmdTag.CmdType & 0x00FF);
            cmdIndex     += 1;

            //生成命字
            cmd[cmdIndex] = (byte)(cmdTag.CmdCode & 0x00FF);
            cmdIndex     += 1;

            //状态
            cmd[cmdIndex] = cmdTag.CmdState;
            cmdIndex     += 1;

            //设备ID
            Array.Copy(cmdTag.DeviceInfo.Serial, 0, cmd, cmdIndex, cmdTag.DeviceInfo.Serial.Length);
            cmdIndex += cmdTag.DeviceInfo.Serial.Length;

            //数据区
            Array.Copy(cmdTag.Param, 0, cmd, cmdIndex, cmdTag.Param.Length);
            //cmdIndex += cmdTag.Param.Length;

            //计算校验码
            UInt16 crc = FormatHelper.CRC16(cmd, 0, cmd.Length - 3);

            //添加校验码
            cmd[cmd.Length - 3] = (byte)((crc & 0xFF00) >> 8);
            // cmdIndex += 1;
            cmd[cmd.Length - 2] = (byte)(crc & 0x00FF);
            // cmdIndex += 1;
            cmd[cmd.Length - 1] = safeCode;
            // cmdIndex += 1;

            //处理转义字
            byte[] temp = new byte[cmd.Length * 2];
            i        = 1;
            cmdIndex = 1;
            temp[0]  = cmd[0];
            while (i < (cmd.Length - 1))
            {
                temp[cmdIndex] = cmd[i];

                if (temp[cmdIndex] == sigByte)
                {
                    // temp[cmdIndex] = safeCode;
                    cmdIndex      += 1;
                    temp[cmdIndex] = 0x02;
                }

                if (temp[cmdIndex] == safeCode)
                {
                    temp[cmdIndex] = sigByte;
                    cmdIndex      += 1;
                    temp[cmdIndex] = 0x01;
                }

                cmdIndex += 1;
                i++;
            }

            //添加标识尾
            temp[cmdIndex] = safeCode;
            cmdIndex      += 1;

            byte[] ret = new byte[cmdIndex];
            Array.Copy(temp, ret, cmdIndex);

            return(ret);
        }