The toolbox provides basic blocks for sending and receiving CAN messages for ERT Linux Simulink target when mainlined socket based CAN bus support is used. The cantransmit and canreceive blocks support directly encapsulation and conversion to the Simulink signals for basic data types: uint8, uint16 and uint32. The standard Simulink defined CAN MESSAGE TYPE can be used when more complex structure of CAN message sending and receiption is required. The signal of CAN MESSAGE TYPE can be connected to CAN Pack and CAN Unpack blocks which encode or retrieve multiple signals to/from CAN message. The CAN Pack and CAN Unpack blocks are provided by canmsglib which is part of standard Simulink distribution.

The code generation requires to include CAN Setup into diagram to work correctly. This block is used to setup baudrate for each CAN channel in the embedded environment but this option is ignored in the actual setup block implementation on Linux. The baudrate can be selected by system administrator from commandline

ip link set can0 down
ip link set can0 type can bitrate 1000000 
ip link set can0 up
or on Debian based systems /etc/network/interfaces configuration file can be used to setup CAN interface baudrate at system startup
allow-hotplug can0
iface can0 can static
    bitrate 1000000
The later setup works automatic even with CAN-USB converter hotplug.

Project Included Files

The projects includes blocks library socketcan_lib.slx, simple example of provided blocks use cantransmit_ert.slx and more complex model canopen_2j_arm_ert.slx which demonstrates use of the block to send PDO messages to two stepper motors controlled robotic arm. This simple CAN library does not provide CANopen support yet so initial motors SDO parameters setup is solved by canopen_2j_arm_setup shell script which invokes cansend command. The variant of the model (canopen_2j_arm_rpi.slx) with build parameters preset for crosscompilation to Raspberry Pi is provided as well. The CAN-USB converter from 8devices has been used in this case.

Download/clone ERT Linux target and Linux CAN bus toolbox repositories. The example expects the toolbox are placed into user local ~/matlab/toolbox directory. Setup Matlab path for thee projects. One option is to use ~/matlab/startup.m file for this purpose:

function startup(AddRem)

disp 'Executing local setup stript ~/matlab/startup.m'


[matlab_startup_dir, matlab_startup_filename, matlab_startup_dir_ext] = fileparts(mfilename('fullpath'));

addpath(fullfile(matlab_startup_dir, 'toolbox/ert_linux/ert_linux'));
addpath(fullfile(matlab_startup_dir, 'toolbox/socketcan-simulink/blocks'));

Use example model as base for the project or open SimulationModel Configuration Parameters dialog and select System target file to ert_linux.tlc in Code Genration section. Check Make command. It should be something like:

make_rtw OPT_OPTS="-O2 -ggdb -Wall -D_BSD_SOURCE"

Use proper CC variable definition in case of cross compilation to some other target. See next example for Raspberry Pi:

make_rtw OPT_OPTS="-O2 -ggdb -Wall -D_BSD_SOURCE" CC=arm-rpi-linux-gnueabihf-gcc
Linux CAN bus toolbox repository
git clone git://
Usable Simulink Embedded Coder Target for Linux
Michal Sojka, Pavel Pisa
16th Real-Time Linux Workshop, Düsseldorf, Germany, October 2014. The paper (PDF) and slides (PDF) are available from our publications archive.