7

問題

組み込みデバイス用のコードを書いています。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

4

7 に答える 7

14

これは、 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 = 0x11021poly = 0x1021

于 2014-08-12T08:05:16.983 に答える
2

本来の機能であるcheckCRC、「CRC-CCITT (XModem)」もできます。

設定するだけです:

poly = 0x1021
reg = 0

それ以外の

poly = 0x11021
reg = 0xFFFF
于 2016-07-29T09:38:59.623 に答える
0

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ゼロに初期化されます。

于 2014-08-12T04:14:02.137 に答える