Transmit and Receive CAN Raw Data and Messages on Virtual Linux Target Device
R2026bThis example shows how to connect to a Linux Target Device, build a real-time application from an Embedded Coder model and the Simulink Real-Time Support Package for Linux Devices, configure virtual CAN channels on a virtual Linux target device, and run the real-time application on a Linux Target Devices.
The virtual CAN communications uses the CANBusEvent.dbc file to provide message data.
For more information about the Simulink Real-Time operations in this example, see Apply Real-Time Simulation and Testing Operations (Simulink Real-Time)
Open the Real-Time Model
To open the model, in the MATLAB® Command Window, type:
model = 'slrtlinux_ex_socketcan';
open_system(model);

Build Real-Time Application from Model
With a Linux target device that has been configure with the Simulink Real-Time Support Package for Linux Device connected to your development computer, use Simulink Real-Time to define the Linux target device as your default target computer. Then:
Create a Target object
tgthat represents the Linux target device.Connect Simulink Real-Time to the Target object.
Identify the system target file (STF) that corresponds to the Linux target device.
Configure the model to use the STF for code generation and build the real-time
application MLDATX file from the model.
tg = slrealtime;
connect(tg);
modelSTF = getSTFName(tg);
set_param(model,"SystemTargetFile",modelSTF);
evalc('slbuild(model)');Configure Virtual CAN Channels on Linux Target Device
At the Linux target device command line, use these commands to set up virtual CAN channel vcan0:
sudo ip link add dev vcan0 type vcan sudo ip link set up vcan0
At the Linux target device command line, use these commands to set up virtual CAN channel vcan1:
sudo ip link add dev vcan1 type vcan sudo ip link set up vcan1
Run Real-Time Application and View Signal Data
Load and start the real-time application. When the application status changes to stopped, auto import the file log data and view the signal data in the Simulation Data Inspector.
load(tg,model);
start(tg,'AutoImportFileLog',true);
while ~strcmp(tg.status,'stopped')
pause(5);
end
stop(tg);
Simulink.sdi.viewClose Models
bdclose(model);
See Also
CAN Receive | CAN Transmit | CAN Pack (Simulink Real-Time) | CAN Unpack (Simulink Real-Time)
Topics
- Create and Run Real-Time Application from Simulink Model on Linux Target Device
- Prerequisites for Real-Time Linux Setup
- About SocketCAN Blocks in Real-Time Linux Support Package
- Configure SocketCAN Block Interface for Real-Time Linux Support Package