예제 #1
0
        /// <summary>
        /// 解构808消息
        /// </summary>
        private void SplitMessage()
        {
            //定义消息各部分的长度
            uint msgStartTagLength = 1, msgHeadLength = 12, msgBodyLength, msgCheckCodeLength = 1, msgEndTagLength = 1;

            try
            {
                BytesConverter iBytesConverter = new BytesConverter();

                //获取起始标识位msgStartTag
                MsgStartTag = new byte[msgStartTagLength];
                Array.Copy(msgHex, 0, MsgStartTag, 0, msgStartTagLength);

                //获取消息头msgHead
                MsgHead = new byte[msgHeadLength];
                Array.Copy(msgHex, msgStartTagLength, MsgHead, 0, msgHeadLength);

                //获取消息体的长度msgBodyLength,单位bytes
                msgBodyLength = iBytesConverter.ToUShort(MsgHead, 2);
                msgBodyLength = msgBodyLength & 0x03FF;

                //获取消息体msgBody
                MsgBody = new byte[msgBodyLength];
                Array.Copy(msgHex, msgStartTagLength + msgHeadLength, MsgBody, 0, msgBodyLength);

                //获取校验码msgCheckCode
                MsgCheckCodeag = new byte[msgCheckCodeLength];
                Array.Copy(msgHex, msgStartTagLength + msgHeadLength + msgBodyLength, MsgCheckCodeag, 0, msgCheckCodeLength);

                //获取结束标识位msgEndTag
                MsgEndTag = new byte[msgEndTagLength];
                Array.Copy(msgHex, msgStartTagLength + msgHeadLength + msgBodyLength + msgCheckCodeLength, MsgEndTag, 0, msgEndTagLength);
            }
            catch (Exception e)
            {
                throw new Exception($"SplitMessage函数异常-> {e.Message}");
            }
        }
