Documentation

CAN Message Reception Callback Functions

This example shows you how to configure and use a callback function to receive and process messages received from a CAN channel. It uses Vector 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('Vector', 'Virtual 1', 2)
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

Configure the Callback Function

Set a callback function on the channel to trigger via incoming messages.

rxCh.MessageReceivedFcn = @receivingFcn;

Configure the Message Received Count

Specify an available message threshold to control the quantity of messages required in the channel before the callback function is triggered.

rxCh.MessageReceivedFcnCount = 30;

Implement the Callback Function

The example callback function receives message from the channel on each execution and plots the CAN identifiers against their timestamps on each execution.

type receivingFcn
function receivingFcn(rxCh)
% RECEIVINGFCN A CAN channel message receive callback function.
%
%   This is a callback function used to receive CAN message. It receives 
%   messages from the channel RXCH, extracts them by identifier, unpacks 
%   signal values from the messages, and plots the result.
%

% Copyright 2009 The MathWorks, Inc.

    % Receive all available messages.
    rxMsg = receive(rxCh, Inf);

    % Plot the signal values against their message timestamps.
    plot([rxMsg.Timestamp], [rxMsg.ID], 'x');
    ylim([0 2047])
    xlabel('Timestamp');
    ylabel('CAN Identifier');
    hold all;
end

Start the Channel

Use the start command to set the channel online.

start(rxCh);

Execute the Callback Function

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™. As the messages are transmitted, the callback function executes each time the message received function count threshold is met.

generateMsgs();

Inspect the Remaining Messages

Note the remaining messages in the channel. Since the available message count is below the threshold specified earlier, more messages are required to trigger the callback another time.

rxCh
rxCh = 

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

  Channel Parameters:  Bus Speed is 500000.
                       Bus Status is 'ErrorActive'.
                       Transceiver name is ''.
                       Serial Number of this device is 0.
                       Initialization access is allowed.
                       No database is attached.

              Status:  Online.
                       5 messages available to receive.
                       0 messages transmitted since last start.
                       413 messages received since last start.

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

Stop the Channel

Use the stop command to set the channel offline.

stop(rxCh);
Was this topic helpful?