Vehicle Network Toolbox
This demo illustrates the use of CAN channels for transmitting and receiving messages. It uses Vector Virtual CAN channels which are inherently connected in a loopback configuration.
Create a CAN channel to receive messages. With the canChannel command, you can build a channel by specifying the vendor name, the device name with an index to the specific device on your system, and lastly the device channel index. The device index is necessary in case you have multiples of the same device available on your system. The device channel index is used as many devices have more than a single CAN channel available.
rxCh = canChannel('Vector', 'Virtual 1', 2);
To display details of the CAN channel, type the channel name. Information such as the current status of the channel and various statistics regarding its operation is displayed.
rxCh = Summary of CAN Channel using 'Vector' 'Virtual 1' Channel 2. Channel Parameters: Bus Speed is 500000. Bus Status is 'N/A'. Transceiver name is ''. Serial Number of this device is 0. Initialization access is allowed. No database is attached. Status: Offline - Waiting for start. 0 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
To obtain more detailed information on all of the channel's properties and their current values, use the GET command.
General Settings: BusStatus = 'N/A' Database =  FilterHistory = 'Standard ID Filter: Allow All | Extended ID Filter: Allow A ll' InitializationAccess = 1 MessageReceivedFcn =  MessageReceivedFcnCount = 1 MessagesAvailable = 0 MessagesReceived = 0 MessagesTransmitted = 0 ReceiveErrorCount = 0 Running = 0 SilentMode = 0 TransmitErrorCount = 0 Device Settings: Device = 'Virtual 1' DeviceChannelIndex = 2 DeviceSerialNumber = 0 DeviceVendor = 'Vector' Transceiver Settings: TransceiverName = '' TransceiverState = 0 Bit Timing Settings: BusSpeed = 500000 SJW = 1 TSEG1 = 4 TSEG2 = 3 NumOfSamples = 1
Use the START command to set the channel online.
The function generateMsgs creates messages and transmits them at various periodic rates. It is used to create traffic on the CAN bus for demonstration purposes and is not part of the Vehicle Network Toolbox™. Viewing generateMsgs shows you how to construct CAN messages with the canMessage function and also how to configure them to transmit periodically.
function generateMsgs() % generateMsgs Creates and transmits CAN messages for demo purposes. % % generateMsgs periodically transmits multiple CAN messages at various % periodic rates with changing message data. % % Copyright 2008-2010 The MathWorks, Inc. % Create the messages to send using the canMessage function. The % identifier, an indication of standard or extended type, and the data % length is given for each message. msgTx100 = canMessage(100, false, 0); msgTx200 = canMessage(200, false, 2); msgTx400 = canMessage(400, false, 4); msgTx600 = canMessage(600, false, 6); msgTx800 = canMessage(800, false, 8); % Create a CAN channel on which to transmit. txCh = canChannel('Vector', 'Virtual 1', 1); % Register each message on the channel at a specified periodic rate. transmitPeriodic(txCh, msgTx100, 'On', 0.500); transmitPeriodic(txCh, msgTx200, 'On', 0.250); transmitPeriodic(txCh, msgTx400, 'On', 0.125); transmitPeriodic(txCh, msgTx600, 'On', 0.050); transmitPeriodic(txCh, msgTx800, 'On', 0.025); % Start the CAN channel. start(txCh); % Run for several seconds incrementing the message data regularly. for ii = 1:50 % Increment the message data bytes. msgTx200.Data = msgTx200.Data + 1; msgTx400.Data = msgTx400.Data + 1; msgTx600.Data = msgTx600.Data + 1; msgTx800.Data = msgTx800.Data + 1; % Wait for a time period. pause(0.100); end % Stop the CAN channel. stop(txCh); end
Now you can run the generateMsgs function to transmit messages for the demo.
Once the generateMsgs command is done sending messages, you can acquire them on the receiving channel using the RECEIVE command. Specify the number of messages you want returned in the second argument to RECEIVE. Use 'Inf' to receive all available messages.
rxMsg = receive(rxCh, Inf)
rxMsg = 1x412 can.Message handle Package: can Properties: ID Extended Name Database Error Remote Timestamp Data Signals UserData
You can now set the CAN channel offline with the STOP command. If you are connected to an active CAN network, you can leave a channel online to continue communicating as needed.
MATLAB® provides a powerful environment for performing analysis on CAN messages. A general visualization of the messages is shown below. A plot of message Timestamps and identifiers provides an overview of when certain messages appeared on the network. The Vehicle Network Toolbox also provides a number of additional functions to simplify handling of CAN messages and signal data. Some of these functions are explained in other demos.
plot([rxMsg.Timestamp], [rxMsg.ID], 'x') ylim([0 2047]) xlabel('Timestamp') ylabel('CAN Identifier')
Remove the CAN channel.