Documentation Center

  • Trial Software
  • Product Updates

Log and Replay CAN Messages

This example shows you how to log and replay CAN messages using Vector Virtual CAN channels in Simulink®. You can update this model to connect to supported hardware on your system.

Load CAN Message

To use this example, navigate to the product demos folder.

cd (matlabroot)
cd toolbox\vnt\vntdemos

Load the saved CAN message from sourceMsgs.mat file from the demos folder. The file contains CAN messages representing a 90 second drive cycle around a test track.

Convert these messages to a format compatible with the CAN Replay block and save it to a separate file.

load SourceMsgs.mat
canMsgs = vntslgate('privatecanslconvertmsgs', canMsgObjects);
save DriveReplay.mat canMsgs
  Name               Size                  Bytes  Class          Attributes

  canMsgObjects      1x100000             800104  can.Message              
  canMsgs            1x1                 2401232  struct                   

CAN Replay Model

This model contains:

  • A CAN Replay block that transmits to Vector Virtual Channel 1.

  • A CAN Receive block that receives the messages on a CAN network, through Vector Virtual Channel 2.

The CAN Receive block is configured to block all extended IDs and allow only the WheelSpeed message with the standard ID 1200 to pass.

The Wheel Speeds subsystem unpacks the wheel speed information from the received CAN messages and plots them to a scope. The subsystem also logs the messages to a file.

Visualize Wheel Speed Imformation

The plot shows the wheel speed for all wheels for the duration of the test drive.

Load the Logged Message File

The CAN Log block creates a unique file each time you run the model. Use dir in the MATLAB Command Window to find the latest log file.

dir WheelSpeeds*.mat
load WheelSpeeds_2011-May-03_020634.mat

  Name               Size                  Bytes  Class          Attributes

  canMsgObjects      1x100000             800104  can.Message              
  canMsgs            1x1                 2401232  struct                   
  outMsgs            1x1                  154376  struct                   

Convert Logged Messages to MATLAB Objects

Use canMessage to convert messages logged during the simulation to MATLAB objects that you can use in the command window.

To access message signals directly, attach the appropriate database file to the message. The database applies definitions to the converted messages.

db = canDatabase('VehicleInfo.dbc')
wheelSpeedMsgObjects = canMessage(outMsgs, db)
db = 

  can.Database handle
  Package: can

        Name: 'VehicleInfo'
        Path: 'C:\Mar152012\matlab\toolbox\vnt\vntdemos\VehicleInfo.dbc'
    Messages: {'WheelSpeeds'}
    UserData: []

wheelSpeedMsgObjects = 

  1x6381 can.Message handle
  Package: can


Vector CAN Virtual channels were used for this example. You can however connect your models to other supported hardware.

Was this topic helpful?