CRC-CCITT 16-bit Python manual calculation

Problem

I am writing code for embedded devices. Many solutions for CRC-CCITT 16-bit calculations require libraries.

< p>Given that it is almost impossible to use the library and exhaust its resources, a function is needed.

Possible solutions

The following CRC calculation was found online. However, its implementation is not Correct.

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 +=''
#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< br /> reg ^= poly
return reg

Existing online solutions

The following link calculates the 16-bit CRC correctly.

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

The result under “CRC-CCITT(XModem)” is the correct CRC.

Specifications< /p>

I believe that the “CRC-CCITT(XModem)” in the existing online solution uses the polynomial 0x1021.

Question

If someone can write a new function or Provide directions to resolve the checkCRC function to the required specification. Please note that using a library or any import will not help.

This is a python port of the C library for CRC-CCITT XMODEM from http://www.lammertbies.nl/comm/info/crc-calculation.html

this The library is very interesting for practical use cases because it pre-computes a crc table to improve speed.

Usage (with string or byte list):

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

The test gives: ‘0x31c3’

< p>

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
< br /> tmp = (crc >> 8) ^ cc
crc = (crc << 8) ^ _tab[tmp & 0xff]
crc = crc & 0xffff
print (crc)< br />
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

If poly = 0x1021 replaces poly = 0x11021 at the beginning, the recommended checkCRC routine is the CRC-CCITT variable ‘1D0F’.

Question

I am writing code for embedded devices. Many solutions for CRC-CCITT 16-bit calculations require libraries.

Given It is almost impossible to use the library and exhaust its resources, a function is needed.

Possible solutions

The following CRC calculation was found online. However, its implementation is incorrect.

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 +=' '
#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 solutions

The following link calculates the 16-bit CRC correctly.

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

“CRC-CCITT(XModem )” The result is the correct CRC.

Specifications

I believe the calculation of “CRC-CCITT(XModem)” in the existing online solutions uses the polynomial 0x1021.

Question

If someone can write a new function or provide directions to solve the checkCRC function to the required specification. Please note that using a library or any import will not help.

This is a python port of the C library for CRC-CCITT XMODEM starting from http://www.lammertbies.nl/comm/info/crc-calculation.html

< /p>

This library is very interesting for practical use cases, because it pre-calculates a crc table to improve speed.

Usage (with string or byte list):

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

The test gives:’ 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

If poly = 0x1021 replace poly = 0x11021 at the beginning , The recommended checkCRC routine is the CRC-CCITT variable ‘1D0F’.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5317 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.