add checksum

This commit is contained in:
Junzi Sun 2015-09-17 23:45:35 +02:00
parent 2b55f8a5df
commit d15538296b

View File

@ -25,7 +25,6 @@ MODES_CHECKSUM_TABLE = [
0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000
] ]
def hex2bin(hexstr): def hex2bin(hexstr):
"""Convert a hexdecimal string to binary string, with zero fillings. """ """Convert a hexdecimal string to binary string, with zero fillings. """
length = len(hexstr) * 4 length = len(hexstr) * 4
@ -34,41 +33,34 @@ def hex2bin(hexstr):
msgbin = '0' + msgbin msgbin = '0' + msgbin
return msgbin return msgbin
def bin2int(msgbin): def bin2int(binstr):
return int(msgbin, 2) return int(binstr, 2)
def hex2int(hexstr):
return int(hexstr, 16)
def checksum(msg): def checksum(msg):
nbits = len(msg) if len(msg) == 28:
if nbits == 28:
offset = 0 offset = 0
elif nbits == 14: elif len(msg) == 14:
offset = 112-56 offset = 112-56
else: else:
# raise exception # raise exception
return False return False
print msg
msgbin = hex2bin(msg) msgbin = hex2bin(msg)
checksumhex = msg[22:28] checksum = int(msg[22:28], 16)
checksum = int(checksumhex, 16)
crc = 0 crc = 0
for i in xrange(nbits): for i in xrange(len(msgbin)):
if int(msgbin[i]): if int(msgbin[i]):
crc ^= MODES_CHECKSUM_TABLE[i+offset] crc ^= MODES_CHECKSUM_TABLE[i+offset]
print bin(crc)
print bin(checksum)
print
if crc == checksum: if crc == checksum:
return True return True
else: else:
return False return False
def get_df(msg): def get_df(msg):
"""Decode Downlink Format vaule, bits 1 to 5.""" """Decode Downlink Format vaule, bits 1 to 5."""
msgbin = hex2bin(msg) msgbin = hex2bin(msg)