Main Content

Get Started with J1939 Parameter Groups in MATLAB

This example shows you how to create and manage J1939 parameter groups using information stored in DBC files. This example uses file J1939.dbc. Creating and using parameter groups this way is recommended when needing to transmit data to a J1939 network.

Open the DBC File

Open the DBC file using canDatabase to access the definitions.

db = canDatabase("J1939.dbc")
db = 
  Database with properties:

             Name: 'J1939'
             Path: 'C:\Users\michellw\OneDrive - MathWorks\Documents\MATLAB\Examples\vnt-ex46196345\J1939.dbc'
            Nodes: {2×1 cell}
         NodeInfo: [2×1 struct]
         Messages: {2×1 cell}
      MessageInfo: [2×1 struct]
       Attributes: {3×1 cell}
    AttributeInfo: [3×1 struct]
         UserData: []

Create a Parameter Group

Use the j1939ParameterGroup function to create a parameter group using information contained within the database.

pg = j1939ParameterGroup(db, "VehicleDataSingle")
pg = 
  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 6
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 254
    DestinationAddress: 254

   Data Details:
   -------------
             Timestamp: 0
                  Data: [255 255 255 255 255 255 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []

Set Source and Destination Addresses

To fully define the parameter group and determine the logistics of its transmission on a network, set the source and destination addresses.

pg.SourceAddress = 30
pg = 
  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 6
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 254

   Data Details:
   -------------
             Timestamp: 0
                  Data: [255 255 255 255 255 255 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []

pg.DestinationAddress = 50
pg = 
  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 6
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [255 255 255 255 255 255 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []

Set Priority

Set the Priority property to further customize the transmission.

pg.Priority = 5;

View Signal Information

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

pg.Signals
ans = struct with fields:
    VehicleSignal4: -1
    VehicleSignal3: -1
    VehicleSignal2: -1
    VehicleSignal1: -1

Change Signal Information

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

pg.Signals.VehicleSignal1 = 10
pg = 
  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 5
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [10 0 255 255 255 255 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []

pg.Signals.VehicleSignal2 = 100
pg = 
  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 5
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [10 0 100 0 255 255 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []

pg.Signals.VehicleSignal3 = 1000
pg = 
  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 5
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [10 0 100 0 232 3 255 255]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []

pg.Signals.VehicleSignal4 = 10000
pg = 
  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 5
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [10 0 100 0 232 3 16 39]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []

pg.Signals
ans = struct with fields:
    VehicleSignal4: 10000
    VehicleSignal3: 1000
    VehicleSignal2: 100
    VehicleSignal1: 10

Write New Direct Data

You can also write values directly into the Data property, although setting values through Signals is generally recommended and preferred.

pg.Data(1:2) = [50 0]
pg = 
  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataSingle'
                   PGN: 40192
              Priority: 5
         PDUFormatType: 'Peer-to-Peer (Type 1)'
         SourceAddress: 30
    DestinationAddress: 50

   Data Details:
   -------------
             Timestamp: 0
                  Data: [50 0 100 0 232 3 16 39]
               Signals: [1×1 struct]

   Other Information:
   ------------------
              UserData: []

pg.Signals
ans = struct with fields:
    VehicleSignal4: 10000
    VehicleSignal3: 1000
    VehicleSignal2: 100
    VehicleSignal1: 50