This article will show you how to install Python-CAN for the Raspberry Pi for use with the PiCAN2 board.

Before the Python-CAN install make sure the PiCAN2 software is install first.

Reboot and bring up the can0 interface:

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

Now download the Python-CAN files:

https://bitbucket.org/hardbyte/python-can/get/4085cffd2519.zip

Unzip and install by

sudo python3 setup.py install

Now start python3

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)

 

To received messages and display on screen type:

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