Main Content

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 CAN Explorer in conjunction may provide a more complete understanding of what the code is doing. To run CAN Explorer, open and configure it to use the same interface as the receiving channel of the example. Make sure to start CAN Explorer 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('CANDatabaseEvent.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:

   Message Identification
    ProtocolMode: 'CAN'
              ID: 100
        Extended: 0
            Name: 'EngineMsg'

   Data Details
       Timestamp: 0
            Data: [0 0 0 0 0 0 0 0]
         Signals: [1x1 struct]
          Length: 8

   Protocol Flags
           Error: 0
          Remote: 0

   Other Information
        Database: [1x1 can.Database]
        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 = 
  Channel with properties:

   Device Information
            DeviceVendor: 'MathWorks'
                  Device: 'Virtual 1'
      DeviceChannelIndex: 2
      DeviceSerialNumber: 0
            ProtocolMode: 'CAN'

   Status Information
                 Running: 0
       MessagesAvailable: 2
        MessagesReceived: 0
     MessagesTransmitted: 0
    InitializationAccess: 1
        InitialTimestamp: 23-Feb-2021 17:22:13
           FilterHistory: 'Standard ID Filter: Allow All | Extended ID Filter: Allow All'

   Channel Information
               BusStatus: 'N/A'
              SilentMode: 0
         TransceiverName: 'N/A'
        TransceiverState: 'N/A'
       ReceiveErrorCount: 0
      TransmitErrorCount: 0
                BusSpeed: 500000
                     SJW: []
                   TSEG1: []
                   TSEG2: []
            NumOfSamples: []

   Other Information
                Database: [1x1 can.Database]
                UserData: []

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

msgRx = receive(rxCh, Inf, 'OutputFormat', 'timetable')
msgRx=2×8 timetable
       Time        ID     Extended        Name                   Data              Length      Signals       Error    Remote
    ___________    ___    ________    _____________    ________________________    ______    ____________    _____    ______

    0.07335 sec    100     false      {'EngineMsg'}    {[250 100 0 0 20 0 0 0]}      8       {1x1 struct}    false    false 
    1.1006 sec     100     false      {'EngineMsg'}    {[250 100 0 0 60 0 0 0]}      8       {1x1 struct}    false    false 

signals = canSignalTimetable(msgRx)
signals=2×2 timetable
       Time        VehicleSpeed    EngineRPM
    ___________    ____________    _________

    0.07335 sec         20           2835   
    1.1006 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