Skip to content

Instantly share code, notes, and snippets.

@projectgus
Created September 24, 2021 06:49
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 projectgus/7146a2f4b252759226a58179e590b18e to your computer and use it in GitHub Desktop.
Save projectgus/7146a2f4b252759226a58179e590b18e to your computer and use it in GitHub Desktop.
Quick script to check no CAN messages were dropped - see https://github.com/hardbyte/python-can/pull/1127
#!/usr/bin/env python
import csv
import base64
import re
import struct
last_counter = [None, None]
lines = 0
with open("spammer.txt") as f:
for l in f:
lines += 1
m = re.match(r'Timestamp: *([\d\.]+) *ID: ([\da-f]+) *([S]) (Rx)? *DLC: *(\d+) *([\da-f ]{23}) *Channel: (\d)', l)
if m:
timestamp, canid, idtype, _, dlc, data, channel = m.groups()
timestamp = int(float(timestamp) * 1e6) # to microseconds
dbytes = bytes.fromhex(data)
extended = idtype != 'S' # bit hacky
canid = int(canid, 16)
channel = int(channel)
# print(timestamp, channel, dbytes.hex())
counter = struct.unpack("<Q", dbytes)[0]
if last_counter[channel] is not None:
if last_counter[channel] + 1 != counter:
print(f"Line {lines}. Channel {channel}. Expected counter {hex(last_counter[channel]+1)}, got {hex(counter)}")
last_counter[channel] = counter
print(f"Done! {lines} lines. Final counters {last_counter}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment