Quantcast

Vehicle Network Toolbox

Parse Raw CAN Messages and Data

This example shows you how to use various Vehicle Network Toolbox™ commands to parse CAN messages and unpack raw signal data.

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)
mostRecentMsg400 = 

  can.Message handle
  Package: can

  Properties:
           ID: 400
     Extended: 0
         Name: ''
     Database: []
        Error: 0
       Remote: 0
    Timestamp: 5.1301
         Data: [238 200 175 119 66 145 63 81]
      Signals: []
     UserData: []


You can also obtain the most recent instance of each unique message in the log.

mostRecentMsgs = extractRecent(msgLog)
mostRecentMsgs = 

  1x5 can.Message handle
  Package: can

  Properties:
    ID
    Extended
    Name
    Database
    Error
    Remote
    Timestamp
    Data
    Signals
    UserData


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)
timeExtractedMsgs = 

  1x111 can.Message handle
  Package: can

  Properties:
    ID
    Extended
    Name
    Database
    Error
    Remote
    Timestamp
    Data
    Signals
    UserData


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)
allMsg400 = 

  1x40 can.Message handle
  Package: can

  Properties:
    ID
    Extended
    Name
    Database
    Error
    Remote
    Timestamp
    Data
    Signals
    UserData



remainder = 

  1x330 can.Message handle
  Package: can

  Properties:
    ID
    Extended
    Name
    Database
    Error
    Remote
    Timestamp
    Data
    Signals
    UserData


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)
timeExtractedMsgs = 

  1x148 can.Message handle
  Package: can

  Properties:
    ID
    Extended
    Name
    Database
    Error
    Remote
    Timestamp
    Data
    Signals
    UserData



allMsg600 = 

  1x8 can.Message handle
  Package: can

  Properties:
    ID
    Extended
    Name
    Database
    Error
    Remote
    Timestamp
    Data
    Signals
    UserData


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')
signalValues =

   -329  22284  32110  17542 -11129 -29965  21304 -22082

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