Main Content

CAN Input/Output with Vehicle Network Toolbox

This example shows how to transfer data through CAN bus, utilizing the CAN_MESSAGE data type and the CAN Pack and CAN Unpack blocks available in Vehicle Network Toolbox™ block library. The CAN_MESSAGE data type can be directly processed by Simulink Desktop Real-Time™ blocks.

The example also shows the difference in data transfer capability between the CAN and CAN FD protocols. The CAN protocol maximum data size is 8 bytes. This protocol is able to send eight double-precision values, the values need to be scaled and converted to the uint8 data type, losing some precision. The CAN FD protocol maximum data size is 64 bytes. This protocol is capable of transferring eight double-precision values in their native format without any conversion and precision loss.

The model sends data within one computer, from one virtual CAN channel to another. The two CAN channels can be either virtual channels or physical channels on a dual-channel CAN device. You can modify the model to communicate between two computers by splitting this model into its send and receive parts and running the models on two computers.

The yellow blocks are used to send the data. The blue blocks are used to receive the data.

Note: This model runs on Microsoft Windows only.

Run Model in Normal Mode

  1. To switch to normal mode if needed, on the Simulation tab, select Normal mode.

  2. To start the real-time execution, on the Simulation tab, click Run.

Run Model in External Mode

To start the real-time execution in external mode, on the Desktop Real-Time tab, click Run in Real Time. The model builds, connects to Simulink in external mode, and starts.

Open the Model

These commands open the model and suppress warning about board not installed.

w = warning('off', 'sldrt:blkgui:boardnotonlist');
open_system(fullfile(matlabroot,'toolbox','sldrt','sldrtexamples','sldrtex_canmessage'));
warning(w);

Close Open Scopes

close_system(find_system(gcs ,'BlockType', 'Scope'));

Clean Up Model

clear
close all
bdclose all

See Also