Skip to content

Instantly share code, notes, and snippets.

@darksidelemm
Created April 8, 2019 04:00
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 darksidelemm/514a4b0299231c896b91385fd6d0f8ec to your computer and use it in GitHub Desktop.
Save darksidelemm/514a4b0299231c896b91385fd6d0f8ec to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import crcmod
def check_crc(data, crc):
crc16 = crcmod.predefined.mkCrcFun('crc-ccitt-false')
_crc = crc16(data)
# Convert incoming CRC to a int
_crc_in = ord(crc[0]) + ord(crc[1])*256
if _crc_in == _crc:
return True
else:
return False
data = "8635F44093DF1A600EF140A063BD8050B4004CBF43AFC40DECC8572C5B6C82CE57A8D69BDEEB097BD0E40BA12F6282CD6DD78291C6D66AF40F7928F91852303233303535361A00000300031300002600073232DD768C015D020700F9F5BC011B170000900980A7E0F00B450FBAB82EA91F4BC3A9909DA992320AF6DB0071A0C5706D03C4DC43B52E1EF11174702367EAD95460579740BD3CC076676FB6392629C161FE1F4EC78176CA2CAF1A52311EE3AB8EC092BA4137A0DF09E3A6DFA6CD4A14D0C9BD7DA6A5968C6385EB9C08BA70107156D39C3C7A1547C8198A471B7BEC2108D35853D7CB1DAC2DF83943019DB72452C22C791FC4CE2D1075FF29DA3CF82FAF47A6810148970B5064BCFFA84A2C762C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000789A"
_data = data.decode('hex')
_header = _data[:0x08]
_ecc = _data[0x08:0x38]
if _data[0x38] != '\x0f':
print("Unknown frametype flag.")
_frames = _data[0x39:]
# Extract frame blocks.
i = 0
frames = []
while i < len(_frames):
_type = _frames[i+0].encode('hex')
_len = ord(_frames[i+1])
_content = _frames[i+2:i+2+_len]
_crc = _frames[i+2+_len:i+2+_len+2]
_crc_ok = "OK" if check_crc(_content, _crc) else "BAD"
print("Got frame type: %s, length: %d, CRC:%s" % (_type, _len, _crc_ok))
frames.append({'type':_type, 'len':_len, 'content':_content.encode('hex'), 'crc':_crc})
i += _len+4
for _frame in frames:
print(_frame)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment