www.gusucode.com > vnt 源码程序 matlab案例代码 > vnt/demoVNT_HandlingCANMessages.m
%% Parse Raw CAN Messages and Data % This example shows you how to use various Vehicle Network Toolbox(TM) % commands to parse CAN messages and unpack raw signal data. % % Copyright 2009-2012 The MathWorks, Inc. %% Load a Message Log % For this example, you can load the |demoVNT_HandlingCANMessages.mat| % file. It contains a log of CAN messages. load demoVNT_HandlingCANMessages.mat %% Extract the Most Recent Messages % Use the |extractRecent| command to obtain the most recent messages per % each unique CAN identifier from an array of messages. %% % You can specify a specific message to return by identifier. mostRecentMsg400 = extractRecent(msgLog, 400, false) %% % You can also obtain the most recent instance of each unique message % in the log. mostRecentMsgs = extractRecent(msgLog) %% % Plotting the most recent instance of each CAN message against their % timestamps gives an indication on the time at which each occurred. plot([mostRecentMsgs.Timestamp], [mostRecentMsgs.ID], 'x') title('Most Recent Instance of Each Unique Message', 'FontWeight', 'bold') axis([0 6 0 2047]) xlabel('Timestamp') ylabel('CAN Identifier') %% Extract Messages with Respect to Time % The |extractTime| command allows you to parse a message log based on the % message timestamps to return all messages occurring within a time range. timeExtractedMsgs = extractTime(msgLog, 1, 2.5) %% % Plotting the result of the extraction by time shows that only the % messages within the time range were provided as output. plot([timeExtractedMsgs.Timestamp], [timeExtractedMsgs.ID], 'x') title('Messages Occurring Within a Defined Time Range', 'FontWeight', 'bold') axis([0 6 0 2047]) xlabel('Timestamp') ylabel('CAN Identifier') %% Extract All Messages % The |extractAll| command returns all instances of a specific message % from a larger array. It also returns everything else except the % specified message identifier as a remainder. [allMsg400, remainder] = extractAll(msgLog, 400, false) %% Extraction Commands in Sequence % Extraction commands work together in sequence. For example, you can % extract by time and then by identifier on the output of the time-based % operation. timeExtractedMsgs = extractTime(msgLog, 2, 4) allMsg600 = extractAll(timeExtractedMsgs, 600, false) %% Unpack Signal Data % Obtaining data from a CAN message can often be a complex operation given % signal definition complexity (signed/unsigned, little/big endian, etc). % The |unpack| command simplifies access to raw signal data by taking % signal definition information as input. In this example, you can take % the output of the last extraction operation and unpack a % signal from each message in the array. signalValues = unpack(allMsg600, 0, 16, 'LittleEndian', 'int16') %% % Plotting the signals values against their timestamps allows you to % analyze how the signal changed over time. plot([allMsg600.Timestamp], signalValues) title('Signal Value from Message 600', 'FontWeight', 'bold') xlabel('Timestamp') ylabel('Signal Value') axis([0 6 -40000 40000])