Last active
August 29, 2015 14:02
-
-
Save mba7/1db47f979471b70f48e4 to your computer and use it in GitHub Desktop.
crc16 computer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################# 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