Setting up CAN on Raspberry Pi
The easiest way to work with CAN bus in Linux is the can-utils program set. Install it with the following command:
sudo apt-get install can-utils
Can-utils consists of the following tools:
candump - allows to print, filter and record all data that is being received by a CAN interface
canplayer - playbacks recorded frames
cansend - transmits CAN frame
cangen - generates random CAN frames
canbusload - displays current CAN bus load
We will mainly use the candump and cansend utilities.
To start the CAN interface, enter the command
sudo ip link set can0 up txqueuelen 65535 type can bitrate 1000000
The bitrate field specifies interface speed, 1000000 bit/s in this example.
In case of "Cannot find device "can0"" error, do the following:
sudo nano /boot/config.txt
Append the string
dtoverlay=seeed-can-fd-hat-v2

Save (Ctrl+S) and close (Ctrl+X) the file. Reboot the raspberry:
sudo reboot
To view frames on the CAN0 bus use the candump command
candump can0
This will print all frames. In case you want to see frames with specific ID, use the command
candump can0,<ID>:7ff
. It uses hexadecimal numerals. ID=100 decimal will be ID=0x64 hexadecimal (100=0х64). For this ID the command takes the following form:
candump can0,064:7ff
To send frames to the bus use cansend. Sending 4 bytes (0xDE 0xAD 0xBE 0xEF) with message ID=100(0x64) looks like this:
cansend can0 00000064#DEADBEEF
To disable the interface use
sudo ifconfig can0 down
CAN-FD
CAN FD is the second generation of CAN protocol developed by Bosch. The basic idea is to overclock part of the frame and to oversize the payload. Developed in 2011 and released in 2012 by Bosch, CAN FD was developed to meet the need to increase the data transfer rate up to 5 times faster and with larger frame/message sizes. To configure CAN interface with FD mode on, use the following command:
sudo ip link set can0 up txqueuelen 65535 type can bitrate 1000000 dbitrate 8000000 fd on
In addition to bitrate there's dbitrate which specifies the speed of data segment in frame.
From here on we will use FD mode by default.
Let's set up automatic CAN bus configuration when Raspberry starts. To do so we will write a little script canup.sh and place it in home directory
sudo nano canup.sh
#!/bin/sh
sudo ip link set can0 up txqueuelen 65535 type can bitrate 1000000 dbitrate 8000000 fd on
To make the file executable:
sudo chmod +x canup.sh
To launch application when Raspberry starts you have to specify it in /etc/rc.local
sudo nano /etc/rc.local
In the nano editor window you will see lines of code. Find the exit 0
line and put the following command before it:
sudo bash /home/pi/canup.sh

Save (Ctrl + S) and close (Ctrl + X) the editor. Reboot the Raspberry
sudo reboot
It is useful to know the command
ip -details link show can0
This command allows you to get information about the CAN0 interface. Note that if there are no errors when sending or receiving messages, you will see 0 in the error counter, as shown below

Last updated