Skip to content

Instantly share code, notes, and snippets.

@helo9
Created August 15, 2024 09:47
Show Gist options
  • Save helo9/da57140e61159e8787a4c4e56436b94c to your computer and use it in GitHub Desktop.
Save helo9/da57140e61159e8787a4c4e56436b94c to your computer and use it in GitHub Desktop.
Read Peak PCAN Traces in Python
import binascii
import re
def read_pcan_trace(trace_path: str):
with open(trace_path, "r") as candump:
c = 0
HEADER_LINES_COUNT = 14
regex = re.compile(
r"\s*(?P<index>\d+)\)\s+(?P<time>(?:\d+)\.(?:\d+))\s+Rx\s+(?P<can_id>[0-9a-zA-Z]+)\s+(?P<datalen>\d+)\s+(?P<data>(?:[0-9a-zA-Z]{2})(?:\s+[0-9a-zA-Z]{2}){0,7})")
data = []
for line in candump.readlines()[HEADER_LINES_COUNT:]:
if line.startswith(";"):
# comment
continue
res = regex.match(line)
if res == None:
print("Failed to parse {}", line)
continue
data.append((
float(res.group("time")),
int(res.group("can_id"), base=16),
binascii.unhexlify(res.group("data").replace(" ",""))
))
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment