Installing Python-CAN for Raspberry Pi

05/12/2015

This blog will show you how to install Python-CAN for the Raspberry Pi 2 for use with the PiCAN board.

Before the install use latest Raspbian Jessie, currently it is (2015-11-21) Kernel 4.1

Do an update first.

sudo apt-get update 
sudo apt-get upgrade 
sudo reboot 

Add the overlays by:

sudo nano /boot/config.txt

Add these 3 lines to the end of file:

dtparam=spi=on 
dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25 
dtoverlay=spi-bcm2835-overlay

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()])

 

 

Short URL:

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*