Files
dragonpilot/selfdrive/debug/can_printer.py

46 lines
1.6 KiB
Python
Raw Normal View History

2020-01-17 11:23:21 -08:00
#!/usr/bin/env python3
2021-10-11 19:47:49 -07:00
import argparse
2020-01-17 11:23:21 -08:00
import binascii
import time
2020-01-17 11:23:21 -08:00
from collections import defaultdict
import cereal.messaging as messaging
2022-01-13 15:50:50 +01:00
def can_printer(bus, max_msg, addr, ascii_decode):
2020-01-17 11:23:21 -08:00
logcan = messaging.sub_sock('can', addr=addr)
start = time.monotonic()
lp = time.monotonic()
2020-01-17 11:23:21 -08:00
msgs = defaultdict(list)
while 1:
can_recv = messaging.drain_sock(logcan, wait_for_one=True)
for x in can_recv:
for y in x.can:
2021-10-11 19:47:49 -07:00
if y.src == bus:
2020-01-17 11:23:21 -08:00
msgs[y.address].append(y.dat)
if time.monotonic() - lp > 0.1:
2020-01-17 11:23:21 -08:00
dd = chr(27) + "[2J"
dd += f"{time.monotonic() - start:5.2f}\n"
2021-10-11 20:03:35 -07:00
for addr in sorted(msgs.keys()):
2022-01-13 15:50:50 +01:00
a = f"\"{msgs[addr][-1].decode('ascii', 'backslashreplace')}\"" if ascii_decode else ""
2021-10-11 20:03:35 -07:00
x = binascii.hexlify(msgs[addr][-1]).decode('ascii')
freq = len(msgs[addr]) / (time.monotonic() - start)
2021-10-11 20:03:35 -07:00
if max_msg is None or addr < max_msg:
2022-05-12 17:07:32 -07:00
dd += "%04X(%4d)(%6d)(%3dHz) %s %s\n" % (addr, addr, len(msgs[addr]), freq, x.ljust(20), a)
2020-01-17 11:23:21 -08:00
print(dd)
lp = time.monotonic()
2020-01-17 11:23:21 -08:00
if __name__ == "__main__":
2021-10-12 20:17:12 -07:00
parser = argparse.ArgumentParser(description="simple CAN data viewer",
2021-10-11 19:47:49 -07:00
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
2021-10-11 20:41:28 -07:00
parser.add_argument("--bus", type=int, help="CAN bus to print out", default=0)
2022-01-13 15:50:50 +01:00
parser.add_argument("--max_msg", type=int, help="max addr")
parser.add_argument("--ascii", action='store_true', help="decode as ascii")
2021-10-11 19:47:49 -07:00
parser.add_argument("--addr", default="127.0.0.1")
args = parser.parse_args()
2022-01-13 15:50:50 +01:00
can_printer(args.bus, args.max_msg, args.addr, args.ascii)