CRC-CCITT 16位 Python 手动计算

10 投票
8 回答
42357 浏览
提问于 2025-04-18 16:48

问题

我正在为一个嵌入式设备编写代码。网上有很多关于CRC-CCITT 16位计算的解决方案,但大多数都需要使用库。

由于使用库几乎不可能,并且会消耗设备的资源,所以我需要一个函数来完成这个任务。

可能的解决方案

我在网上找到了一种CRC计算方法,但它的实现是错误的。

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

现有的在线解决方案

以下链接可以正确计算16位CRC。

http://www.lammertbies.nl/comm/info/crc-calculation.html#intr

在“CRC-CCITT (XModem)”下的结果就是正确的CRC。

规范

我相信现有在线解决方案中的“CRC-CCITT (XModem)”计算使用的多项式是0x1021

问题

如果有人能写一个新的函数,或者提供一些方向来解决checkCRC函数,使其符合要求,那就太好了。请注意,使用库或任何import都没有帮助。

8 个回答

0

这里有一个C语言的版本,你可以把它翻译成Python:

#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;
}

crc 被初始化为零。

1

如果有人对用Python计算CRC-16-CITT感兴趣,现在Python里有一个内置的包叫做binascii,可以帮你处理这个问题。你可以查看这个链接了解更多:binascii.b2a_hqx(data, value)

2

原来的函数 checkCRC 也可以用来做“CRC-CCITT (XModem)”。

只需要设置:

poly = 0x1021
reg = 0

而不是

poly = 0x11021
reg = 0xFFFF
2

这是我使用的一个函数:

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
16

这里有一个用Python写的库,它是从C语言的库移植过来的,主要用于计算CRC-CCITT XMODEM的校验和,详细信息可以在这个链接找到:http://www.lammertbies.nl/comm/info/crc-calculation.html

这个库在实际应用中很有意思,因为它提前计算了一张CRC的表,这样可以提高计算速度。

使用方法(可以用字符串或字节列表):

crc('123456789')
crcb(0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39)

测试的结果是:'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

你提到的checkCRC函数是CRC-CCITT的'1D0F'变种,如果你在开头把poly = 0x11021替换成poly = 0x1021,就可以了。

撰写回答