Using the CAN bus from linux shell

Difficulty Levels: Intermediate
Date added: June 27, 2018
Affected Products: EDM-IMX7 , EDM1-CF-IMX6 , EDM1-CF-IMX6SX , EDM1-IMX6UL , EDM2-IMX6PLUS , PICO-IMX6 , PICO-IMX6UL , PICO-IMX6UL-NAND , PICO-IMX6UL-NOR , PICO-IMX7 , TAM-3517 , TC-0700 , TC-0710 , TC-1010 , TEK3-IMX6 , TEK3-IMX6UL , TEP-0500-IMX6UL , TEP-0500-IMX7 , TEP-0700-IMX6UL , TEP-0700-IMX7 , TEP-1010-IMX6 , TEP-1560-IMX6 , THB-3517

Summary

This guide shows how to send and receive CAN frames using linux command line on TechNexion SOMs.

Introduction and Setup

Many TechNexion products have two CAN buses. The numbering of these can be a little confusing, since hardware manuals and
schematic uses a 1-based numbering (CAN1 and CAN2), while the linux kernel starts the numbering at zero (can0 and can1).

In this article, upper case CAN refers to hardware, and lower case can0 or can1 to the network interface provided by linux kernel.

One of the simplest tests for CAN functionality is to send a can frame from CAN1 to CAN2. It is here assumed that the two CAN buses
are loopbacked so data sent on one port is received on the other.

If the product lacks CAN transceivers (transforming RX/TX based signal to differential), the loopback cabling should kept as short as possible.

Linux Commands

The commands used are ip, cansend and candump.

First set up the can interfaces with

# ip link set can0 type can bitrate 125000
# ifconfig can0 up

and

# ip link set can1 type can bitrate 125000
# ifconfig can1 up

Now the buses are ready and set to operate at 125kbps. If needed, triple sampling can also be enabled with the ip tool.

To receive a CAN frame on can0, use the command
# candump can0 &

This will display any received frames on the terminal.

Then send a test frame on can1:
# cansend can1 123#CAFEBABE
and the frame should be printed on the terminal by the candump command as
can0 123 [4] CA FE BA BE

Of course the roles of can0 and can1 is swappable.

Stay up to date with all the latest TechNexion news...

Sign-up for our Newsletter