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: 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.
start(rxCh); start(txCh); msgEngineMsg.Data = [250 100 0 0 20 0 0 0]; pause(1); msgEngineMsg.Signals.VehicleSpeed = 60; pause(1); stop(txCh); stop(rxCh);
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: 10-Aug-2018 16:54:06 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') signals = canSignalTimetable(msgRx)
msgRx = 2x8 timetable Time ID Extended Name Data Length Signals Error Remote _____________ ___ ________ ___________ ___________ ______ ____________ _____ ______ 0.0089732 sec 100 false 'EngineMsg' [1x8 uint8] 8 [1x1 struct] false false 1.0203 sec 100 false 'EngineMsg' [1x8 uint8] 8 [1x1 struct] false false signals = 2x2 timetable Time VehicleSpeed EngineRPM _____________ ____________ _________ 0.0089732 sec 20 2835 1.0203 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 None Event Messages ID Extended Name Data --- -------- --------- --------------------- 100 false EngineMsg 250 100 0 0 60 0 0 0