
CAN Bus Configuration
CAN Bus
The BOAT IO and NMEA-4 CAN buses both uses the Microchip MCP2515. This is natively supported by the Raspberry Pi overlay, but has to be configured. Follow the steps below.
Step 1 - Install Updates
pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get upgrade
pi@raspberrypi ~ $ sudo reboot
Step 2 - Enable a CAN device tree
pi@raspberrypi ~ $sudo nano /boot/config.txt
Edit the pi configuration and add an overlay for the device
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=25
dtoverlay=spi0-bcm2835-overlay
Reboot
Step 3 - Download the CAN bus test software
Download the test software from http://www.skpang.co.uk/dl/can-test_pi2.zip
Extract the files to a folder
There are various programs in the folder, but the two most useful are candump and cansend
To make them executable use the commands
pi@raspberrypi ~ $ chmod u+x cansend
pi@raspberrypi ~ $ chmod u+x candump
Step 4 - Configure the CAN Bus
pi@raspberrypi ~ $ sudo /sbin/ip link set can0 up type can bitrate 500000
This sets up the CAN bus for 500k bps. Change this number to match your bus speed
Now monitor traffic on the bus using the command
pi@raspberrypi ~ $ ./candump can0
To send data issue the command
pi@raspberrypi ~ $ ./cansend can0 000007DF#0201050000000000
Step 5 - Programming with Python
Download the Python-CAN files from:
https://bitbucket.org/hardbyte/python-can/get/4085cffd2519.zip
Unzip and install by entering:
pi@raspberrypi ~ $ sudo python3 setup.py install
Bring the CAN interface up if it is not already done:
pi@raspberrypi ~ $ sudo /sbin/ip link set can0 up type can bitrate 500000
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], extended_id=False)
bus.send(msg)