You have different possibilities to control your RevPi Con CAN module. In this tutorial we will show you what these are. But before we can control the RevPi Con CAN, we must first configure the device in the Linux device tree.
Prerequisites
- Your RevPi ConCAN is connected to your RevPi Connect.
- Your RevPi Connect is connected to the power supply.
- Your Rev Pi Connect is connected to the Internet or your network.
- You know the IP address of your RevPi Connect.
Let’s go!
- Start PiCtory.
- Drag a RevPi Connect and a RevPi Con CAN onto the virtual DIN rail.
- Save this file.
- Reboot your RevPi Connect.
- Log in on the command line.
The RevPi Con CAN module is now visible in the system as a network interface. Usually it is given the name “can0”. DIt is accessed via the SocketCAN layer.
Configuring the interface
Before you can access the interface, you need to configure a bitrate and bring up the interface.
The bit rate defines the speed of data transmission on an interface. For the CAN bus you could use a bitrate up to 1 MBit/s. However, you must consider the line length of your CAN bus. If you have a very long line and set a fast bit rate, some devices may not receive the information sent. This is because the signal is too short to reach all devices in your network within the specified time.
We recommend that you set these bit rates for the corresponding line length:
Line length | Bitrate |
40 meters | 1000 kBit/s |
100 meters | 500 kBit/s |
500 meters | 100 kBit/s |
1000 meters | 50 kBit/s |
If you want to use 1 MBit/s as bitrate, for example, you have to write the following commands to the command line:
sudo ip link set can0 type can bitrate 1000000
sudo ip link set can0 up
Control on the command line
With the help of the pre-installed can-utils you can:
- Send a single data package
cansend can0 123#1122334455667788
- Sending data packets with random data in an endless loop
cangen can0 -g 0 -i -x
- Displaying Received Data Packets
candump can0
- Monitoring whether bit errors occur during transmission
ip -details -statistics link show can0
If bit errors occur frequently, the interface first goes into the “Error Passive” and then into the “Bus Off” error state. In the “Bus Off” error state, the interface no longer sends and receives any packets. You must reset the interface to the normal state with the following command:
sudo ip link set can0 type can restart
It is also possible to automatically return to the normal state after a waiting time:
sudo ip link set can0 down
sudo ip link set can0 type can restart-ms 100
sudo ip link set can0 up
Control with Python and C/C++
With the pre-installed python-can library you can easily access the interface from Python scripts. You set the bitrate when opening the interface with the can.interface.Bus() constructor.
For C/C++ use the libsocketcan library.
You can install API documentation in HTML format with the packages python-can-doc and libsocketcan-doc. If you are using a communication matrix , the packages python-canmatrix, python3-canmatrix and canmatrix-utils might also be interesting for you.
Recording CAN communication
You can record the CAN communication with tcpdump to analyze it on another computer with wireshark.
Enter the following command in the command line:
sudo tcpdump -i can0 -w out.pcap
Alternatively, you can install wireshark on your RevPi Connect and view it locally, or redirect screen output to a remote machine using the X Windows protocol or VNC.