CRC-16和/或帧校验序列



我有一个从串行端口读取7E0302403F387E的传入数据包。

起始和结束标志为7E, FCS/CRC为3F38,数据为030240。FCS是根据RFC 1662中指定的算法计算的。https://www.rfc-editor.org/rfc/rfc1662附录a

这是我用来为030240生成FCS的代码:

static byte[] HexToBytes(string input)
{
byte[] result = new byte[input.Length / 2];
for (int i = 0; i < result.Length; i++)
{
result[i] = Convert.ToByte(input.Substring(2 * i, 2), 16);
}
return result;
}

public static class Crc16
{
const ushort polynomial = 0x8408;
static readonly ushort[] table = new ushort[256];
public static ushort ComputeChecksum(byte[] bytes)
{
ushort crc = 0;
for (int i = 0; i < bytes.Length; ++i)
{
byte index = (byte)(crc ^ bytes[i]);
crc = (ushort)((crc >> 8) ^ table[index]);
}
return crc;
}
static Crc16()
{
ushort value;
ushort temp;
for (ushort i = 0; i < table.Length; ++i)
{
value = 0;
temp = i;
for (byte j = 0; j < 8; ++j)
{
if (((value ^ temp) & 0x0001) != 0)
{
value = (ushort)((value >> 1) ^ polynomial);
}
else
{
value >>= 1;
}
temp >>= 1;
}
table[i] = value;
}
}
}

我是这样称呼它的:

//string input = "00 03 00 02 04 00";
string input = "030240";
var bytes = HexToBytes(input);
string hexe = Crc16.ComputeChecksum(bytes).ToString("x2");

我应该得到3F38的FCS,但我得到9ED0。我做错了什么?

编辑1:

我正在从串口读取~u0003u0002u0004?8~。我可以把它转换成7E-03-02-40-3F-38-7E或者7e-5c-30-5c-75-30-30-30-33-5c-30-5c-75-30-30-30-32-5c-75-30-30-30-34-5c-30-3f-38-7e。第一个太短,第二个太长。我应该得到10个字节。任何建议吗?

编辑2:

我使用ASCII_To_Hex将~u0003u0002u0004?8~转换为7E-03-02-40-3F-38-7E

public string ASCII_To_Hex(string ASCII)
{
char[] charValues = dataIN.ToCharArray();
string hexOutput = "";
foreach (char _eachChar in charValues)
{
// Get the integral value of the character.
int value = Convert.ToInt32(_eachChar);
// Convert the decimal value to a hexadecimal value in string form.
hexOutput += String.Format("{0:X}", value);
// to make output as your eg 
//  hexOutput +=" "+ String.Format("{0:X}", value);
}
return hexOutput;
}

我可以在这里做什么更改来正确破译传入的数据包?

编辑3:

根据Mark的建议,我做了以下修改:

public static class Crc16
{
const ushort polynomial = 0x1021;
static readonly ushort[] table = new ushort[256];
public static ushort ComputeChecksum(byte[] bytes)
{
ushort crc = 0xffff;
for (int i = 0; i < bytes.Length; ++i)
{
byte index = (byte)(crc ^ bytes[i]);
crc = (ushort)((crc >> 8) ^ table[index]);
}
return (ushort)~crc;
}
static Crc16()
{
ushort value;
ushort temp;
for (ushort i = 0; i < table.Length; ++i)
{
value = 0;
temp = i;
for (byte j = 0; j < 8; ++j)
{
if (((value ^ temp) & 0x0001) != 0)
{
value = (ushort)((value >> 1) ^ polynomial);
}
else
{
value >>= 1;
}
temp >>= 1;
}
table[i] = value;
}
}
}

我是这样称呼它的:

string hex = "00-03-00-02-04-00";
hex = hex.Replace("-", "");
var bytes = HexToBytes(hex);
string hexe = Crc16.ComputeChecksum(bytes).ToString("X2");

我得到FO93而不是389B

根据链接的附录,CRC的初始值是0xffff,而不是0,您需要对0xffff的结果进行排他或运算,以获得要放入数据包中的FCS。

然而,这仍然不适合您的示例数据包。我只能猜测你的示例包没有被正确提取或识别。

更新:

从下面的OP评论和一些推断,消息中的实际数据是"~x00x03x00x02x04x00x9bx38~"。由于某种原因,OP遗漏了三个零字节,并且OP以一种将一个字节模糊为问号的方式打印出来。

现在CRC正确计算为0x389b,在消息little-endian中存储为0x9b0x38。要实现这一点,必须将我上面的注释应用于初始值和最终的异或。

最新更新