Skip to content

Installing python-can for Raspberry Pi

This blog shows the steps required to install python-can on the Raspberry Pi for use with PiCAN2, PiCAN3, PiCAN-M boards.

First make sure the driver is installed.

Now install python-can:

pip3 install python-can

Check there is no error.

You can now bring the CAN interface up:

sudo /sbin/ip link set can0 up type can bitrate 500000

Check there is no error message returned. Now we can try the interactive python shell. Start typing at the prompt:

python3

To sent a message out type the following lines:

import can

bus = can.interface.Bus(channel='can0', bustype='socketcan_native') 

msg = can.Message(arbitration_id=0x7de,data=[0, 25, 0, 1, 3, 1, 4, 1])

bus.send(msg)

Check CAN frames on the bus.

To received messages and display on screen type:

notifier = can.Notifier(bus, [can.Printer()])

 More python can examples at:

https://github.com/skpang/PiCAN-Python-examples