예제 #2
0
        private byte[] msgHex; //十六进制格式
        #endregion

        /// <summary>
        /// PreProcess
        /// </summary>
        /// <param name="input"></param>
        public PreProcess(string input)
        {
            try
            {
                msg = input;
                BytesConverter  iBytesConverter  = new BytesConverter();
                StringConverter iStringConverter = new StringConverter();

                //检验整条消息的合法性:
                VerifyEntireMessage();

                //将消息转换为字节数组
                msgHex = iStringConverter.ToByteArray(msg, 16);

                //还原转义
                RestoreMessage();

                //解构消息
                SplitMessage();

                //提取消息ID
                MsgId = iBytesConverter.ToUShort(MsgHead, 0);

                //打印结果
                Console.WriteLine("\n---消息解构------------------");
                Console.WriteLine("{0}:{1}", nameof(MsgStartTag), BitConverter.ToString(MsgStartTag).Replace("-", string.Empty));
                Console.WriteLine("{0}:{1}", nameof(MsgHead), BitConverter.ToString(MsgHead).Replace("-", string.Empty));
                Console.WriteLine("{0}:{1}", nameof(MsgBody), BitConverter.ToString(MsgBody).Replace("-", string.Empty));
                Console.WriteLine("{0}:{1}", nameof(MsgCheckCodeag), BitConverter.ToString(MsgCheckCodeag).Replace("-", string.Empty));
                Console.WriteLine("{0}:{1}", nameof(MsgEndTag), BitConverter.ToString(MsgEndTag).Replace("-", string.Empty));
            }
            catch (Exception e)
            {
                throw new Exception($"异常类:{this.GetType().FullName}\n消息内容:{input}\n失败原因:{e.Message}");
            }
        }
        public void Main(byte[] input)
        {
            try
            {
                //协议相关变量
                ushort replySeq;        //应答流水号,详见JTT808-2013第8.6章节
                byte   result;          //结果,详见JTT808-2013第8.6章节
                string resultExplain;   //结果的描述
                string authKey;         //鉴权码,详见JTT808-2013第8.6章节

                //临时变量
                BytesConverter iBytesConverter = new BytesConverter();
                int            startIndex      = 0;
                int            length;

                //所有字符串都使用GBK编码规则
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                Encoding gbk = Encoding.GetEncoding("GBK");

                //解析"应答流水号"
                replySeq   = iBytesConverter.ToUShort(input, startIndex);
                length     = iBytesConverter.returnLength;
                startIndex = startIndex + length;

                //解析"结果"
                result        = iBytesConverter.ToByte(input, startIndex);
                resultExplain = result.ToString("D");
                length        = iBytesConverter.returnLength;
                startIndex    = startIndex + length;
                switch (result)
                {
                case 0:
                    resultExplain = resultExplain + " 成功";
                    break;

                case 1:
                    resultExplain = resultExplain + " 车辆已被注册";
                    break;

                case 2:
                    resultExplain = resultExplain + " 数据库中无该车辆";
                    break;

                case 3:
                    resultExplain = resultExplain + " 终端已被注册";
                    break;

                case 4:
                    resultExplain = resultExplain + " 数据库中无该终端";
                    break;

                default:
                    resultExplain = "!!!>>>数值错误<<<!!!";
                    break;
                }

                //解析"鉴权码"
                length  = input.Length - startIndex;
                authKey = gbk.GetString(input, startIndex, length);

                //打印
                ConsoleColorPrint iPrint = new ConsoleColorPrint();
                iPrint.TripleInOneLine("---消息体名称:", ConsoleColor.Gray, "终端注册应答", ConsoleColor.Green, "---", ConsoleColor.Gray);
                iPrint.DoubleInOneLine("应答流水号:", ConsoleColor.Green, replySeq.ToString("D"), ConsoleColor.White);
                iPrint.DoubleInOneLine("结果:", ConsoleColor.Green, resultExplain, ConsoleColor.White);
                iPrint.DoubleInOneLine("鉴权码:", ConsoleColor.Green, authKey, ConsoleColor.White);
            }
            catch (Exception e)
            {
                throw new Exception($"{this.GetType().FullName}.{MethodBase.GetCurrentMethod().Name}异常:{e.Message}");
            }
        }
        public void Main(byte[] input)
        {
            //协议相关变量
            ushort provinceId;          //省域ID,详见JTT808-2013第8.5章节
            ushort cityId;              //市县域ID,详见JTT808-2013第8.5章节
            string manufId;             //制造商ID,详见JTT808-2013第8.5章节
            string terminalModel;       //终端型号,详见JTT808-2013第8.5章节
            string terminalId;          //终端ID,详见JTT808-2013第8.5章节
            byte   carPlateColor;       //车牌颜色,详见JTT808-2013第8.5章节
            string carId;               //车辆标识,详见JTT808-2013第8.5章节
            string carPlateNumber = ""; //车辆牌照,详见JTT808-2013第8.5章节
            string carVin         = ""; //车辆VIN,详见JTT808-2013第8.5章节

            //临时变量
            BytesConverter iBytesConverter = new BytesConverter();
            int            startIndex      = 0;
            int            length;

            //所有字符串都使用GBK编码规则
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            Encoding gbk = Encoding.GetEncoding("GBK");

            try
            {
                //提取"省域ID"
                length     = 2;
                provinceId = iBytesConverter.ToUShort(input, startIndex);
                length     = iBytesConverter.returnLength;
                startIndex = startIndex + length;

                //提取"市县域ID"
                cityId     = iBytesConverter.ToUShort(input, startIndex);
                length     = iBytesConverter.returnLength;
                startIndex = startIndex + length;

                //提取"制造商ID"
                length     = 5;
                manufId    = gbk.GetString(input, startIndex, length);
                startIndex = startIndex + length;

                //提取"终端型号"
                length        = 20;
                terminalModel = gbk.GetString(iBytesConverter.TrimEndZero(input, startIndex, length));
                startIndex    = startIndex + length;

                //提取"终端ID"
                length     = 7;
                terminalId = gbk.GetString(iBytesConverter.TrimEndZero(input, startIndex, length));
                startIndex = startIndex + length;

                //提取"车牌颜色"
                carPlateColor = iBytesConverter.ToByte(input, startIndex);
                length        = iBytesConverter.returnLength;
                startIndex    = startIndex + length;

                //提取"车辆标识"
                //车牌颜色为0时,车辆标识表示车辆VIN
                //车牌颜色不为0时,车辆标识表示公安交通管理部门颁发的机动车号牌
                length = input.Length - startIndex;
                if (carPlateColor == 0)
                {
                    carVin = gbk.GetString(input, startIndex, length);
                }
                else
                {
                    carPlateNumber = gbk.GetString(input, startIndex, length);
                }

                #region 打印
                ConsoleColorPrint iPrint = new ConsoleColorPrint();
                iPrint.TripleInOneLine("---消息体名称:", ConsoleColor.Gray, "终端注册", ConsoleColor.Green, "---", ConsoleColor.Gray);
                //省域ID
                iPrint.DoubleInOneLine("省域ID:", ConsoleColor.Green, provinceId.ToString("D"), ConsoleColor.White);
                //市县域ID
                iPrint.DoubleInOneLine("市县域ID:", ConsoleColor.Green, cityId.ToString("D4"), ConsoleColor.White);
                //制造商ID
                iPrint.DoubleInOneLine("制造商ID:", ConsoleColor.Green, manufId, ConsoleColor.White);
                //终端型号
                iPrint.DoubleInOneLine("终端型号:", ConsoleColor.Green, terminalModel, ConsoleColor.White);
                //终端ID
                iPrint.DoubleInOneLine("终端ID:", ConsoleColor.Green, terminalId, ConsoleColor.White);
                //车牌颜色
                iPrint.DoubleInOneLine("车牌颜色:", ConsoleColor.Green, carPlateColor.ToString(), ConsoleColor.White);
                //车辆标识
                if (carPlateColor == 0)
                {
                    iPrint.DoubleInOneLine("车辆VIN:", ConsoleColor.Green, carVin, ConsoleColor.White);
                }
                else
                {
                    iPrint.DoubleInOneLine("机动车号牌:", ConsoleColor.Green, carPlateNumber, ConsoleColor.White);
                }
                #endregion
            }
            catch (Exception e)
            {
                throw new Exception($"{this.GetType().FullName}.{MethodBase.GetCurrentMethod().Name}异常:{e.Message}");
            }
        }
