This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Transmit and Receive CAN Messages

This example shows you how to use CAN channels to transmit and receive CAN messages. It uses MathWorks Virtual CAN channels connected in a loopback configuration.

Create a Receiving Channel

Create a CAN channel to receive messages by specifying the vendor name, device name, and device channel index.

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

Inspect the Channel

Use the get command to obtain more detailed information on all of the channel's properties and their current values.

get(rxCh)
        ArbitrationBusSpeed: []
               DataBusSpeed: []
          ReceiveErrorCount: 0
         TransmitErrorCount: 0
       InitializationAccess: 1
           InitialTimestamp: [0x0 datetime]
                 SilentMode: 0
           TransceiverState: 'N/A'
                   BusSpeed: 500000
               NumOfSamples: []
                        SJW: []
                      TSEG1: []
                      TSEG2: []
                  BusStatus: 'N/A'
            TransceiverName: 'N/A'
                   Database: []
         MessageReceivedFcn: []
    MessageReceivedFcnCount: 1
                   UserData: []
              FilterHistory: 'Standard ID Filter: Allow All | Extended ID Filter: Allow All'
           MessagesReceived: 0
        MessagesTransmitted: 0
                    Running: 0
                     Device: 'Virtual 1'
         DeviceChannelIndex: 2
         DeviceSerialNumber: 0
               DeviceVendor: 'MathWorks'
               ProtocolMode: 'CAN'
          MessagesAvailable: 0

Start the Channel

Use the start command to set the channel online.

start(rxCh);

Transmit Messages

The example function generateMsgs creates CAN messages and transmits them at various periodic rates. It creates traffic on the CAN bus for example purposes and is not part of the Vehicle Network Toolbox™.

type generateMsgs
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-2016 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('MathWorks', '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

Run the generateMsgs function to transmit messages for the example.

generateMsgs();

Receive Messages

Once generateMsgs completes, receive all of the available messages from the channel.

rxMsg = receive(rxCh, Inf, 'OutputFormat', 'timetable');
rxMsg(1:25, :)
ans =

  25x8 timetable

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

    0.0040945 sec    100     false       ''     [1x0 uint8]      0       [0x0 struct]    false    false 
    0.0041009 sec    200     false       ''     [1x2 uint8]      2       [0x0 struct]    false    false 
    0.0041123 sec    400     false       ''     [1x4 uint8]      4       [0x0 struct]    false    false 
    0.0041189 sec    600     false       ''     [1x6 uint8]      6       [0x0 struct]    false    false 
    0.0041204 sec    800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.029065 sec     800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.054068 sec     600     false       ''     [1x6 uint8]      6       [0x0 struct]    false    false 
    0.05407 sec      800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.079092 sec     800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.10408 sec      600     false       ''     [1x6 uint8]      6       [0x0 struct]    false    false 
    0.10409 sec      800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.12907 sec      400     false       ''     [1x4 uint8]      4       [0x0 struct]    false    false 
    0.12907 sec      800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.15415 sec      600     false       ''     [1x6 uint8]      6       [0x0 struct]    false    false 
    0.15415 sec      800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.17911 sec      800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.20407 sec      600     false       ''     [1x6 uint8]      6       [0x0 struct]    false    false 
    0.20411 sec      800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.2291 sec       800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.2541 sec       200     false       ''     [1x2 uint8]      2       [0x0 struct]    false    false 
    0.2541 sec       400     false       ''     [1x4 uint8]      4       [0x0 struct]    false    false 
    0.2541 sec       600     false       ''     [1x6 uint8]      6       [0x0 struct]    false    false 
    0.2541 sec       800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.27908 sec      800     false       ''     [1x8 uint8]      8       [0x0 struct]    false    false 
    0.30411 sec      600     false       ''     [1x6 uint8]      6       [0x0 struct]    false    false 

Stop the Channel

Use the stop command to set the channel offline.

stop(rxCh);

Analyze Received Messages

MATLAB® provides a powerful environment for performing analysis on CAN messages. The plot command can create a scatter plot with message Timestamps and identifiers to provide an overview of when certain messages occurred on the network.

plot(rxMsg.Time, rxMsg.ID, 'x')
ylim([0 2047])
xlabel('Timestamp')
ylabel('CAN Identifier')