Skip to content

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.

Info

Ensure that the MIO04 CAN bus configuration matches your network settings. You can set a persistent configuration as described in the CAN API demo.

Prerequisites

Hardware

  • A Moducop Edge Computer with a MIO04 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 MIO04

Bus Configuration

Bus configuration must be set once to tell the MIO04 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 (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 MIO04-1 set-parameter can-config 125000:625:1:0
# Restart to apply parameters
io4edge-cli -d MIO04-1 restart

Connecting

CAN connection

Connect CAN_L, CAN_H and GND_ISO to the CAN bus. Ensure proper termination of 120 ohms at each end of the line.

Create a SocketCAN Instance

To access the MIO04 via SocketCAN, create a virtual SocketCAN network that matches the service name of your MIO04 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-IOU04-USB-EXT-1-can → vcan name vcanS101xxEXT-1
  • Service name 123456789012-can → vcan name vcan1234xx89012
  • Service name MIO04-1-can → vcan name vcanMIO04-1

Create a virtual SocketCAN network. On your Moducop, run:

ip link add dev vcanMIO04-1 type vcan
ip link set up vcanMIO04-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 vcanMIO04-1 vcanMIO04-1,1FFFFFFF:1FFFFFFF,#FFFFFFFF -e
  vcanMIO04-1  6B6   [5]  37 67 2F 0F F2
  vcanMIO04-1  24A   [6]  B1 39 8A 3A A5 77
  vcanMIO04-1  57C   [5]  01 B2 9F 37 22
  vcanMIO04-1  665   [8]  1C C2 60 0A 8E E3 85 42
  vcanMIO04-1  18B   [5]  B0 E5 E4 2E 24
  vcanMIO04-1  0D0   [8]  64 49 45 71 6B B2 6E 09
  vcanMIO04-1  146   [7]  2E A6 CF 44 1A E9 2A
  vcanMIO04-1  508   [0]
  vcanMIO04-1  726   [8]  B0 B5 2E 62 70 89 78 4F
  vcanMIO04-1  454   [2]  15 01