Currently refactoring to implement proper Python packaging.
Install the tool along with dependencies (python-can) with: python setup.py install
.
You still need to configure your CAN interface per the instructions below.
Package installation tested on:
- Python 3.10
- Python 3.6 - 3.9
- Python 2.7
The setup consists of two steps. First we need to get the USB-to-Can device working and secondly configure Python-Can for the device.
-
Install a reasonably modern Linux dist (with a kernel >= 3.4) e.g. latest Ubuntu. Tested with Ubuntu 14.04 LTS, Ubuntu 16.04 LTS and Ubuntu 17.04.
-
Plug in the USB-to-Can device (we use PEAK PCAN-USB)
-
Load the CAN module (needs to be done after each reboot):
sudo modprobe can
-
Set up the can device (needs to be done after each reboot).
sudo ip link set can0 up type can bitrate 500000
Note: The bit rate may differ between different CAN buses.
-
can0
will now display as a normal network interface
Tested with python-can 1.5.2 and 2.0.0a2.
-
Install pip (package management system for python - often installed by default)
-
Install python-can by running:
pip install python-can
-
Verify that the installation worked by running python from the terminal and load the can module.
$ python Python 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import can >>>
Python-Can uses a configuration file ~/.canrc
to specify a CAN interface.
The contents of this file might e.g. be:
[default]
interface = socketcan
channel = can0
Run the following command:
cc.py dump
If packets are received, everything is good to go!
The simplest solution is to download VMware Player , install a Linux distribution and to follow the Linux guide above.
- Raspberry Pi model B
- SD-card 8 GB
- piCAN-shield
- DBUS 9 male
- 2 wires
NOTE! The install procedure was more complex earlier - if you are using an older Raspbian, try looking into older versions of this howto. Regarding HW, this blogpost details a couple of interesting alternatives
- Download and flash latest raspbian image to SD card.
We used
http://downloads.raspberrypi.org/raspbian/images/raspbian-2017-07-05/
- Update, upgrade & reboot
sudo apt-get update
sudo apt-get upgrade
sudo reboot
- Enable kernel module for spi by;
- either using the Raspberry Pi Configuraton tool, available from the Raspbian desktop
- or editing
/boot/config.txt
and addingdtparam=spi=on
- Enable kernel module for the mcp2515 chipset (CAN controller) on piCAN by adding
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
to/boot/config.txt
- Add
dtoverlay=spi-bcm2835-overlay
to/boot/config.txt
, because everyone says it's the right thing to do
sudo reboot
Configure CAN interface and bring it up
sudo ip link set can0 up type can bitrate 500000
See instructions above in the Linux guide