Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Create and Process Messages Using Database Definitions

This example shows you how to create, receive and process messages using information stored in CAN database files. This example uses the CAN database file, demoVNT_CANdbFiles.dbc.

Open the Database File

Open the database file and examine the Messages property to see the names of all message defined in this database.

db = canDatabase('demoVNT_CANdbFiles.dbc')
db.Messages
db = 

  Database with properties:

             Name: 'demoVNT_CANdbFiles'
             Path: '\\central-mi\home\jpyle\Documents\MATLAB\examples\vnt-ex80654288\demoVNT_CANdbFiles.dbc'
            Nodes: {}
         NodeInfo: [0×0 struct]
         Messages: {5×1 cell}
      MessageInfo: [5×1 struct]
       Attributes: {}
    AttributeInfo: [0×0 struct]
         UserData: []


ans =

  5×1 cell array

    'DoorControlMsg'
    'EngineMsg'
    'SunroofControlMsg'
    'TransmissionMsg'
    'WindowControlMsg'

View Message Information

Use messageInfo to view message information, including the identifier, data length, and a signal list.

messageInfo(db, 'EngineMsg')
ans = 

  struct with fields:

             Name: 'EngineMsg'
          Comment: ''
               ID: 100
         Extended: 0
            J1939: []
           Length: 8
          Signals: {2×1 cell}
       SignalInfo: [2×1 struct]
          TxNodes: {0×1 cell}
       Attributes: {}
    AttributeInfo: [0×0 struct]

You can also query for information on all messages at once.

messageInfo(db)
ans = 

  5×1 struct array with fields:

    Name
    Comment
    ID
    Extended
    J1939
    Length
    Signals
    SignalInfo
    TxNodes
    Attributes
    AttributeInfo

View Signal Information

Use signalInfo to view signal definition information, including type, byte ordering, size, and scaling values that translate raw signals to physical values.

signalInfo(db, 'EngineMsg', 'EngineRPM')
ans = 

  struct with fields:

             Name: 'EngineRPM'
          Comment: ''
         StartBit: 0
       SignalSize: 32
        ByteOrder: 'LittleEndian'
           Signed: 0
        ValueType: 'Integer'
            Class: 'uint32'
           Factor: 0.1000
           Offset: 250
          Minimum: 250
          Maximum: 9500
            Units: 'rpm'
       ValueTable: [0×1 struct]
      Multiplexor: 0
      Multiplexed: 0
    MultiplexMode: 0
          RxNodes: {0×1 cell}
       Attributes: {}
    AttributeInfo: [0×0 struct]

You can also query for information on all signals in the message at once.

signalInfo(db, 'EngineMsg')
ans = 

  2×1 struct array with fields:

    Name
    Comment
    StartBit
    SignalSize
    ByteOrder
    Signed
    ValueType
    Class
    Factor
    Offset
    Minimum
    Maximum
    Units
    ValueTable
    Multiplexor
    Multiplexed
    MultiplexMode
    RxNodes
    Attributes
    AttributeInfo

Create a Message Using Database Definitions

Specify the name of the message when you create a new message to have the database definition applied. CAN signals in this messages are represented in engineering units in addition to the raw data bytes.

msgEngineInfo = canMessage(db, 'EngineMsg')
msgEngineInfo = 

  Message with properties:

           ID: 100
     Extended: 0
         Name: 'EngineMsg'
     Database: [1×1 can.Database]
        Error: 0
       Remote: 0
    Timestamp: 0
         Data: [0 0 0 0 0 0 0 0]
      Signals: [1×1 struct]
     UserData: []

View Signal Information

Use the Signals property to see signal values for this message. You can directly write to and read from these signals to pack or unpack data from the message.

msgEngineInfo.Signals
ans = 

  struct with fields:

    VehicleSpeed: 0
       EngineRPM: 250

Change Signal Information

Write directly to the signal to change a value and read its current value back.

msgEngineInfo.Signals.EngineRPM = 5500.25
msgEngineInfo.Signals
msgEngineInfo = 

  Message with properties:

           ID: 100
     Extended: 0
         Name: 'EngineMsg'
     Database: [1×1 can.Database]
        Error: 0
       Remote: 0
    Timestamp: 0
         Data: [23 205 0 0 0 0 0 0]
      Signals: [1×1 struct]
     UserData: []


ans = 

  struct with fields:

    VehicleSpeed: 0
       EngineRPM: 5.5003e+03

When you write directly to the signal, the value is translated, scaled, and packed into the message data using the database definition.

msgEngineInfo.Signals.VehicleSpeed = 70.81
msgEngineInfo.Signals
msgEngineInfo = 

  Message with properties:

           ID: 100
     Extended: 0
         Name: 'EngineMsg'
     Database: [1×1 can.Database]
        Error: 0
       Remote: 0
    Timestamp: 0
         Data: [23 205 0 0 71 0 0 0]
      Signals: [1×1 struct]
     UserData: []


ans = 

  struct with fields:

    VehicleSpeed: 71
       EngineRPM: 5.5003e+03

Receive Messages with Database Information

Attach a database to a CAN channel that receives messages to apply database definitions to incoming messages automatically. The database decodes only messages that are defined. All other messages are received in their raw form.

rxCh = canChannel('MathWorks', 'Virtual 1', 2);
rxCh.Database = db
rxCh = 

