CRC-CCITT 16位Python手动计算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25239423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CRC-CCITT 16-bit Python Manual Calculation
提问by Alex Stewart
Problem
问题
I am writing code for an embedded device. A lot of solutions out there for CRC-CCITT 16-bit calculations require libraries.
我正在为嵌入式设备编写代码。CRC-CCITT 16 位计算的许多解决方案都需要库。
Given that using libraries is almost impossible and a drain on its resources, a function is required.
鉴于使用库几乎是不可能的并且消耗其资源,因此需要一个函数。
Possible Solution
可能的解决方案
The following CRC calculation was found online. However, its implementation is incorrect.
以下CRC计算是在网上找到的。但是,它的实现是不正确的。
http://bytes.com/topic/python/insights/887357-python-check-crc-frame-crc-16-ccitt
http://bytes.com/topic/python/insights/887357-python-check-crc-frame-crc-16-ccitt
def checkCRC(message):
#CRC-16-CITT poly, the CRC sheme used by ymodem protocol
poly = 0x11021
#16bit operation register, initialized to zeros
reg = 0xFFFF
#pad the end of the message with the size of the poly
message += '\x00\x00'
#for each bit in the message
for byte in message:
mask = 0x80
while(mask > 0):
#left shift by one
reg<<=1
#input the next bit from the message into the right hand side of the op reg
if ord(byte) & mask:
reg += 1
mask>>=1
#if a one popped out the left of the reg, xor reg w/poly
if reg > 0xffff:
#eliminate any one that popped out the left
reg &= 0xffff
#xor with the poly, this is the remainder
reg ^= poly
return reg
Existing Online Solution
现有在线解决方案
The following link calculates a 16 bit CRC correctly.
以下链接正确计算了 16 位 CRC。
http://www.lammertbies.nl/comm/info/crc-calculation.html#intr
http://www.lammertbies.nl/comm/info/crc-calculation.html#intr
The result under "CRC-CCITT (XModem)" is the correct CRC.
“CRC-CCITT (XModem)”下的结果是正确的CRC。
Specification
规格
I believe the "CRC-CCITT (XModem)" calculation in the existing online solution uses a polynomial of 0x1021.
我相信现有在线解决方案中的“CRC-CCITT(XModem)”计算使用了多项式0x1021。
Question
题
If someone could write a new function or provide direction to solve the checkCRCfunction to the required specification. Please note that the use of libraries or any import's would not help.
如果有人可以编写一个新函数或提供方向来解决checkCRC所需的规范的函数。请注意,使用库或任何import's 都无济于事。
采纳答案by Serge Ballesta
Here is a python port of the C library from http://www.lammertbies.nl/comm/info/crc-calculation.htmlfor CRC-CCITT XMODEM
这是来自http://www.lammertbies.nl/comm/info/crc-calculation.html的 C 库的 python 端口,用于 CRC-CCITT XMODEM
This library is interesting for real use cases because it pre-computes a table of crc for enhanced speed.
这个库对于实际用例很有趣,因为它预先计算了一个 crc 表以提高速度。
Usage (with a string or a list of bytes) :
用法(使用字符串或字节列表):
crc('123456789')
crcb(0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39)
The test gives : '0x31c3'
测试给出: '0x31c3'
POLYNOMIAL = 0x1021
PRESET = 0
def _initial(c):
crc = 0
c = c << 8
for j in range(8):
if (crc ^ c) & 0x8000:
crc = (crc << 1) ^ POLYNOMIAL
else:
crc = crc << 1
c = c << 1
return crc
_tab = [ _initial(i) for i in range(256) ]
def _update_crc(crc, c):
cc = 0xff & c
tmp = (crc >> 8) ^ cc
crc = (crc << 8) ^ _tab[tmp & 0xff]
crc = crc & 0xffff
print (crc)
return crc
def crc(str):
crc = PRESET
for c in str:
crc = _update_crc(crc, ord(c))
return crc
def crcb(*i):
crc = PRESET
for c in i:
crc = _update_crc(crc, c)
return crc
Your proposed checkCRCroutine is CRC-CCITT variant '1D0F' if you replace poly = 0x11021with poly = 0x1021at the beginning.
checkCRC如果您在开头替换poly = 0x11021为poly = 0x1021,您建议的例程是 CRC-CCITT 变体“1D0F” 。
回答by Mark Adler
Here is a C version that you can translate to Python:
这是您可以转换为 Python 的 C 版本:
#define POLY 0x1021
/* CRC-16 XMODEM: polynomial 0x1021, init = 0, xorout = 0, no reflection */
unsigned crc16x(unsigned crc, unsigned char *buf, size_t len)
{
while (len--) {
crc ^= *buf++ << 8;
crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x8000 ? (crc << 1) ^ POLY : crc << 1;
}
return crc & 0xffff;
}
crcis initialized to zero.
crc初始化为零。
回答by Luciano Barcaro
Here's a function that I use:
这是我使用的一个函数:
def crc16_ccitt(crc, data):
msb = crc >> 8
lsb = crc & 255
for c in data:
x = ord(c) ^ msb
x ^= (x >> 4)
msb = (lsb ^ (x >> 3) ^ (x << 4)) & 255
lsb = (x ^ (x << 5)) & 255
return (msb << 8) + lsb
回答by Killercode
I have developed a small python module to generate crc. Give it a shot and check the source code it may help!
我开发了一个小的python模块来生成crc。试一试并检查它可能有帮助的源代码!
https://github.com/killercode/PythonCRC
https://github.com/killercode/PythonCRC
For what you want you just need to use the following code
对于你想要的,你只需要使用下面的代码
import crc
crccalc = crc.Crc()
crccalc.setCRCccitt() # Let's calculate the CRC CCITT of a value
crccalc.data = "My Data"
crccalc.compute()
print crccalc.result
Hope it helps :)
希望能帮助到你 :)
回答by Arik Yavilevich
The original function, checkCRC, can also do "CRC-CCITT (XModem)".
原来的功能,checkCRC也可以做“CRC-CCITT (XModem)”。
Just set:
只需设置:
poly = 0x1021
reg = 0
Instead of
代替
poly = 0x11021
reg = 0xFFFF

