MATLAB Examples

Event Triggered CAN Message Transmission

This example shows you how to use the automated CAN message transmit features of Vehicle Network Toolbox™ to send messages on event. It uses MathWorks Virtual CAN channels connected in a loopback configuration. As this example is based on sending and receiving CAN messages on a virtual network, running Vehicle CAN Bus Monitor in conjunction may provide a more complete understanding of what the code is doing. To run Vehicle CAN Bus Monitor, open and configure it to use the same interface as the receiving channel of the example. Make sure to start Vehicle CAN Bus Monitor before beginning to run the example in order to see all of the messages as they occur.


Create the CAN Channels

Create CAN channels on which to use the automated message transmit commands.

txCh = canChannel('MathWorks', 'Virtual 1', 1);
rxCh = canChannel('MathWorks', 'Virtual 1', 2);

In this example, you will use a CAN database file to define and decode messages. Open the database and attach it to the CAN channels.

db = canDatabase('demoVNT_CANdbFiles.dbc');
txCh.Database = db;
rxCh.Database = db;

Create the CAN Message

You can create a CAN message to register for event transmit using the database information.

msgEngineMsg = canMessage(db, 'EngineMsg')
msgEngineMsg = 

  Message with properties:

           ID: 100
     Extended: 0
         Name: 'EngineMsg'
     Database: [1×1 can.Database]
        Error: 0
       Remote: 0
    Timestamp: 0
         Data: [0 0 0 0 0 0 0 0]
      Signals: [1×1 struct]
     UserData: []

Configure the Message for Event Transmit

To configure a message for event transmit, use the transmitEvent command to specify the channel, the message to register on the channel, and a mode value.

transmitEvent(txCh, msgEngineMsg, 'On');

Start the Event Message Transmit

Start the channels and write the new values to either the Data property or directly to the signals to trigger automatic event-based transmission of the message onto the CAN bus.

msgEngineMsg.Data = [250 100 0 0 20 0 0 0];
msgEngineMsg.Signals.VehicleSpeed = 60;

Analyze the Event Transmit Behavior

The receiving channel now has two messages available, corresponding to the two updates that resulted in two transmits.

rxCh = 

Summary of CAN Channel using 'MathWorks' 'Virtual 1' Channel 2.

  Channel Parameters:  Bus Speed is 500000.
                       Bus Status is 'N/A'.
                       Transceiver name is 'N/A'.
                       Serial Number of this device is 0.
                       Initialization access is allowed.
                       'demoVNT_CANdbFiles.dbc' database is attached.

              Status:  Offline - Waiting for start.
                       2 messages available to receive.
                       0 messages transmitted since last start.
                       0 messages received since last start.

      Filter History:  Standard ID Filter: Allow All | Extended ID Filter: Allow All

Receive and inspect each message to see that each has the data values previously set.

msgRx = receive(rxCh, Inf, 'OutputFormat', 'timetable')
signals = canSignalTimetable(msgRx)
msgRx =

  2×8 timetable array

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

    0.56706 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     1.5762 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 

signals =

  2×2 timetable array

        Time       VehicleSpeed    EngineRPM
    ___________    ____________    _________

    0.56706 sec    20              2835     
     1.5762 sec    60              2835     

View Messages Configured for Event Transmit

To see messages configured on a channel for event transmit, use the transmitConfiguration command.

Periodic Messages


Event Messages

ID  Extended   Name            Data         
--- -------- --------- ---------------------
100 false    EngineMsg 250 100 0 0 60 0 0 0