Skip to content

Instantly share code, notes, and snippets.

@mba7
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mba7/1db47f979471b70f48e4 to your computer and use it in GitHub Desktop.
Save mba7/1db47f979471b70f48e4 to your computer and use it in GitHub Desktop.
crc16 computer
################# Compute CRC16 from buffer ###############
""" usage: add : from crc16 import crc16
call crc16(vCRCList)
"""
def crc16(packet):
POLY = 0x8408 # 0x8408 is deduced from the polynomial X**16 + X**12 + X**5 + X**0
# process each byte
vFCS = 0xFFFF; # init FCS to all ones
for byte in packet:
vFCS ^= ord(byte) # attention au XOR...
# process each bit of the current byte
for x in range(8):
if (vFCS & 1):
vFCS = (vFCS>>1) ^ POLY;
else:
vFCS >>= 1;
vFCS ^= 0xFFFF; # finally invert vFCS
#print "\nvFCS final = %x" % (vFCS)
return vFCS
if __name__ == '__main__':
vFrm = ['\x00', '\x00', '\x00', '\x80']
calccrc = crc16(vFrm) #send frame to function (without the crc!)
print "calccrc %x" % calccrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment