Skip to content

CAN Demo using io4edge API

In this demo we demonstrate how to receive data from a CAN bus and print it to the console by accessing the MIO04 CAN interface via the io4edge API.

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

Tools on Development PC

The examples are written in programming language Go, we download the go sources and compile them. Therefore we need some tools on your development PC:

Get Demo Software

Clone the repository containing the examples to a folder of your choise (here myworkdir).

c:
cd \myworkdir
git clone https://github.com/ci4rail/io4edge-client-go.git
cd io4edge-client-go
cd ~/myworkdir
git clone https://github.com/ci4rail/io4edge-client-go.git
cd io4edge-client-go

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.

Demo Software

Compile Demo

Run this in a powershell console

cd examples\canL2\streamDump
$Env:GOOS = "linux"
$Env:GOARCH = "arm64"
go build
cd examples/canL2/streamDump
GOOS=linux GOARCH=arm64 go build

This produces the binary file streamDump in the current folder.

Copy Demo to Moducop

Transfer the compiled binary. Replace <target-ip> with the IP address of your Moducop.

We copy the binary to the /data folder of ModuCop, as this is a writeable, whereas the rest of the filesystem is write protected.

scp streamDump root@<target-ip>:/data

Running the Demo

Login into your Moducop over SSH:

ssh root@<target-ip>

Once logged in into the Moducop's Shell, run the demo.

In case your MIO04 is not in the slot next to the CPU, use a different address, e.g. S101-MIO04-USB-EXT-2-can

/data/streamDump MIO04-1-can

You should now see all frames that are sent on the CAN bus. Example:

Started stream
got stream data with 3 samples
  @56036918665 us: ID:7ff DATA:22 33 44  ERROR:CAN_NO_ERROR STATE:CAN_OK
  @56037270984 us: ID:123 DATA:22 33 44  ERROR:CAN_NO_ERROR STATE:CAN_OK
  @56037462740 us: ID:456 DATA:22 33 44  ERROR:CAN_NO_ERROR STATE:CAN_OK
got stream data with 3 samples
  @56038232385 us: ID:222 DATA:22 33 44  ERROR:CAN_NO_ERROR STATE:CAN_OK
  @56038422757 us: ID:334 DATA:22 33 44  ERROR:CAN_NO_ERROR STATE:CAN_OK
  @56038632969 us: ID:555 DATA:22 33 44  ERROR:CAN_NO_ERROR STATE:CAN_OK
...