From 973bf71b874a67da0ebf26cc0e9617bf5364026f Mon Sep 17 00:00:00 2001 From: smallchi <564952747@qq.com> Date: Mon, 12 Aug 2019 11:43:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0808=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=B1=9E=E6=80=A7=E6=98=AF=E5=90=A6=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=8E=BB0=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Formatters/JT808HeaderFormatter.cs | 2 +- .../Formatters/JT808HeaderPackageFormatter.cs | 2 +- .../Formatters/JT808PackageFormatter.cs | 2 +- .../JT808_0x0107_Formatter.cs | 2 +- .../JT808_0x8103_Formatter.cs | 5 ++++- .../Interfaces/GlobalConfigBase.cs | 2 ++ src/JT808.Protocol/Interfaces/IJT808Config.cs | 6 ++++++ .../JT808HeaderMessageBodyProperty.cs | 6 +++--- .../MessagePack/JT808MessagePackReader.cs | 16 +++++++++++----- 9 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/JT808.Protocol/Formatters/JT808HeaderFormatter.cs b/src/JT808.Protocol/Formatters/JT808HeaderFormatter.cs index 95fdd14..28b806d 100644 --- a/src/JT808.Protocol/Formatters/JT808HeaderFormatter.cs +++ b/src/JT808.Protocol/Formatters/JT808HeaderFormatter.cs @@ -20,7 +20,7 @@ namespace JT808.Protocol.Formatters // 2.消息体属性 jT808Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(reader.ReadUInt16()); // 3.终端手机号 - jT808Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength); + jT808Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); jT808Header.MsgNum = reader.ReadUInt16(); // 4.判断有无分包 if (jT808Header.MessageBodyProperty.IsPackage) diff --git a/src/JT808.Protocol/Formatters/JT808HeaderPackageFormatter.cs b/src/JT808.Protocol/Formatters/JT808HeaderPackageFormatter.cs index 41b48b4..418af5b 100644 --- a/src/JT808.Protocol/Formatters/JT808HeaderPackageFormatter.cs +++ b/src/JT808.Protocol/Formatters/JT808HeaderPackageFormatter.cs @@ -32,7 +32,7 @@ namespace JT808.Protocol.Formatters // 3.2.1.解包消息体属性 jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(messageBodyPropertyValue); // 3.3.读取终端手机号 - jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength); + jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); // 3.4.读取消息流水号 jT808Package.Header.MsgNum = reader.ReadUInt16(); // 3.5.判断有无分包 diff --git a/src/JT808.Protocol/Formatters/JT808PackageFormatter.cs b/src/JT808.Protocol/Formatters/JT808PackageFormatter.cs index a44705c..de18470 100644 --- a/src/JT808.Protocol/Formatters/JT808PackageFormatter.cs +++ b/src/JT808.Protocol/Formatters/JT808PackageFormatter.cs @@ -34,7 +34,7 @@ namespace JT808.Protocol.Formatters // 3.2.读取消息体属性 jT808Package.Header.MessageBodyProperty=new JT808HeaderMessageBodyProperty(reader.ReadUInt16()); // 3.3.读取终端手机号 - jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength); + jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); // 3.4.读取消息流水号 jT808Package.Header.MsgNum= reader.ReadUInt16(); // 3.5.判断有无分包 diff --git a/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x0107_Formatter.cs b/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x0107_Formatter.cs index e13cc06..fd5454a 100644 --- a/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x0107_Formatter.cs +++ b/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x0107_Formatter.cs @@ -15,7 +15,7 @@ namespace JT808.Protocol.Formatters.MessageBodyFormatters jT808_0X0107.MakerId = reader.ReadString(5); jT808_0X0107.TerminalModel = reader.ReadString(20); jT808_0X0107.TerminalId = reader.ReadString(7); - jT808_0X0107.Terminal_SIM_ICCID = reader.ReadBCD(10); + jT808_0X0107.Terminal_SIM_ICCID = reader.ReadBCD(10, config.Trim); jT808_0X0107.Terminal_Hardware_Version_Length = reader.ReadByte(); jT808_0X0107.Terminal_Hardware_Version_Num = reader.ReadString(jT808_0X0107.Terminal_Hardware_Version_Length); jT808_0X0107.Terminal_Firmware_Version_Length = reader.ReadByte(); diff --git a/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x8103_Formatter.cs b/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x8103_Formatter.cs index 1293477..6702171 100644 --- a/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x8103_Formatter.cs +++ b/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x8103_Formatter.cs @@ -36,7 +36,10 @@ namespace JT808.Protocol.Formatters.MessageBodyFormatters } } } - catch (Exception ex) { } + catch (Exception ex) + { + + } return jT808_0x8103; } diff --git a/src/JT808.Protocol/Interfaces/GlobalConfigBase.cs b/src/JT808.Protocol/Interfaces/GlobalConfigBase.cs index 1a8ca9e..f955374 100644 --- a/src/JT808.Protocol/Interfaces/GlobalConfigBase.cs +++ b/src/JT808.Protocol/Interfaces/GlobalConfigBase.cs @@ -23,6 +23,7 @@ namespace JT808.Protocol.Interfaces JT808_0X8103_Custom_Factory = new JT808_0x8103_Custom_Factory(); JT808_0X8103_Factory = new JT808_0x8103_Factory(); TerminalPhoneNoLength = 12; + Trim = true; } public abstract string ConfigId { get; } public virtual IJT808MsgSNDistributed MsgSNDistributed { get; set; } @@ -37,6 +38,7 @@ namespace JT808.Protocol.Interfaces public virtual IJT808_0x8103_Custom_Factory JT808_0X8103_Custom_Factory { get; set; } public virtual IJT808_0x8103_Factory JT808_0X8103_Factory { get; set; } public virtual int TerminalPhoneNoLength { get; set; } + public virtual bool Trim { get; set; } public virtual IJT808Config Register(params Assembly[] externalAssemblies) { if (externalAssemblies != null) diff --git a/src/JT808.Protocol/Interfaces/IJT808Config.cs b/src/JT808.Protocol/Interfaces/IJT808Config.cs index e31856c..a1d9d7b 100644 --- a/src/JT808.Protocol/Interfaces/IJT808Config.cs +++ b/src/JT808.Protocol/Interfaces/IJT808Config.cs @@ -59,6 +59,12 @@ namespace JT808.Protocol /// bool SkipCRCCode { get; set; } /// + /// ReadBCD是否需要去0操作 + /// 默认是去0 + /// 注意:有时候对协议来说是有意义的0 + /// + bool Trim { get; set; } + /// /// 设备终端号(默认12位) /// int TerminalPhoneNoLength { get; set; } diff --git a/src/JT808.Protocol/JT808HeaderMessageBodyProperty.cs b/src/JT808.Protocol/JT808HeaderMessageBodyProperty.cs index 0f69227..e093529 100644 --- a/src/JT808.Protocol/JT808HeaderMessageBodyProperty.cs +++ b/src/JT808.Protocol/JT808HeaderMessageBodyProperty.cs @@ -51,12 +51,12 @@ namespace JT808.Protocol { // 1.是否分包 int tmpIsPacke = 0; - // 2.是否加密 - int tmpEncrypt = 0; if (IsPackage) { tmpIsPacke = 1 << 13; } + // 2.是否加密 + int tmpEncrypt; // 2.3.数据加密方式 switch (Encrypt) { @@ -64,7 +64,7 @@ namespace JT808.Protocol tmpEncrypt = 0; break; case JT808EncryptMethod.RSA: - tmpEncrypt=1 << 10; + tmpEncrypt = 1 << 10; break; default: tmpEncrypt = 0; diff --git a/src/JT808.Protocol/MessagePack/JT808MessagePackReader.cs b/src/JT808.Protocol/MessagePack/JT808MessagePackReader.cs index 597ddfb..20a3e28 100644 --- a/src/JT808.Protocol/MessagePack/JT808MessagePackReader.cs +++ b/src/JT808.Protocol/MessagePack/JT808MessagePackReader.cs @@ -25,8 +25,8 @@ namespace JT808.Protocol.MessagePack /// 主要用来一次性读取所有数据体内容操作 /// private bool _decoded; - private static byte[] decode7d01 = new byte[] { 0x7d, 0x01 }; - private static byte[] decode7d02 = new byte[] { 0x7d, 0x02 }; + private static readonly byte[] decode7d01 = new byte[] { 0x7d, 0x01 }; + private static readonly byte[] decode7d02 = new byte[] { 0x7d, 0x02 }; /// /// 解码(转义还原),计算校验和 /// @@ -308,7 +308,7 @@ namespace JT808.Protocol.MessagePack } return d; } - public string ReadBCD(int len) + public string ReadBCD(int len , bool trim = true) { int count = len / 2; var readOnlySpan = GetReadOnlySpan(count); @@ -317,8 +317,14 @@ namespace JT808.Protocol.MessagePack { bcdSb.Append(readOnlySpan[i].ToString("X2")); } - // todo:对于协议来说这个0是有意义的,下个版本在去掉 - return bcdSb.ToString().TrimStart('0'); + if (trim) + { + return bcdSb.ToString().TrimStart('0'); + } + else + { + return bcdSb.ToString(); + } } private ReadOnlySpan GetReadOnlySpan(int count) {