-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_can.py
41 lines (32 loc) · 1.01 KB
/
decode_can.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
37
38
39
40
41
import can
import cantools
from cantools import database
def decode_can_message(msg, db:database.Database):
decoded_msg = db.decode_message(msg.arbitration_id, msg.data)
return (decoded_msg)
def main(dbc_file_path):
# Load the DBC file
db = cantools.db.load_file(dbc_file_path)
# Initialize the CAN interface
bus = can.Bus(interface='pcan', channel='PCAN_USBBUS1', bitrate=500000)
# Main loop to receive and decode CAN messages
while True:
try:
# Receive a CAN message with a timeout of 1 second
msg = bus.recv(timeout=1.0)
if msg is not None:
try:
print(decode_can_message(msg, db))
except:
pass
except KeyboardInterrupt:
break
# Cleanup
bus.shutdown()
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
print("Usage: decode_can.py <dbc_file_path>")
else:
dbc_file_path = sys.argv[1]
main(dbc_file_path)