예제 #5
0
        private ushort msgBodyProperty;                        //消息体属性,详见JTT808-2013第4.4.3章节
        #endregion


        /// <summary>
        /// MessageHead
        /// </summary>
        /// <param name="msgHead"></param>
        public MessageHead(byte[] msgHead)
        {
            BytesConverter iBytesConverter = new BytesConverter();
            int            startIndex      = 0;
            int            length;

            try
            {
                //提取"消息ID"
                MsgId      = iBytesConverter.ToUShort(msgHead, startIndex);
                length     = iBytesConverter.returnLength;
                startIndex = startIndex + length;

                //提取"消息体属性"
                msgBodyProperty = iBytesConverter.ToUShort(msgHead, startIndex);
                length          = iBytesConverter.returnLength;
                startIndex      = startIndex + length;

                //提取"消息体属性"中的:消息体长度
                MsgLength = Convert.ToUInt16(msgBodyProperty & 0x03FF);

                //提取"消息体属性"中的:数据加密方式,并计算分析加密方式
                encryption10 = Convert.ToBoolean(msgBodyProperty & 0x0400);
                encryption11 = Convert.ToBoolean(msgBodyProperty & 0x0800);
                encryption12 = Convert.ToBoolean(msgBodyProperty & 0x1000);
                if (encryption10)
                {
                    EncryptionType = "RSA";
                }
                else
                {
                    EncryptionType = "unencrypted";
                }

                //提取"消息体属性"中的:分包
                IsSubpackage = Convert.ToBoolean(msgBodyProperty & 0x2000);

                //提取"消息体属性"中的:保留
                Reserved = Convert.ToByte(msgBodyProperty & 0xC000);

                //提取"终端手机号"
                length      = 6;
                PhoneNumber = iBytesConverter.ToStringInBase(msgHead, startIndex, length, 16);
                startIndex  = startIndex + length;

                //提取"消息流水号"
                MsgSequence = iBytesConverter.ToUShort(msgHead, startIndex);

                #region  打印结果
                ConsoleColorPrint iPrint = new ConsoleColorPrint();
                string[]          iPrintStrings;
                ConsoleColor[]    iPrintConsoleColor;

                //标题
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("\n---消息头------------------");
                //消息ID
                iPrintStrings = new string[3] {
                    "消息ID:", "0x", MsgId.ToString("X4")
                };
                iPrintConsoleColor = new ConsoleColor[3] {
                    ConsoleColor.Magenta, ConsoleColor.White, ConsoleColor.White
                };
                iPrint.MultipleInOneLine(iPrintStrings, iPrintConsoleColor);
                //消息体长度
                iPrintStrings = new string[3] {
                    "消息体长度:", MsgLength.ToString(), "Bytes"
                };
                iPrintConsoleColor = new ConsoleColor[3] {
                    ConsoleColor.Magenta, ConsoleColor.White, ConsoleColor.White
                };
                iPrint.MultipleInOneLine(iPrintStrings, iPrintConsoleColor);
                //数据加密方式
                iPrint.DoubleInOneLine("数据加密方式:", ConsoleColor.Magenta, EncryptionType, ConsoleColor.White);
                //分包
                iPrint.DoubleInOneLine("分包:", ConsoleColor.Magenta, IsSubpackage.ToString(), ConsoleColor.White);
                //保留
                iPrint.DoubleInOneLine("保留:", ConsoleColor.Magenta, Reserved.ToString(), ConsoleColor.White);
                //终端手机号
                iPrint.DoubleInOneLine("终端手机号:", ConsoleColor.Magenta, PhoneNumber, ConsoleColor.White);
                //消息流水号
                iPrint.DoubleInOneLine("消息流水号:", ConsoleColor.Magenta, MsgSequence.ToString(), ConsoleColor.White);
                #endregion
            }
            catch (Exception e)
            {
                throw new Exception($"{this.GetType().FullName}.{MethodBase.GetCurrentMethod().Name}异常:{e.Message}");
            }
        }
        public void Main(byte[] input)
        {
            try
            {
                //协议相关变量
                ushort replySeq;        //应答流水号,详见JTT808-2013第8.1章节
                ushort replyId;         //应答ID,详见JTT808-2013第8.1章节
                byte   result;          //结果,详见JTT808-2013第8.1章节
                string resultExplain;   //结果的描述

                //临时变量
                BytesConverter iBytesConverter = new BytesConverter();
                int            startIndex      = 0;
                int            length;

                //解析"应答流水号"
                replySeq   = iBytesConverter.ToUShort(input, startIndex);
                length     = iBytesConverter.returnLength;
                startIndex = startIndex + length;

                //解析"应答ID"
                replyId    = iBytesConverter.ToUShort(input, startIndex);
                length     = iBytesConverter.returnLength;
                startIndex = startIndex + length;

                //解析"结果"
                result        = iBytesConverter.ToByte(input, startIndex);
                resultExplain = result.ToString("D");
                length        = iBytesConverter.returnLength;
                startIndex    = startIndex + length;
                switch (result)
                {
                case 0:
                    resultExplain = resultExplain + " 成功/确认";
                    break;

                case 1:
                    resultExplain = resultExplain + " 失败";
                    break;

                case 2:
                    resultExplain = resultExplain + " 消息有误";
                    break;

                case 3:
                    resultExplain = resultExplain + " 不支持";
                    break;

                case 4:
                    resultExplain = resultExplain + " 报警处理确认";
                    break;

                default:
                    resultExplain = "!!!>>>数值错误<<<!!!";
                    break;
                }

                //打印
                ConsoleColorPrint iPrint = new ConsoleColorPrint();
                iPrint.TripleInOneLine("---消息体名称:", ConsoleColor.Gray, "平台通用应答", ConsoleColor.Green, "---", ConsoleColor.Gray);
                iPrint.DoubleInOneLine("应答流水号:", ConsoleColor.Green, replySeq.ToString("D"), ConsoleColor.White);
                iPrint.DoubleInOneLine("应答ID:", ConsoleColor.Green, replyId.ToString("D"), ConsoleColor.White);
                iPrint.DoubleInOneLine("结果:", ConsoleColor.Green, resultExplain, ConsoleColor.White);
            }
            catch (Exception e)
            {
                throw new Exception($"{this.GetType().FullName}.{MethodBase.GetCurrentMethod().Name}异常:{e.Message}");
            }
        }