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