www.gusucode.com > vnt 源码程序 matlab案例代码 > vnt/demoVNT_InitialTimestamp.m

    %% Using Timestamps
% This example shows you how to use Vehicle Network Toolbox(TM) with 
% the InitialTimestamp CAN channel property to work with relative and
% absolute timestamps for CAN messages. It also uses Vector Virtual CAN 
% channels connected in a loopback configuration.
%
% Copyright 2015 The MathWorks, Inc.

%% Open the Database File 
% Open the database file to access the definitions.
db = canDatabase('VehicleInfo.dbc')

%% Create the CAN Channels
% Create CAN channels on which you can send and receive information.
txCh = canChannel('Vector','Virtual 1',1)
rxCh = canChannel('Vector','Virtual 1',2)
rxCh.Database = db;

%% Create the CAN Message
% You can create CAN messages to send on the network.
msg = canMessage(db,'WheelSpeeds')

%% Start the CAN Channels
% To begin using channels for transmit and receive operations, place
% them online.
start(rxCh)
start(txCh)

%% Send CAN Messages
% The |transmit| function sends messages onto the network. Use |pause| to
% delay the transmits. Update the signal data each transmit.
msg.Signals.LF_WSpeed = 10;
transmit(txCh,msg)
pause(1);
msg.Signals.LF_WSpeed = 20;
transmit(txCh,msg)
pause(2);
msg.Signals.LF_WSpeed = 30;
transmit(txCh,msg)
pause(3);
msg.Signals.LF_WSpeed = 40;
transmit(txCh,msg)
pause(1);
msg.Signals.LF_WSpeed = 50;
transmit(txCh,msg)

%% Receive the CAN Messages
% The |receive| function retrieves information from the channel representing
% messaging that occurred on the network.
stop(rxCh)
stop(txCh)
msgRx = receive(rxCh,Inf)

%% Inspect Signal Data
% View details of the received messages, including the timestamps and the
% signal value. Note that timestamp values are relative from the start time
% of the CAN channel.
relativeTimestamps = [msgRx.Timestamp]
signals = [msgRx.Signals]
signalValues = [signals.LF_WSpeed]
plot(relativeTimestamps,signalValues,'x')
title('Signal Data with Relative Time','FontWeight','bold')
xlabel('Relative Timestamp')
ylabel('Signal Value')

%% Inspect InitialTimestamp Property
% View the |InitialTimestamp| property of the receiving CAN channel. It is a
% datetime value that gives the absolute time of when the channel was
% started.
rxCh.InitialTimestamp

%% Analyze Data with Absolute Timestamps
% The relative timestamps of each message and the InitialTimestamp 
% property combine to apply absolute time values to each message or signal
% contained within.
absoluteTimestamps = seconds(relativeTimestamps) + rxCh.InitialTimestamp
figure
plot(absoluteTimestamps,signalValues,'x')
title('Signal Data with Absolute Time','FontWeight','bold')
xlabel('Absolute Timestamp')
ylabel('Signal Value')