Load .dbc Files and Create Messages

Load the CAN Database

To use a CAN database file, load the database into your MATLAB® session. At the MATLAB command prompt, type:

db = canDatabase('filename.dbc')

Here db is a variable you chose for your database handle and filename.dbc is the actual file name of your CAN database. If your CAN database is not in the current working directory, type the path to the database:

db = canDatabase('path\filename.dbc')

    Tip   CAN database file names containing non-alphanumeric characters such as equal signs, ampersands, and so forth are incompatible with Vehicle Network Toolbox™. You can use periods in your database name. Rename any CAN database files with non-alphanumeric characters before you use them.

This command returns a database object that you can use to create and interpret CAN messages using information stored in the database. Refer to the canDatabase function for more information.

Create a CAN Message

This example shows you how to create a message using a database constructed specifically for this example. You can access this database in the Toolbox > VNT > VNTDemos subfolder in your MATLAB installation folder. This database has a message, EngineMsg. To try this example, create messages and signals using definitions in your own database.

  1. Create the CAN database object:

    d = canDatabase('demoVNT_CANdbFiles.dbc')
  2. Create a CAN message using the message name in the database:

    message = canMessage(d, 'EngineMsg')

Access Signals in the Constructed CAN Message

You can access the two signals defined for the message you created in the example database, message. You can also change the values for some signals.

  1. To display signals in your message, type:

    sig = (message.Signals)

    Signals in the message are displayed as follows:

    sig = 
    
        VehicleSpeed: 0
           EngineRPM: 250
  2. Change the value of the EngineRPM signal:

    message.Signals.EngineRPM = 300
  3. Display signal information again to see the change:

    sig
    
    sig = 
    
        VehicleSpeed: 0
           EngineRPM: 300

Add a Database to a CAN Channel

To add a database to the CAN channel canch, type:

canch.Database = canDatabase('Mux.dbc') 

For more information, see the Database property.

Update Database Information

When you make changes to a database file:

  1. Reload the database file into your MATLAB session using the canDatabase function.

  2. Reattach the database to messages using the attachDatabase function.

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 = 

  can.Database handle
  Package: can

  Properties:
        Name: 'demoVNT_CANdbFiles'
        Path: [1x75 char]
    Messages: {5x1 cell}
    UserData: []



ans = 

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

        Name: 'EngineMsg'
     Comment: ''
          ID: 100
    Extended: 0
      Length: 8
     Signals: {2x1 cell}

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

messageInfo(db)
ans = 

5x1 struct array with fields:
    Name
    Comment
    ID
    Extended
    Length
    Signals

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 = 

             Name: 'EngineRPM'
         StartBit: 0
       SignalSize: 32
        ByteOrder: 'LittleEndian'
           Signed: 0
        ValueType: 'Integer'
            Class: 'uint32'
           Factor: 0.1000
           Offset: 250
          Minimum: 250
          Maximum: 9500
            Units: 'rpm'
          Comment: ''
      Multiplexor: 0
      Multiplexed: 0
    MultiplexMode: 0

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

signalInfo(db, 'EngineMsg')
ans = 

2x1 struct array with fields:
    Name
    StartBit
    SignalSize
    ByteOrder
    Signed
    ValueType
    Class
    Factor
    Offset
    Minimum
    Maximum
    Units
    Comment
    Multiplexor
    Multiplexed
    MultiplexMode

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 = 

  can.Message handle
  Package: can

  Properties:
           ID: 100
     Extended: 0
         Name: 'EngineMsg'
     Database: [1x1 can.Database]
        Error: 0
       Remote: 0
    Timestamp: 0
         Data: [0 0 0 0 0 0 0 0]
      Signals: [1x1 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 = 

    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 = 

  can.Message handle
  Package: can

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



ans = 

    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 = 

  can.Message handle
  Package: can

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



ans = 

    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('Vector', 'Virtual 1', 2);
rxCh.Database = db
rxCh = 

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

  Channel Parameters:  Bus Speed is 500000.
                       Bus Status is 'N/A'.
                       Transceiver name is ''.
                       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)
rxMsg = 

  1x418 can.Message handle
  Package: can

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


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(10).Signals
ans = 

  can.Message handle
  Package: can

  Properties:
           ID: 100
     Extended: 0
         Name: 'EngineMsg'
     Database: [1x1 can.Database]
        Error: 0
       Remote: 0
    Timestamp: 0.1746
         Data: [88 134 0 0 52 0 0 0]
      Signals: [1x1 struct]
     UserData: []



ans = 

    VehicleSpeed: 52
       EngineRPM: 3.6892e+03

Extract Most Recent Message by Name

Use extractRecent and specify a message name to extract the most recent occurrence of a message.

msgRecentWindows = extractRecent(rxMsg, 'WindowControlMsg')
msgRecentWindows = 

  can.Message handle
  Package: can

  Properties:
           ID: 600
     Extended: 0
         Name: 'WindowControlMsg'
     Database: [1x1 can.Database]
        Error: 0
       Remote: 0
    Timestamp: 5.5749
         Data: [64 62 0 0]
      Signals: [1x1 struct]
     UserData: []


Extract All Instances of a Specified Message by Name

Use extractAll and specify a message name to extract all instances of a specified message.

allMsgEngine = extractAll(rxMsg, 'EngineMsg')
allMsgEngine = 

  1x225 can.Message handle
  Package: can

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


Plot Physical Signal Values

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

signals = [allMsgEngine.Signals]
plot([allMsgEngine.Timestamp], [signals.VehicleSpeed])
title('Vehicle Speed from EngineMsg', 'FontWeight', 'bold')
xlabel('Timestamp')
ylabel('Vehicle Speed')
axis([0 6 0 75])
signals = 

1x225 struct array with fields:
    VehicleSpeed
    EngineRPM

Was this topic helpful?