-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcan.py
More file actions
40 lines (32 loc) · 1017 Bytes
/
can.py
File metadata and controls
40 lines (32 loc) · 1017 Bytes
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
"""
Pipe CAN bus frames into Plexus.
Prereq:
pip install plexus-python python-can cantools
sudo ip link set can0 type can bitrate 500000 && sudo ip link set can0 up
export PLEXUS_API_KEY=plx_xxx
Run:
python can.py can0 [vehicle.dbc]
"""
import sys
import can
import cantools
from plexus import Plexus
channel = sys.argv[1] if len(sys.argv) > 1 else "can0"
dbc_path = sys.argv[2] if len(sys.argv) > 2 else None
px = Plexus(source_id="vehicle-001")
bus = can.interface.Bus(channel=channel, bustype="socketcan")
db = cantools.database.load_file(dbc_path) if dbc_path else None
for msg in bus:
if db:
try:
decoded = db.decode_message(msg.arbitration_id, msg.data)
for name, value in decoded.items():
if isinstance(value, (int, float)):
px.send(name, value)
continue
except KeyError:
pass
px.send(
f"can.raw.0x{msg.arbitration_id:x}",
int.from_bytes(msg.data, "big"),
)