Summary of CAN Channel using 'MathWorks' 'Virtual 1' Channel 2.

  Channel Parameters:  Bus Speed is 500000.
                       Bus Status is 'N/A'.
                       Transceiver name is 'N/A'.
                       Serial Number of this device is 0.
                       Initialization access is allowed.
                       'demoVNT_CANdbFiles.dbc' database is attached.

              Status:  Offline - Waiting for start.
                       0 messages available to receive.
                       0 messages transmitted since last start.
                       0 messages received since last start.

      Filter History:  Standard ID Filter: Allow All | Extended ID Filter: Allow All

Receive Messages

Start the channel, generate some message traffic and receive messages with physical message decoding.

start(rxCh);
generateMsgsDb();
rxMsg = receive(rxCh, Inf, 'OutputFormat', 'timetable');
rxMsg(1:15, :)
ans =

  15×8 timetable array

        Time        ID     Extended           Name               Data        Length      Signals       Error    Remote
    ____________    ___    ________    ___________________    ___________    ______    ____________    _____    ______

    0.047005 sec    100    false       'EngineMsg'            [1×8 uint8]    8         [1×1 struct]    false    false 
    0.047005 sec    200    false       'TransmissionMsg'      [1×8 uint8]    8         [1×1 struct]    false    false 
    0.047005 sec    400    false       'DoorControlMsg'       [1×8 uint8]    8         [1×1 struct]    false    false 
    0.047005 sec    600    false       'WindowControlMsg'     [1×4 uint8]    4         [1×1 struct]    false    false 
    0.047005 sec    800    false       'SunroofControlMsg'    [1×2 uint8]    2         [1×1 struct]    false    false 
    0.072007 sec    100    false       'EngineMsg'            [1×8 uint8]    8         [1×1 struct]    false    false 
     0.09701 sec    100    false       'EngineMsg'            [1×8 uint8]    8         [1×1 struct]    false    false 
     0.09701 sec    200    false       'TransmissionMsg'      [1×8 uint8]    8         [1×1 struct]    false    false 
     0.12201 sec    100    false       'EngineMsg'            [1×8 uint8]    8         [1×1 struct]    false    false 
     0.14701 sec    100    false       'EngineMsg'            [1×8 uint8]    8         [1×1 struct]    false    false 
     0.14701 sec    200    false       'TransmissionMsg'      [1×8 uint8]    8         [1×1 struct]    false    false 
     0.17202 sec    100    false       'EngineMsg'            [1×8 uint8]    8         [1×1 struct]    false    false 
     0.17202 sec    400    false       'DoorControlMsg'       [1×8 uint8]    8         [1×1 struct]    false    false 
     0.19702 sec    100    false       'EngineMsg'            [1×8 uint8]    8         [1×1 struct]    false    false 
     0.19702 sec    200    false       'TransmissionMsg'      [1×8 uint8]    8         [1×1 struct]    false    false 

Stop the channel and clear it from the workspace.

stop(rxCh);
clear rxCh

Examine a Received Message

Inspect a received message to see the applied database decoding.

rxMsg(10, :)
rxMsg.Signals{10}
ans =

  1×8 timetable array

        Time       ID     Extended       Name           Data        Length      Signals       Error    Remote
    ___________    ___    ________    ___________    ___________    ______    ____________    _____    ______

    0.14701 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 


ans = 

  struct with fields:

    VehicleSpeed: 50
       EngineRPM: 3.5696e+03

Extract All Instances of a Specified Message

Use MATLAB notation to extract all instances of a specified message by name.

allMsgEngine = rxMsg(strcmpi('EngineMsg', rxMsg.Name), :);
allMsgEngine(1:15, :)
ans =

  15×8 timetable array

        Time        ID     Extended       Name           Data        Length      Signals       Error    Remote
    ____________    ___    ________    ___________    ___________    ______    ____________    _____    ______

    0.047005 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
    0.072007 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.09701 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.12201 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.14701 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.17202 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.19702 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.22202 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.24702 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.27203 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.29703 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.32203 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.34703 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.37204 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 
     0.39704 sec    100    false       'EngineMsg'    [1×8 uint8]    8         [1×1 struct]    false    false 

Plot Physical Signal Values

Plot the values of database decoded signals over time. Reference the message timestamps and the signal values in variables.

signalTimetable = canSignalTimetable(rxMsg, 'EngineMsg');
signalTimetable(1:15, :)
plot(signalTimetable.Time, signalTimetable.VehicleSpeed)
title('Vehicle Speed from EngineMsg', 'FontWeight', 'bold')
xlabel('Timestamp')
ylabel('Vehicle Speed')
ans =

  15×2 timetable array

        Time        VehicleSpeed    EngineRPM
    ____________    ____________    _________

    0.047005 sec     0                 250   
    0.072007 sec     0                 250   
     0.09701 sec    50              3569.6   
     0.12201 sec    50              3569.6   
     0.14701 sec    50              3569.6   
     0.17202 sec    50              3569.6   
     0.19702 sec    50              3569.6   
     0.22202 sec    55              3621.3   
     0.24702 sec    55              3621.3   
     0.27203 sec    55              3621.3   
     0.29703 sec    55              3621.3   
     0.32203 sec    55              3663.9   
     0.34703 sec    55              3663.9   
     0.37204 sec    55              3663.9   
     0.39704 sec    55              3663.9   

Was this topic helpful?