www.gusucode.com > vnt 源码程序 matlab案例代码 > vnt/demoVNT_BasicCAN.m
%% Transmit and Receive CAN Messages % This example shows you how to use CAN channels to transmit and % receive CAN messages. It uses Vector Virtual CAN channels connected in % a loopback configuration. % % Copyright 2008-2012 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('Vector', '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) %% 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(TM). type generateMsgs %% % 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) %% Stop the Channel % Use the |stop| command to set the channel offline. stop(rxCh); %% Analyze Received Messages % MATLAB(R) 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.Timestamp], [rxMsg.ID], 'x') ylim([0 2047]) xlabel('Timestamp') ylabel('CAN Identifier')