-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcan_dump.py
executable file
·36 lines (35 loc) · 986 Bytes
/
can_dump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
import can
import csv
import datetime
import sys
if len(sys.argv) > 2:
arg1 = sys.argv[1].lower()
if arg1 == 'pcan':
# PCAN
interface = 'pcan'
channel = 'PCAN_USBBUS1'
elif arg1 in ['can1', 'can0']:
# pican
channel = arg1
interface = 'socketcan'
else:
exit("Invalid CAN bus specified")
channel = "can1"
interface = "socketcan_native"
with open('/home/pi/log/can-dumps/can_dump_{}.csv'.format(datetime.datetime.now().timestamp()), 'w') as outf:
fields = ['seq', 'arb_id', 'bytes']
i = 0
writer = csv.DictWriter(outf, fieldnames=fields)
writer.writeheader()
bus = can.interface.Bus(channel=channel, bustype=interface)
while True:
i +=1
msg = bus.recv()
row = {
'seq': i,
'arb_id': hex(msg.arbitration_id),
'bytes': ','.join(hex(x) for x in msg.data)
}
print(row)
writer.writerow(row)