Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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.

load SourceMsgs.mat
canMsgs = canMessageReplayBlockStruct(canMsgTimetable);
save DriveReplay.mat canMsgs
whos

  Name                      Size               Bytes  Class        Attributes

  canMsgTimetable      100000x8             35910842  timetable              
  canMsgs                   1x1              2401232  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 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
whos
WheelSpeeds_2011-May-03_020634.mat  

  Name                      Size               Bytes  Class        Attributes

  canMsgTimetable      100000x8             35910842  timetable              
  canMsgs                   1x1              2401232  struct                 
  outMsgs                   1x1               154376  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.

db = canDatabase('VehicleInfo.dbc');
wheelSpeedMsgTimetable = canMessageTimetable(outMsgs, db);
wheelSpeedMsgTimetable(1:15, :)
wheelSpeedSignals = canSignalTimetable(wheelSpeedMsgTimetable);
wheelSpeedSignals(1:15, :)
ans =

  15×8 timetable array

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

    0.10701 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.1153 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.12349 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.13178 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.13998 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.14826 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.15647 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.16475 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.17338 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.18122 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.18941 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.19768 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.20591 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.2142 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.2224 sec    1200    false       'WheelSpeeds'    [1×8 uint8]    8         [1×1 struct]    false    false 


ans =

  15×4 timetable array

        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.

Was this topic helpful?