@@ -29,9 +29,12 @@ namespace JT809.Protocol.JT809Formatters | |||
// 2.3. 从消息头到校验码前一个字节 | |||
ushort checkCode = buffer.ToCRC16_CCITT(1, checkIndex); | |||
// 2.4. 验证校验码 | |||
if (jT809Package.CRCCode != checkCode) | |||
if (!JT809GlobalConfig.Instance.SkipCRCCode) | |||
{ | |||
throw new JT809Exception(JT809ErrorCode.CRC16CheckInvalid,$"{jT809Package.CRCCode.ToString()}!={checkCode.ToString()}"); | |||
if (jT809Package.CRCCode != checkCode) | |||
{ | |||
throw new JT809Exception(JT809ErrorCode.CRC16CheckInvalid, $"{jT809Package.CRCCode.ToString()}!={checkCode.ToString()}"); | |||
} | |||
} | |||
jT809Package.BeginFlag = JT809BinaryExtensions.ReadByteLittle(buffer, ref offset); | |||
// 3.初始化消息头 | |||
@@ -14,6 +14,7 @@ namespace JT809.Protocol | |||
{ | |||
MsgSNDistributed = new DefaultMsgSNDistributedImpl(); | |||
HeaderOptions = new JT809HeaderOptions(); | |||
SkipCRCCode = false; | |||
} | |||
public static JT809GlobalConfig Instance | |||
@@ -29,23 +30,51 @@ namespace JT809.Protocol | |||
public IMsgSNDistributed MsgSNDistributed {get;private set;} | |||
public JT809HeaderOptions HeaderOptions { get; private set; } | |||
/// <summary> | |||
/// 跳过校验码 | |||
/// 默认:false | |||
/// </summary> | |||
public bool SkipCRCCode { get; private set; } | |||
/// <summary> | |||
/// 设置加密算法实现 | |||
/// </summary> | |||
/// <param name="jT809Encrypt"></param> | |||
/// <returns></returns> | |||
public JT809GlobalConfig SetEncrypt(IJT809Encrypt jT809Encrypt) | |||
{ | |||
instance.Value.Encrypt = jT809Encrypt; | |||
return instance.Value; | |||
} | |||
/// <summary> | |||
/// 设置消息序列号 | |||
/// </summary> | |||
/// <param name="msgSNDistributed"></param> | |||
/// <returns></returns> | |||
public JT809GlobalConfig SetMsgSNDistributed(IMsgSNDistributed msgSNDistributed) | |||
{ | |||
instance.Value.MsgSNDistributed = msgSNDistributed; | |||
return instance.Value; | |||
} | |||
/// <summary> | |||
/// 设置头部选项 | |||
/// </summary> | |||
/// <param name="jT809HeaderOptions"></param> | |||
/// <returns></returns> | |||
public JT809GlobalConfig SetHeaderOptions(JT809HeaderOptions jT809HeaderOptions) | |||
{ | |||
instance.Value.HeaderOptions = jT809HeaderOptions; | |||
return instance.Value; | |||
} | |||
/// <summary> | |||
/// 设置跳过校验码 | |||
/// 场景:测试的时候,可能需要收到改数据,所以测试的时候有用 | |||
/// </summary> | |||
/// <param name="skipCRCCode"></param> | |||
/// <returns></returns> | |||
public JT809GlobalConfig SetSkipCRCCode(bool skipCRCCode) | |||
{ | |||
instance.Value.SkipCRCCode = skipCRCCode; | |||
return instance.Value; | |||
} | |||
} | |||
} |
@@ -12,28 +12,12 @@ namespace JT809.Protocol | |||
{ | |||
public static byte[] Serialize(JT809Package jT809Package, int minBufferSize = 4096) | |||
{ | |||
var formatter = JT809FormatterExtensions.GetFormatter<JT809Package>(); | |||
var pool = MemoryPool<byte>.Shared; | |||
IMemoryOwner<byte> buffer = pool.Rent(minBufferSize); | |||
try | |||
{ | |||
var len = formatter.Serialize(buffer, 0, jT809Package); | |||
return buffer.Memory.Slice(0, len).ToArray(); | |||
} | |||
finally | |||
{ | |||
// 源码:System.Memory.MemoryPool | |||
// private static readonly MemoryPool<T> s_shared = new ArrayMemoryPool<T>(); | |||
// 单例内存池 不需要手动释放资源 | |||
// buffer.Dispose() 相当于调用ArrayPool<T>.Shared.Return(array) | |||
buffer.Dispose(); | |||
} | |||
return Serialize(jT809Package, minBufferSize); | |||
} | |||
public static JT809Package Deserialize(ReadOnlySpan<byte> bytes) | |||
{ | |||
var formatter = JT809FormatterExtensions.GetFormatter<JT809Package>(); | |||
return formatter.Deserialize(bytes, out int readSize); | |||
return Deserialize<JT809Package>(bytes); | |||
} | |||
public static byte[] Serialize<T>(T obj, int minBufferSize = 4096) | |||