Пример #1
0
        /// <summary>
        /// 二进制数据 转成 内容包数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public JT808_PackageData Bytes2PackageData(byte[] data)
        {
            JT808_PackageData ret = new JT808_PackageData();

            // 1. 16byte 或 12byte 消息头
            JT808_PackageData.MsgHeader msgHeader = this.ParseMsgHeaderFromBytes(data);
            ret.msgHeader = msgHeader;

            int msgBodyByteStartIndex = 12 + 1;

            // 2. 消息体
            // 有子包信息,消息体起始字节后移四个字节:消息包总数(word(16))+包序号(word(16))
            if (msgHeader.hasSubPackage)
            {
                msgBodyByteStartIndex = 16 + 1;
            }

            byte[] tmp = new byte[msgHeader.msgBodyLength];
            Buffer.BlockCopy(data, msgBodyByteStartIndex, tmp, 0, tmp.Length);
            ret.setMsgBodyBytes(tmp);

            // 3. 去掉分隔符之后,最后一位就是校验码
            int checkSumInPkg      = data[data.Length - 1 - 1];
            int calculatedCheckSum = ExplainUtils.getCheckSum4JT808(data, 0 + 1, data.Length - 1 - 1);

            ret.checkSum = (checkSumInPkg);
            if (checkSumInPkg != calculatedCheckSum)
            {
                ret.errorlog = ret.errorlog + "检验码不一致;";// string.Format("检验码不一致,msgid:{},pkg:{},calculated:{}", msgHeader.getMsgId(), checkSumInPkg, calculatedCheckSum);
            }
            return(ret);
        }
Пример #2
0
        /// <summary>
        /// 解析生成 消息头
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private JT808_PackageData.MsgHeader ParseMsgHeaderFromBytes(byte[] bytes)
        {
            JT808_PackageData.MsgHeader msgHeader = new JT808_PackageData.MsgHeader();

            // 1. 消息ID word(16)
            msgHeader.msgId = ExplainUtils.ParseIntFromBytes(bytes, 1, 2);
            // 2. 消息体属性 word(16)
            int msgBodyProps = ExplainUtils.ParseIntFromBytes(bytes, 2 + 1, 2);

            msgHeader.msgBodyPropsField = (msgBodyProps);
            // [ 0-9 ] 0000,0011,1111,1111(3FF)(消息体长度)
            msgHeader.msgBodyLength = (msgBodyProps & 0x3ff);
            // [10-12] 0001,1100,0000,0000(1C00)(加密类型)
            msgHeader.encryptionType = ((msgBodyProps & 0x1c00) >> 10);
            // [ 13_ ] 0010,0000,0000,0000(2000)(是否有子包)
            msgHeader.hasSubPackage = (((msgBodyProps & 0x2000) >> 13) == 1);
            // [14-15] 1100,0000,0000,0000(C000)(保留位)
            msgHeader.reservedBit = (((msgBodyProps & 0xc000) >> 14) + "");
            // 3. 终端手机号 bcd[6]
            msgHeader.terminalPhone = (ExplainUtils.ParseBcdStringFromBytes(bytes, 4 + 1, 6));
            // 4. 消息流水号 word(16) 按发送顺序从 0 开始循环累加
            msgHeader.flowId = ExplainUtils.ParseIntFromBytes(bytes, 10 + 1, 2);

            // 5. 消息包封装项
            // 有子包信息
            if (msgHeader.hasSubPackage)
            {
                // 消息包封装项字段
                msgHeader.packageInfoField = ExplainUtils.ParseIntFromBytes(bytes, 12 + 1, 4);

                msgHeader.totalSubPackage = ExplainUtils.ParseIntFromBytes(bytes, 12 + 1, 2);

                msgHeader.subPackageSeq = ExplainUtils.ParseIntFromBytes(bytes, 12 + 1, 2);
            }
            return(msgHeader);
        }