Documentation Center

  • Trial Software
  • Product Updates


Log received CAN messages


Vehicle Network Toolbox: CAN Communication


The CAN Log block logs CAN messages from the CAN network or messages sent to the blocks input port to a .mat file. You can load the saved messages into MATLAB® for further analysis or into another Simulink® model.

    Note:   If your model uses a National Instruments® device, you cannot connect CAN Receive block and CAN Log to the same channel on the device.

You cannot connect a channel on a National Instruments device to more than one block. Configure your CAN Log block to log from the Simulink input port. Refer to the Basic CAN Message Replay and Logging example for more information.

The Log block appends the specified filename with the current date and time, creating unique log files for repeated logging.

If you want to use messages logged using Simulink blocks in the MATLAB Command window, use canMessage to convert messages to the correct format. Refer to the Basic CAN Message Replay and Logging example for information.

    Note:   You need a license for both Vehicle Network Toolbox™ and Simulink software to use this block.

Other Supported Features

The CAN Log block supports the use of Simulink Accelerator™ and Rapid Accelerator mode. Using this feature, you can speed up the execution of Simulink models.

For more information on this feature, see the Simulink documentation.

The CAN Log block supports the use of code generation along with the packNGo function to group required source code and dependent shared libraries. For more information, see Code Generation.

Code Generation

Vehicle Network Toolbox Simulink blocks allow you to generate code, enabling models containing these blocks to run successfully in Accelerator, Rapid Accelerator, External, and Deployed modes.

Code Generation with the Simulink Coder

You can use Vehicle Network Toolbox, Simulink Coder™, and Embedded Coder®products together to generate code (on the host end) that you can use to implement your model for a practical application. For more information on code generation, see Program Builds.

Shared Library Dependencies

The block generates code with limited portability. The block uses precompiled shared libraries, such as DLLs, to support I/O for specific types of devices. With this block, you can use the packNGo function supported by the Simulink Coder, to set up and manage the build information for your models. The packNGo function allows you to package model code and dependent shared libraries into a zip file for deployment. You do not need MATLAB installed on the target system, but the target system needs to be supported by MATLAB.

To set up packNGo:

set_param(gcs, 'PostCodeGenCommand', 'packNGo(buildInfo)');

In this example, gcs is the current model that you wish to build. Building the model creates a zip file with the same name as model name. You can move this zip file to another machine and the source code in the zip file can be built to create an executable which can be run independent of MATLAB and Simulink. The generated code compiles with both C and C++ compilers. For more information on packNGo, see packNGo.

Dialog Box

Use the Block Parameters dialog box to configure your message logging.

    Tip   If you are logging from the network, you need to configure your CAN channel with a CAN Configuration block.

File name

Type the name and path of the file to log CAN messages to, or click Browse to browse to a file location.

The model appends the log file name with the current date and time in the YYYY-MMM-DD_hhmmss format. You can also open the block mask and specify a unique name to differentiate between your files for repeated logging.

Variable Name

Type the variable saved in the MAT-file that holds the CAN message information.

Maximum number of messages to log

Specify the maximum number of messages this block can log from the selected device or port. The specified value must be a positive integer. If you do not specify a value the block uses the default value of 10,000 messages. The log file saves the most recent messages up to the specified maximum number.

Log messages from

Select the source of the messages logged by the block. Possible values are CAN Bus or Input port. To log messages from the network, you must specify a device.


Select the device on the CAN network that you want to log messages from. This filed is unavailable if you select Input port for Log messages from option.

Sample time

Specify the sampling time of the block during simulation, which is the simulation time as described by the Simulink documentation. This value defines the frequency at which the CAN Log block runs during simulation. If the block is inside a triggered subsystem or to inherit sample time, you can specify –1 as your sample time. You can also specify a MATLAB variable for sample time. The default value is 0.01 (in seconds).

See Also

CAN Replay

Was this topic helpful?