diff --git a/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs b/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs
index 9676adf..ae8f7f4 100644
--- a/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs
+++ b/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs
@@ -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.初始化消息头
diff --git a/src/JT809.Protocol/JT809GlobalConfig.cs b/src/JT809.Protocol/JT809GlobalConfig.cs
index a99fe83..d772d0d 100644
--- a/src/JT809.Protocol/JT809GlobalConfig.cs
+++ b/src/JT809.Protocol/JT809GlobalConfig.cs
@@ -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; }
-
+ ///
+ /// 跳过校验码
+ /// 默认:false
+ ///
+ public bool SkipCRCCode { get; private set; }
+ ///
+ /// 设置加密算法实现
+ ///
+ ///
+ ///
public JT809GlobalConfig SetEncrypt(IJT809Encrypt jT809Encrypt)
{
instance.Value.Encrypt = jT809Encrypt;
return instance.Value;
}
-
+ ///
+ /// 设置消息序列号
+ ///
+ ///
+ ///
public JT809GlobalConfig SetMsgSNDistributed(IMsgSNDistributed msgSNDistributed)
{
instance.Value.MsgSNDistributed = msgSNDistributed;
return instance.Value;
}
-
+ ///
+ /// 设置头部选项
+ ///
+ ///
+ ///
public JT809GlobalConfig SetHeaderOptions(JT809HeaderOptions jT809HeaderOptions)
{
instance.Value.HeaderOptions = jT809HeaderOptions;
return instance.Value;
}
+ ///
+ /// 设置跳过校验码
+ /// 场景:测试的时候,可能需要收到改数据,所以测试的时候有用
+ ///
+ ///
+ ///
+ public JT809GlobalConfig SetSkipCRCCode(bool skipCRCCode)
+ {
+ instance.Value.SkipCRCCode = skipCRCCode;
+ return instance.Value;
+ }
}
}
diff --git a/src/JT809.Protocol/JT809Serializer.cs b/src/JT809.Protocol/JT809Serializer.cs
index 4c9457f..4eff7f4 100644
--- a/src/JT809.Protocol/JT809Serializer.cs
+++ b/src/JT809.Protocol/JT809Serializer.cs
@@ -12,28 +12,12 @@ namespace JT809.Protocol
{
public static byte[] Serialize(JT809Package jT809Package, int minBufferSize = 4096)
{
- var formatter = JT809FormatterExtensions.GetFormatter();
- var pool = MemoryPool.Shared;
- IMemoryOwner 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 s_shared = new ArrayMemoryPool();
- // 单例内存池 不需要手动释放资源
- // buffer.Dispose() 相当于调用ArrayPool.Shared.Return(array)
- buffer.Dispose();
- }
+ return Serialize(jT809Package, minBufferSize);
}
public static JT809Package Deserialize(ReadOnlySpan bytes)
{
- var formatter = JT809FormatterExtensions.GetFormatter();
- return formatter.Deserialize(bytes, out int readSize);
+ return Deserialize(bytes);
}
public static byte[] Serialize(T obj, int minBufferSize = 4096)