Main Content

Log and Replay CAN Messages

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

Load the saved CAN message from sourceMsgs.mat file from the examples 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.

  Name                      Size               Bytes  Class        Attributes

  canMsgTimetable      100000x8             33510851  timetable              
  canMsgs                   1x1              2401176  struct                 

CAN Replay Model

This model contains:

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

  • A CAN Receive block that receives the messages on a CAN network, through MathWorks 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 Information

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.

WheelSpeeds_2011-May-03_020634.mat  

  Name                      Size               Bytes  Class        Attributes

  canMsgTimetable      100000x8             33510851  timetable              
  canMsgs                   1x1              2401176  struct                 
  outMsgs                   1x1               154320  struct                 

Convert Logged Messages

Use canMessageTimetable to convert messages logged during the simulation to a timetable that you can use in the command window.

To access message signals directly, use the appropriate database file in the conversion along with canSignalTimetable.

ans =

  15x8 timetable

       Time         ID     Extended         Name                     Data                Length      Signals       Error    Remote
    ___________    ____    ________    _______________    ___________________________    ______    ____________    _____    ______

    0.10701 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.1153 sec     1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.12349 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.13178 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.13998 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.14826 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.15647 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.16475 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.17338 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.18122 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.18941 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.19768 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.20591 sec    1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.2142 sec     1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 
    0.2224 sec     1200     false      {'WheelSpeeds'}    {[39 16 39 16 39 16 39 16]}      8       {1x1 struct}    false    false 


ans =

  15x4 timetable

       Time        LR_WSpeed    RR_WSpeed    RF_WSpeed    LF_WSpeed
    ___________    _________    _________    _________    _________

    0.10701 sec        0            0            0            0    
    0.1153 sec         0            0            0            0    
    0.12349 sec        0            0            0            0    
    0.13178 sec        0            0            0            0    
    0.13998 sec        0            0            0            0    
    0.14826 sec        0            0            0            0    
    0.15647 sec        0            0            0            0    
    0.16475 sec        0            0            0            0    
    0.17338 sec        0            0            0            0    
    0.18122 sec        0            0            0            0    
    0.18941 sec        0            0            0            0    
    0.19768 sec        0            0            0            0    
    0.20591 sec        0            0            0            0    
    0.2142 sec         0            0            0            0    
    0.2224 sec         0            0            0            0    

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