Talking to your vehicle over the CAN bus with Python
When properly configured and connected to your vehicle's OBD-II port, this Python3 program retrieves the following information from the vehicle:
- The specific OBD standard this vehicle conforms to
- The vehicle's current fuel tank level
- The time, in seconds, since the engine was powered on
Prerequisites for success:
- A vehicle powered on (motor on)
- A CAN connection to the OBD-II CAN bus from your computer
python3
(sudo apt install python3
)
python3
(https://www.python.org/downloads/windows/)- CAN driver (Kvaser, PCAN, etc.)
pip3 install -r requirements.txt --user
OBD-II to DB9 Cable | CAN USB Adapter |
---|---|
See the doc string in can_we_talk.py or run ./can_we_talk.py -h
for usage
specifics.
On a Linux system socketcan
is a default so can_we_talk.py
can rely on the default settings
for bustype
and channel
. After initializing the socketcan interface with:
sudo ip link set can0 type can bitrate 500000
sudo ip link set up can0
you can run:
# Default Linux usage
./can_we_talk.py
On a Windows system socketcan
is not available so the bustype
and channel
must be specified.
If you've installed the Kvaser SDK you need to run:
# Default Kvaser CANlib usage
./can_we_talk.py -c 0 -b kvaser
Using PCAN drivers you can run:
# Default PEAK PCAN-USB usage
./can_we_talk.py -c PCAN_USBBUS1 -b pcan
You can run the tests with pytest
, i.e. python3 -m pytest --verbose
This project supplemented a talk titled CAN we talk Python? Talking to your vehicle over the CAN bus with Python, first given at the PDX Python User Group in July of 2018. You can watch the video here.