@@ -20,7 +20,7 @@ namespace JT808.Protocol.Formatters | |||||
// 2.消息体属性 | // 2.消息体属性 | ||||
jT808Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(reader.ReadUInt16()); | jT808Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(reader.ReadUInt16()); | ||||
// 3.终端手机号 | // 3.终端手机号 | ||||
jT808Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength); | |||||
jT808Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); | |||||
jT808Header.MsgNum = reader.ReadUInt16(); | jT808Header.MsgNum = reader.ReadUInt16(); | ||||
// 4.判断有无分包 | // 4.判断有无分包 | ||||
if (jT808Header.MessageBodyProperty.IsPackage) | if (jT808Header.MessageBodyProperty.IsPackage) | ||||
@@ -32,7 +32,7 @@ namespace JT808.Protocol.Formatters | |||||
// 3.2.1.解包消息体属性 | // 3.2.1.解包消息体属性 | ||||
jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(messageBodyPropertyValue); | jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(messageBodyPropertyValue); | ||||
// 3.3.读取终端手机号 | // 3.3.读取终端手机号 | ||||
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength); | |||||
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); | |||||
// 3.4.读取消息流水号 | // 3.4.读取消息流水号 | ||||
jT808Package.Header.MsgNum = reader.ReadUInt16(); | jT808Package.Header.MsgNum = reader.ReadUInt16(); | ||||
// 3.5.判断有无分包 | // 3.5.判断有无分包 | ||||
@@ -34,7 +34,7 @@ namespace JT808.Protocol.Formatters | |||||
// 3.2.读取消息体属性 | // 3.2.读取消息体属性 | ||||
jT808Package.Header.MessageBodyProperty=new JT808HeaderMessageBodyProperty(reader.ReadUInt16()); | jT808Package.Header.MessageBodyProperty=new JT808HeaderMessageBodyProperty(reader.ReadUInt16()); | ||||
// 3.3.读取终端手机号 | // 3.3.读取终端手机号 | ||||
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength); | |||||
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); | |||||
// 3.4.读取消息流水号 | // 3.4.读取消息流水号 | ||||
jT808Package.Header.MsgNum= reader.ReadUInt16(); | jT808Package.Header.MsgNum= reader.ReadUInt16(); | ||||
// 3.5.判断有无分包 | // 3.5.判断有无分包 | ||||
@@ -15,7 +15,7 @@ namespace JT808.Protocol.Formatters.MessageBodyFormatters | |||||
jT808_0X0107.MakerId = reader.ReadString(5); | jT808_0X0107.MakerId = reader.ReadString(5); | ||||
jT808_0X0107.TerminalModel = reader.ReadString(20); | jT808_0X0107.TerminalModel = reader.ReadString(20); | ||||
jT808_0X0107.TerminalId = reader.ReadString(7); | 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_Length = reader.ReadByte(); | ||||
jT808_0X0107.Terminal_Hardware_Version_Num = reader.ReadString(jT808_0X0107.Terminal_Hardware_Version_Length); | jT808_0X0107.Terminal_Hardware_Version_Num = reader.ReadString(jT808_0X0107.Terminal_Hardware_Version_Length); | ||||
jT808_0X0107.Terminal_Firmware_Version_Length = reader.ReadByte(); | jT808_0X0107.Terminal_Firmware_Version_Length = reader.ReadByte(); | ||||
@@ -36,7 +36,10 @@ namespace JT808.Protocol.Formatters.MessageBodyFormatters | |||||
} | } | ||||
} | } | ||||
} | } | ||||
catch (Exception ex) { } | |||||
catch (Exception ex) | |||||
{ | |||||
} | |||||
return jT808_0x8103; | return jT808_0x8103; | ||||
} | } | ||||
@@ -23,6 +23,7 @@ namespace JT808.Protocol.Interfaces | |||||
JT808_0X8103_Custom_Factory = new JT808_0x8103_Custom_Factory(); | JT808_0X8103_Custom_Factory = new JT808_0x8103_Custom_Factory(); | ||||
JT808_0X8103_Factory = new JT808_0x8103_Factory(); | JT808_0X8103_Factory = new JT808_0x8103_Factory(); | ||||
TerminalPhoneNoLength = 12; | TerminalPhoneNoLength = 12; | ||||
Trim = true; | |||||
} | } | ||||
public abstract string ConfigId { get; } | public abstract string ConfigId { get; } | ||||
public virtual IJT808MsgSNDistributed MsgSNDistributed { get; set; } | 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_Custom_Factory JT808_0X8103_Custom_Factory { get; set; } | ||||
public virtual IJT808_0x8103_Factory JT808_0X8103_Factory { get; set; } | public virtual IJT808_0x8103_Factory JT808_0X8103_Factory { get; set; } | ||||
public virtual int TerminalPhoneNoLength { get; set; } | public virtual int TerminalPhoneNoLength { get; set; } | ||||
public virtual bool Trim { get; set; } | |||||
public virtual IJT808Config Register(params Assembly[] externalAssemblies) | public virtual IJT808Config Register(params Assembly[] externalAssemblies) | ||||
{ | { | ||||
if (externalAssemblies != null) | if (externalAssemblies != null) | ||||
@@ -59,6 +59,12 @@ namespace JT808.Protocol | |||||
/// </summary> | /// </summary> | ||||
bool SkipCRCCode { get; set; } | bool SkipCRCCode { get; set; } | ||||
/// <summary> | /// <summary> | ||||
/// ReadBCD是否需要去0操作 | |||||
/// 默认是去0 | |||||
/// 注意:有时候对协议来说是有意义的0 | |||||
/// </summary> | |||||
bool Trim { get; set; } | |||||
/// <summary> | |||||
/// 设备终端号(默认12位) | /// 设备终端号(默认12位) | ||||
/// </summary> | /// </summary> | ||||
int TerminalPhoneNoLength { get; set; } | int TerminalPhoneNoLength { get; set; } | ||||
@@ -51,12 +51,12 @@ namespace JT808.Protocol | |||||
{ | { | ||||
// 1.是否分包 | // 1.是否分包 | ||||
int tmpIsPacke = 0; | int tmpIsPacke = 0; | ||||
// 2.是否加密 | |||||
int tmpEncrypt = 0; | |||||
if (IsPackage) | if (IsPackage) | ||||
{ | { | ||||
tmpIsPacke = 1 << 13; | tmpIsPacke = 1 << 13; | ||||
} | } | ||||
// 2.是否加密 | |||||
int tmpEncrypt; | |||||
// 2.3.数据加密方式 | // 2.3.数据加密方式 | ||||
switch (Encrypt) | switch (Encrypt) | ||||
{ | { | ||||
@@ -64,7 +64,7 @@ namespace JT808.Protocol | |||||
tmpEncrypt = 0; | tmpEncrypt = 0; | ||||
break; | break; | ||||
case JT808EncryptMethod.RSA: | case JT808EncryptMethod.RSA: | ||||
tmpEncrypt=1 << 10; | |||||
tmpEncrypt = 1 << 10; | |||||
break; | break; | ||||
default: | default: | ||||
tmpEncrypt = 0; | tmpEncrypt = 0; | ||||
@@ -25,8 +25,8 @@ namespace JT808.Protocol.MessagePack | |||||
/// 主要用来一次性读取所有数据体内容操作 | /// 主要用来一次性读取所有数据体内容操作 | ||||
/// </summary> | /// </summary> | ||||
private bool _decoded; | 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 }; | |||||
/// <summary> | /// <summary> | ||||
/// 解码(转义还原),计算校验和 | /// 解码(转义还原),计算校验和 | ||||
/// </summary> | /// </summary> | ||||
@@ -308,7 +308,7 @@ namespace JT808.Protocol.MessagePack | |||||
} | } | ||||
return d; | return d; | ||||
} | } | ||||
public string ReadBCD(int len) | |||||
public string ReadBCD(int len , bool trim = true) | |||||
{ | { | ||||
int count = len / 2; | int count = len / 2; | ||||
var readOnlySpan = GetReadOnlySpan(count); | var readOnlySpan = GetReadOnlySpan(count); | ||||
@@ -317,8 +317,14 @@ namespace JT808.Protocol.MessagePack | |||||
{ | { | ||||
bcdSb.Append(readOnlySpan[i].ToString("X2")); | 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<byte> GetReadOnlySpan(int count) | private ReadOnlySpan<byte> GetReadOnlySpan(int count) | ||||
{ | { | ||||