我需要验证 CRC8-SAE-J1850 消息,因此编写了一个脚本,它读取日志并需要从那里计算 CRC8(非零)以将它们与日志中的 CRC8 值匹配,然后检查哪一步工具链造成了麻烦。
无论如何,我花了一些时间研究文档、SO 帖子和其他人的源代码,但我想坚持使用 python,以便更轻松地进行文本处理和与我的其他工具的接口(interface)。
我在 sourceforge 上找到了一些代码 evansneath's Python CRC Implementation这是非常直接的,想试一试,但我认为它没有按预期工作(也许我完全理解错误,但我被困在这里):
def crc(msg, div, code='11111111'):
"""Cyclic Redundancy Check
Generates an error detecting code based on an inputted message and divisor in the form of a polynomial representation.
Arguments:
msg: The input message of which to generate the output code.
div: The divisor in polynomial form. For example, if the polynomial of x^3 + x + 1 is given, this should be represented as '1011' in the div argument.
code: This is an option argument where a previously generated code may be passed in. This can be used to check validity. If the inputted code produces an outputted code of all zeros, then the message has no errors.
Returns:
An error-detecting code generated by the message and the given divisor.
"""
msg = msg + code
msg = list (msg)
div = list (div)
for i in range (len (msg) - len (code)):
if msg[i] == '1':
for j in range (len (div)):
msg[i+j] = str ((int (msg[i+j])+int (div[j]))%2)
return ''.join (msg[-len (code):])
#Testing:
# Use a divisor that simulates: CRC8 SAE J1850 x^8+x^4+x^3+x^2+x^0
div = '100011101'#0x1D with leading 1 as given by polynomial
msg = '10101001' # 0xA9, just for a Test
print('Input message:', hex(int(msg,2)))
print('Polynomial:', hex(int(div,2)))
o = '11111111'
z = '00000000'
code = crc(msg, div, o)
print('CRC8 code:', hex(int(code,2)))
# Test for output code of '00000000' respectively '11111111' proving that the function worked correctly
print('Success:', crc(msg, div, code) == o)
我使用这个生成器检查了结果: CRC Generator这似乎是唯一具有 CRC8 SAE J1850 零和非零的功能。
现在是有趣的部分:对于零,上面的代码工作得很好。
不幸的是,我从我要检查的软件中获得的 CRC 代码已初始化并针对 0xFF('11111111')进行了检查,这两种工具都提供了完全不同的结果。 到目前为止,我什至无法找到一些不一致的问题(我认为这是最有可能的情况)或上面脚本计算的解决方案与网站计算的解决方案之间的数学联系。 该网站符合软件的结果,但上面的 python 部分不符合。
任何人都可以指出我可能遗漏的文档还是另一个问题? 在网站上输入消息时,我已经检查了 MSB/LSB 问题,并尝试按照其他一些代码建议的那样使用 0xFE 进行初始化。但没有成功......然而,大多数示例都是零基础的,我没有任何问题。
编辑:
我检查了计算并手动完成了一个示例,并打印了每一步并取得了同样的效果。所以在数学上它似乎是正确的,但是除了附加一行“11111111”然后按位异或,然后移位直到再次出现前导,异或等...并吐出其余部分之外,SAE 在做什么?这一定是我这边的理解问题。
Test 1 ---------------------------
Input message: 0xa9
Polynome: 0x11d
['1', '0', '1', '0', '1', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', 1']
/
['1', '0', '0', '0', '1', '1', '1', '0', '1']
=
current message: ['1', '0', '1', '0', '1', '0', '0', '1', '1', '1', '1', '1', '1 ', '1', '1', '1']
shift 0 Bits
1 XOR 1 = 0
0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 0 = 0
1 XOR 1 = 0
0 XOR 1 = 1
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
current message: ['0', '0', '1', '0', '0', '1', '1', '1', '0', '1', '1', '1', '1 ', '1', '1', '1']
shift 2 Bits
1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 0 = 0
1 XOR 0 = 1
1 XOR 1 = 0
1 XOR 1 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
current message: ['0', '0', '0', '0', '0', '1', '0', '0', '1', '1', '0', '1', '1 ', '1', '1', '1']
shift 5 Bits
1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 0 = 0
1 XOR 0 = 1
1 XOR 1 = 0
0 XOR 1 = 1
1 XOR 1 = 0
1 XOR 0 = 1
1 XOR 1 = 0
CRC8 code: 0xab
Reverse Calculation: Check
['1', '0', '1', '0', '1', '0', '0', '1', '1', '0', '1', '0', '1', '0', '1', '1']
/
['1', '0', '0', '0', '1', '1', '1', '0', '1']
=
current message: ['1', '0', '1', '0', '1', '0', '0', '1', '1', '0', '1', '0', '1', '0', '1', '1']
shift 0 Bits
1 XOR 1 = 0
0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 0 = 0
1 XOR 1 = 0
0 XOR 1 = 1
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
current message: ['0', '0', '1', '0', '0', '1', '1', '1', '0', '0', '1', '0', '1', '0', '1', '1']
shift 2 Bits
1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 0 = 0
1 XOR 0 = 1
1 XOR 1 = 0
1 XOR 1 = 0
0 XOR 1 = 1
0 XOR 0 = 0
1 XOR 1 = 0
current message: ['0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '0', '1', '1']
shift 5 Bits
1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 1 = 1
0 XOR 1 = 1
1 XOR 0 = 1
0 XOR 1 = 1
CRC correct: True
请您参考如下方法:
CRC 通常用这些参数定义:
width=8 poly=0x1d init=0xff refin=false refout=false xorout=0xff check=0x4b name="CRC-8/SAE-J1850"
crcgen将采用它并生成 C 代码来计算 CRC。如果愿意,您可以轻松地将其转换为 Python。这是来自 crcgen 的 C 中的按位例程:
#include <stdint.h>
unsigned crc8sae_j1850_bit(unsigned crc, unsigned char const *data, size_t len) {
if (data == NULL)
return 0;
crc ^= 0xff;
while (len--) {
crc ^= *data++;
for (unsigned k = 0; k < 8; k++)
crc = crc & 0x80 ? (crc << 1) ^ 0x1d : crc << 1;
}
crc &= 0xff;
crc ^= 0xff;
return crc;
}
链接的 CRC 计算器页面上列出的“零”版本具有这些参数,只需将初始值和 xorout 值更改为零即可:
width=8 poly=0x1d init=0x00 refin=false refout=false xorout=0x00 check=0x37 name="CRC-8/SAE-J1850-ZERO"
这是来自 crcgen 的 C 中的按位例程:
#include <stdint.h>
unsigned crc8sae_j1850_zero_bit(unsigned crc, unsigned char const *data, size_t len) {
if (data == NULL)
return 0;
while (len--) {
crc ^= *data++;
for (unsigned k = 0; k < 8; k++)
crc = crc & 0x80 ? (crc << 1) ^ 0x1d : crc << 1;
}
crc &= 0xff;
return crc;
}
