%% 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 MathWorks Virtual CAN channels connected in a loopback configuration.
%
% Copyright 2008-2019 The MathWorks, Inc.

%%  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)

%% 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

%% 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(TM). 
% 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

%% Stop the Channel
% Use the |stop| command to set the channel offline.
stop(rxCh);
