CRC-CCITT 16 位 Python 手动计算



>问题

我正在为嵌入式设备编写代码。许多用于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 += 'x00x00' 
    #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都无济于事。

这是 CRC-CCITT XMODEM http://www.lammertbies.nl/comm/info/crc-calculation.html C 库的 python 端口

这个库对于实际用例很有趣,因为它预先计算了一个 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

这是我

使用的一个函数:

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

原来的函数checkCRC也可以做"CRC-CCITT (XModem("。

只需设置:

poly = 0x1021
reg = 0

而不是

poly = 0x11021
reg = 0xFFFF

这是一个可以翻译成 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;
}

crc初始化为零。

上面接受的答案是错误的。它不会像 http://srecord.sourceforge.net/crc16-ccitt.html 给出的那样用 16 位 0 来增加零长度输入。幸运的是,它可以很容易地修复。我只会发布我所做的更改。

def crc(str):
    crc = PRESET
    # start crc with two zero bytes
    for _ in range(2):
        crc = _update_crc(crc, 0)
    for c in str:
        crc = _update_crc(crc, ord(c))
    return crc
def crcb(*i):
    crc = PRESET
    for _ in range(2):
        crc = _update_crc(crc, 0)
    for c in i:
        crc = _update_crc(crc, c)
    return crc

现在,如果我们将新实现与预期的CRC值进行比较,我们将得到"good_crc"值而不是"bad_crc"值。

如果有人对使用python的CRC-16-CITT感兴趣,现在有一个内置的python包(binascii(可以解决这个问题:binascii.b2a_hqx(data, value)

  • 一个不错的模块:https://fastcrc.readthedocs.io/en/stable/

    import fastcrc
    data = bytes.fromhex("0900000000423046444f3231323630553058000000000000000000a38e90da0e020000000000000000004149522d415032383032492d452d4b392020202000440188b4034130464357323532335030504c0000000000004c0200010064000102000c006400010200ff5630354149522d415032383032492d452d4b392020000000000000000000000000500f801ca6a01000500f801ca6b01000490189d6051c018b2c0541503238303020202020202020202020202020202021072500000000000000000000000000000000000000")
    print(hex(fastcrc.crc16.genibus(data)))
    
  • 在线API:https://crccalc.com/

我开发了一个小的python模块来生成crc。试一试并检查源代码可能会有所帮助!

https://github.com/killercode/PythonCRC

对于您想要的内容,您只需要使用以下代码

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

希望对:)有所帮助

最新更新