SocketCAN Demo
In this demo we demonstrate how to receive data from a CAN bus and print it to the console using the Linux SocketCAN framework.
Warning
On the IOU06, the CAN and COM ports cannot be used at the same time. To enable CAN, set a persistent bus configuration. If you want to use the COM ports, remove the persistent bus configuration.
Prerequisites
Hardware
- A Moducop Edge Computer with a IOU06 installed
-
A development PC (Windows or Linux), connected via Network to the Moducop
-
CAN bus with at least one CAN device
- Cable to connect the CAN device with the IOU06
Bus Configuration
Bus configuration must be set once to tell the IOU06 about the CAN parameters. This configuration is saved persistently in the device. You only need to perform this configuration step once; it survives reboots and power cycles.
In this example, we assume your
- Bitrate is 125 kBit
- Sampling Point is 0.625 (62.5%)
- Synchronization Jump Width is 1
- Listen only mode is off (i.e. Normal operation)
Configure the device. You specify a string that has the form bitrate:sampling-point/1000:sjw:listen-only
On your Moducop, run:
io4edge-cli -d S101-IOU06-USB-EXT-1 set-parameter can-config 125000:625:1:0
# Restart to apply parameters
io4edge-cli -d S101-IOU06-USB-EXT-1 restart
Connecting (Rev. 0)
Connect CAN_L, CAN_H and GND_ISO to the CAN bus. Be sure to have correct termination of 120R at each end of the line.
Connecting (Rev. 1)
Connect CAN_L, CAN_H and GND_ISO to the CAN bus. Be sure to have correct termination of 120R at each end of the line.
Create a SocketCAN Instance
To access the IOU06 via SocketCAN, create a virtual SocketCAN network that matches the service name of your IOU06 CAN interface.
The virtual SocketCAN network must be named according to the service name of the CAN interface. If the service name is MYDEV-can, the virtual SocketCAN device must be named vcanMYDEV (without -can). Because network interface names can have only 15 characters, but service names can be longer, use this mapping:
vcan<first-4-chars-of-service-name>xx<last-5-chars-of-service-name>
Examples:
- Service name
S101-IOU06-USB-EXT-1-can→ vcan namevcanS101xxEXT-1 - Service name
123456789012-can→ vcan namevcan1234xx89012 - Service name
MIO04-1-can→ vcan namevcanMIO04-1
Create a virtual SocketCAN network. On your Moducop, run:
ip link add dev vcanS101xxEXT-1 type vcan
ip link set up vcanS101xxEXT-1
Function Test
Using the candump tool (part of the can-utils package), you should see all frames that are sent on the CAN bus. Example (also dumps error information from the bus):
./candump vcanS101xxEXT-1 vcanS101xxEXT-1,1FFFFFFF:1FFFFFFF,#FFFFFFFF -e
vcanS101xxEXT-1 6B6 [5] 37 67 2F 0F F2
vcanS101xxEXT-1 24A [6] B1 39 8A 3A A5 77
vcanS101xxEXT-1 57C [5] 01 B2 9F 37 22
vcanS101xxEXT-1 665 [8] 1C C2 60 0A 8E E3 85 42
vcanS101xxEXT-1 18B [5] B0 E5 E4 2E 24
vcanS101xxEXT-1 0D0 [8] 64 49 45 71 6B B2 6E 09
vcanS101xxEXT-1 146 [7] 2E A6 CF 44 1A E9 2A
vcanS101xxEXT-1 508 [0]
vcanS101xxEXT-1 726 [8] B0 B5 2E 62 70 89 78 4F
vcanS101xxEXT-1 454 [2] 15 01