@@ -13,7 +13,7 @@ namespace JT809.Protocol.Test.JT809Extensions | |||||
public void Test1() | public void Test1() | ||||
{ | { | ||||
string vno = "粤A12345"; | string vno = "粤A12345"; | ||||
byte[] bytes = JT809BinaryExtensions.encoding.GetBytes(vno); | byte[] bytes = JT809GlobalConfig.Instance.Encoding.GetBytes(vno); | ||||
Assert.Equal(7,vno.Length); | Assert.Equal(7,vno.Length); | ||||
Assert.Equal(8, bytes.Length); | Assert.Equal(8, bytes.Length); | ||||
} | } | ||||
@@ -199,7 +199,7 @@ namespace JT809.Protocol.JT809Extensions | |||||
public static IEnumerable<byte> ToBytes(this string data) | public static IEnumerable<byte> ToBytes(this string data) | ||||
{ | { | ||||
return ToBytes(data, encoding); | return ToBytes(data, JT809GlobalConfig.Instance.Encoding); | ||||
} | } | ||||
public static IEnumerable<byte> ToBytes(this int data, int len) | public static IEnumerable<byte> ToBytes(this int data, int len) | ||||
@@ -18,7 +18,7 @@ namespace JT809.Protocol.JT809Extensions | |||||
ushort checkCode = 0xFFFF; | ushort checkCode = 0xFFFF; | ||||
for (int j = offset; j < iLen; ++j) | for (int j = offset; j < iLen; ++j) | ||||
{ | { | ||||
checkCode = (ushort)((checkCode << 8) ^ (ushort)CRC[(checkCode >> 8) ^ ucbuf[j]]); | checkCode = (ushort)((checkCode << 8) ^ (ushort)JT809GlobalConfig.Instance.CRC[(checkCode >> 8) ^ ucbuf[j]]); | ||||
} | } | ||||
return checkCode; | return checkCode; | ||||
} | } | ||||
@@ -35,7 +35,7 @@ namespace JT809.Protocol.JT809Extensions | |||||
ushort checkCode = 0xFFFF; | ushort checkCode = 0xFFFF; | ||||
for (int j = offset; j < iLen; ++j) | for (int j = offset; j < iLen; ++j) | ||||
{ | { | ||||
checkCode = (ushort)((checkCode << 8) ^ (ushort)CRC[(checkCode >> 8) ^ ucbuf[j]]); | checkCode = (ushort)((checkCode << 8) ^ (ushort)JT809GlobalConfig.Instance.CRC[(checkCode >> 8) ^ ucbuf[j]]); | ||||
} | } | ||||
return checkCode; | return checkCode; | ||||
} | } | ||||
@@ -1,38 +0,0 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
namespace JT809.Protocol.JT809Extensions | |||||
{ | |||||
public static partial class JT809BinaryExtensions | |||||
{ | |||||
public static Encoding encoding; | |||||
private const ushort cnCRC_CCITT = 0x1021; //CRC校验多项式 | |||||
private static ulong[] CRC = new ulong[256]; //建立CRC16表 | |||||
static JT809BinaryExtensions() | |||||
{ | |||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); | |||||
encoding = Encoding.GetEncoding("GBK"); | |||||
ushort i, j; | |||||
ushort nData; | |||||
ushort nAccum; | |||||
for (i = 0; i < 256; i++) | |||||
{ | |||||
nData = (ushort)(i << 8); | |||||
nAccum = 0; | |||||
for (j = 0; j < 8; j++) | |||||
{ | |||||
if (((nData ^ nAccum) & 0x8000) > 0) | |||||
nAccum = (ushort)((nAccum << 1) ^ cnCRC_CCITT); | |||||
else | |||||
nAccum <<= 1; | |||||
nData <<= 1; | |||||
} | |||||
CRC[i] = (ulong)nAccum; | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -11,21 +11,21 @@ namespace JT809.Protocol.JT809Extensions | |||||
{ | { | ||||
public static string ReadStringLittle(ReadOnlySpan<byte> read, ref int offset, int len) | public static string ReadStringLittle(ReadOnlySpan<byte> read, ref int offset, int len) | ||||
{ | { | ||||
string value = encoding.GetString(read.Slice(offset, len).ToArray()); | string value = JT809GlobalConfig.Instance.Encoding.GetString(read.Slice(offset, len).ToArray()); | ||||
offset += len; | offset += len; | ||||
return value.Trim('\0'); | return value.Trim('\0'); | ||||
} | } | ||||
public static string ReadStringLittle(ReadOnlySpan<byte> read, ref int offset) | public static string ReadStringLittle(ReadOnlySpan<byte> read, ref int offset) | ||||
{ | { | ||||
string value = encoding.GetString(read.Slice(offset).ToArray()); | string value = JT809GlobalConfig.Instance.Encoding.GetString(read.Slice(offset).ToArray()); | ||||
offset += value.Length; | offset += value.Length; | ||||
return value.Trim('\0'); | return value.Trim('\0'); | ||||
} | } | ||||
public static int WriteStringLittle(byte[] bytes, int offset, string data) | public static int WriteStringLittle(byte[] bytes, int offset, string data) | ||||
{ | { | ||||
byte[] codeBytes = encoding.GetBytes(data); | byte[] codeBytes = JT809GlobalConfig.Instance.Encoding.GetBytes(data); | ||||
Array.Copy(codeBytes, 0, bytes, offset, codeBytes.Length); | Array.Copy(codeBytes, 0, bytes, offset, codeBytes.Length); | ||||
return codeBytes.Length; | return codeBytes.Length; | ||||
} | } | ||||
@@ -39,7 +39,7 @@ namespace JT809.Protocol.JT809Extensions | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
tempBytes = encoding.GetBytes(data); | tempBytes = JT809GlobalConfig.Instance.Encoding.GetBytes(data); | ||||
} | } | ||||
byte[] rBytes = new byte[len]; | byte[] rBytes = new byte[len]; | ||||
for (int i = 0; i < tempBytes.Length; i++) | for (int i = 0; i < tempBytes.Length; i++) | ||||
@@ -54,7 +54,7 @@ namespace JT809.Protocol.JT809Extensions | |||||
public static int WriteStringPadLeftLittle(byte[] bytes, int offset, string data, int len) | public static int WriteStringPadLeftLittle(byte[] bytes, int offset, string data, int len) | ||||
{ | { | ||||
data = data.PadLeft(len, '\0'); | data = data.PadLeft(len, '\0'); | ||||
byte[] codeBytes = encoding.GetBytes(data); | byte[] codeBytes = JT809GlobalConfig.Instance.Encoding.GetBytes(data); | ||||
Array.Copy(codeBytes, 0, bytes, offset, codeBytes.Length); | Array.Copy(codeBytes, 0, bytes, offset, codeBytes.Length); | ||||
return codeBytes.Length; | return codeBytes.Length; | ||||
} | } | ||||
@@ -62,7 +62,7 @@ namespace JT809.Protocol.JT809Extensions | |||||
public static int WriteStringPadRightLittle(byte[] bytes, int offset, string data, int len) | public static int WriteStringPadRightLittle(byte[] bytes, int offset, string data, int len) | ||||
{ | { | ||||
data = data.PadRight(len, '\0'); | data = data.PadRight(len, '\0'); | ||||
byte[] codeBytes = encoding.GetBytes(data); | byte[] codeBytes = JT809GlobalConfig.Instance.Encoding.GetBytes(data); | ||||
Array.Copy(codeBytes, 0, bytes, offset, codeBytes.Length); | Array.Copy(codeBytes, 0, bytes, offset, codeBytes.Length); | ||||
return codeBytes.Length; | return codeBytes.Length; | ||||
} | } | ||||
@@ -13,11 +13,16 @@ namespace JT809.Protocol | |||||
private JT809GlobalConfig() | private JT809GlobalConfig() | ||||
{ | { | ||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); | |||||
Encoding = Encoding.GetEncoding("GBK"); | |||||
MsgSNDistributed = new DefaultMsgSNDistributedImpl(); | MsgSNDistributed = new DefaultMsgSNDistributedImpl(); | ||||
HeaderOptions = new JT809HeaderOptions(); | HeaderOptions = new JT809HeaderOptions(); | ||||
SkipCRCCode = false; | SkipCRCCode = false; | ||||
initCrcTable(); | |||||
} | } | ||||
public Encoding Encoding; | |||||
public static JT809GlobalConfig Instance | public static JT809GlobalConfig Instance | ||||
{ | { | ||||
get | get | ||||
@@ -92,5 +97,31 @@ namespace JT809.Protocol | |||||
instance.Value.SkipCRCCode = skipCRCCode; | instance.Value.SkipCRCCode = skipCRCCode; | ||||
return instance.Value; | return instance.Value; | ||||
} | } | ||||
private const ushort cnCRC_CCITT = 0x1021; //CRC校验多项式 | |||||
public ulong[] CRC; //建立CRC16表 | |||||
private void initCrcTable() | |||||
{ | |||||
CRC = new ulong[256]; | |||||
ushort i, j; | |||||
ushort nData; | |||||
ushort nAccum; | |||||
for (i = 0; i < 256; i++) | |||||
{ | |||||
nData = (ushort)(i << 8); | |||||
nAccum = 0; | |||||
for (j = 0; j < 8; j++) | |||||
{ | |||||
if (((nData ^ nAccum) & 0x8000) > 0) | |||||
nAccum = (ushort)((nAccum << 1) ^ cnCRC_CCITT); | |||||
else | |||||
nAccum <<= 1; | |||||
nData <<= 1; | |||||
} | |||||
CRC[i] = (ulong)nAccum; | |||||
} | |||||
} | |||||
} | } | ||||
} | } |