コード例 #1
0
 public static int ParseIntFromBytes(byte[] data, int startIndex, int length, int defaultVal)
 {
     try
     {
         // 字节数大于4,从起始索引开始向后处理4个字节,其余超出部分丢弃
         int    len = length > 4 ? 4 : length;
         byte[] tmp = new byte[len];
         Buffer.BlockCopy(data, startIndex, tmp, 0, len);
         return(ExplainUtils.byteToInteger(tmp));
     }
     catch (Exception e)
     {
         return(defaultVal);
     }
 }
コード例 #2
0
        //组装平台通用应答消息
        public static byte[] rtnServerCommonRespMsg(int msgBodyProps, string phone, int flowId, int msgId)
        {
            //7E
            //8001            消息ID
            //0005            消息体属性
            //018512345678    手机号
            //0015            消息流水号
            //0015            应答流水号
            //04              结果(00成功, 01失败, 02消息有误, 03不支持,04报警处理确认)
            //BA            鉴权码
            //7E

            MemoryStream ms         = new MemoryStream();
            List <byte>  byteSource = new List <byte>();

            // 1. 0x7e
            //ms.Write(ExplainUtils.integerTo1Bytes(pkg_delimiter), 0, 1);
            byte[] bt1 = ExplainUtils.integerTo1Bytes(pkg_delimiter);
            // 2. 消息ID word(16)
            //ms.Write(ExplainUtils.integerTo2Bytes(cmd_terminal_register_resp), 0, 2);
            byte[] bt2 = ExplainUtils.integerTo2Bytes(cmd_common_resp);
            // 3.消息体属性
            byte[] bt3 = ExplainUtils.integerTo2Bytes(msgBodyProps);
            // 4. 终端手机号 bcd[6]
            //ms.Write(ExplainUtils.string2Bcd(phone), 0, 6);
            byte[] bt4 = ExplainUtils.string2Bcd(phone);
            // 5. 消息流水号 word(16),按发送顺序从 0 开始循环累加
            //ms.Write(ExplainUtils.integerTo2Bytes(flowId), 0, 2);
            byte[] bt5 = ExplainUtils.integerTo2Bytes(flowId);
            // 6. 应答流水号
            //ms.Write(ExplainUtils.integerTo2Bytes(flowId), 0, 2);
            byte[] bt6 = ExplainUtils.integerTo2Bytes(flowId);
            // 7. 对应终端消息ID
            byte[] bt7 = ExplainUtils.integerTo2Bytes(msgId);

            // 8. 成功
            //ms.Write(ExplainUtils.integerTo1Bytes(0), 0, 1);
            byte[] bt8 = ExplainUtils.integerTo1Bytes(0);



            byteSource.AddRange(bt1);
            byteSource.AddRange(bt2);
            byteSource.AddRange(bt3);
            byteSource.AddRange(bt4);
            byteSource.AddRange(bt5);
            byteSource.AddRange(bt6);
            byteSource.AddRange(bt7);
            byteSource.AddRange(bt8);
            // 9. BA 校验码
            // 校验码
            int checkSum = ExplainUtils.getCheckSum4JT808(byteSource.ToArray(), 1, (int)(byteSource.Count));

            //ms.Write(ExplainUtils.integerTo1Bytes(checkSum), 0, 1);
            byte[] bt9 = ExplainUtils.integerTo1Bytes(checkSum);
            // 11. 0x7e
            //ms.Write(ExplainUtils.integerTo1Bytes(pkg_delimiter), 0, 1);
            byteSource.AddRange(bt9);
            byteSource.AddRange(bt1);

            // 转义
            return(ExplainUtils.DoEscape4Send(byteSource.ToArray(), 1, byteSource.ToArray().Length - 1));
        }
コード例 #3
0
        //组装终端应答消息
        public static byte[] rtnTerminalRespMsg(int msgBodyProps, string phone, int flowId)
        {
            //7E
            //8100            消息ID
            //0004            消息体属性
            //018512345678    手机号
            //0015            消息流水号
            //0015            应答流水号
            //04              结果(00成功, 01车辆已被注册, 02数据库中无该车辆, 03终端已被注册, 04数据库中无该终端)  无车辆与无终端有什么区别 ?
            //313C             鉴权码
            //7E

            MemoryStream ms         = new MemoryStream();
            List <byte>  byteSource = new List <byte>();

            // 1. 0x7e
            //ms.Write(ExplainUtils.integerTo1Bytes(pkg_delimiter), 0, 1);
            byte[] bt1 = ExplainUtils.integerTo1Bytes(pkg_delimiter);
            // 2. 消息ID word(16)
            //ms.Write(ExplainUtils.integerTo2Bytes(cmd_terminal_register_resp), 0, 2);
            byte[] bt2 = ExplainUtils.integerTo2Bytes(cmd_terminal_register_resp);
            // 3.消息体属性
            byte[] bt3 = ExplainUtils.integerTo2Bytes(msgBodyProps);
            // 4. 终端手机号 bcd[6]
            //ms.Write(ExplainUtils.string2Bcd(phone), 0, 6);
            byte[] bt4 = ExplainUtils.string2Bcd(phone);
            // 5. 消息流水号 word(16),按发送顺序从 0 开始循环累加
            //ms.Write(ExplainUtils.integerTo2Bytes(flowId), 0, 2);
            byte[] bt5 = ExplainUtils.integerTo2Bytes(flowId);
            // 6. 应答流水号
            //ms.Write(ExplainUtils.integerTo2Bytes(flowId), 0, 2);
            byte[] bt6 = ExplainUtils.integerTo2Bytes(flowId);
            // 7. 成功
            //ms.Write(ExplainUtils.integerTo1Bytes(0), 0, 1);
            byte[] bt7 = ExplainUtils.integerTo1Bytes(0);
            // 8. 鉴权码
            //ms.Write(System.Text.Encoding.GetEncoding(string_encoding).GetBytes(replyToken), 0, replyToken.Length);
            byte[] bt8 = System.Text.Encoding.GetEncoding(string_encoding).GetBytes(replyToken);


            byteSource.AddRange(bt1);
            byteSource.AddRange(bt2);
            byteSource.AddRange(bt3);
            byteSource.AddRange(bt4);
            byteSource.AddRange(bt5);
            byteSource.AddRange(bt6);
            byteSource.AddRange(bt7);
            byteSource.AddRange(bt8);
            // 9. BA 校验码
            // 校验码
            int checkSum = ExplainUtils.getCheckSum4JT808(byteSource.ToArray(), 1, (int)(byteSource.Count));

            //ms.Write(ExplainUtils.integerTo1Bytes(checkSum), 0, 1);
            byte[] bt9 = ExplainUtils.integerTo1Bytes(checkSum);
            // 11. 0x7e
            //ms.Write(ExplainUtils.integerTo1Bytes(pkg_delimiter), 0, 1);
            byteSource.AddRange(bt9);
            byteSource.AddRange(bt1);

            // 转义
            return(ExplainUtils.DoEscape4Send(byteSource.ToArray(), 1, byteSource.ToArray().Length - 1));
        }