Main Content

Get Started with J1939 Communication in MATLAB

This example shows you how to create and use J1939 channels to transmit and receive parameter groups on a J1939 network. This example uses the database file J1939.dbc and MathWorks® virtual CAN channels connected in a loopback configuration.

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-ex33605241\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 the J1939 Channels

Use the function j1939Channel to create J1939 channels on which you can send and receive information.

txCh = j1939Channel(db, "MathWorks", "Virtual 1", 1)
txCh = 
  Channel with properties:

   Device Information:
   -------------------
                  DeviceVendor: 'MathWorks'
                        Device: 'Virtual 1'
            DeviceChannelIndex: 1
            DeviceSerialNumber: 0

   Data Details:
   -------------
      ParameterGroupsAvailable: 0
       ParameterGroupsReceived: 0
    ParameterGroupsTransmitted: 0
                FilterPassList: []
               FilterBlockList: []

   Channel Information:
   --------------------
                       Running: 0
                     BusStatus: 'N/A'
          InitializationAccess: 1
              InitialTimestamp: [0×0 datetime]
                    SilentMode: 0
               TransceiverName: 'N/A'
              TransceiverState: 'N/A'
                      BusSpeed: 500000
                           SJW: []
                         TSEG1: []
                         TSEG2: []
                  NumOfSamples: []

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

rxCh = j1939Channel(db, "MathWorks", "Virtual 1", 2)
rxCh = 
  Channel with properties:

   Device Information:
   -------------------
                  DeviceVendor: 'MathWorks'
                        Device: 'Virtual 1'
            DeviceChannelIndex: 2
            DeviceSerialNumber: 0

   Data Details:
   -------------
      ParameterGroupsAvailable: 0
       ParameterGroupsReceived: 0
    ParameterGroupsTransmitted: 0
                FilterPassList: []
               FilterBlockList: []

   Channel Information:
   --------------------
                       Running: 0
                     BusStatus: 'N/A'
          InitializationAccess: 1
              InitialTimestamp: [0×0 datetime]
                    SilentMode: 0
               TransceiverName: 'N/A'
              TransceiverState: 'N/A'
                      BusSpeed: 500000
                           SJW: []
                         TSEG1: []
                         TSEG2: []
                  NumOfSamples: []

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

Create the J1939 Parameter Groups

Use the function j1939ParameterGroup to create a single-frame parameter group to send on the network.

pgSingleFrame = j1939ParameterGroup(db, "VehicleDataSingle")
pgSingleFrame = 
  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 transmission details and signal data.

pgSingleFrame.SourceAddress = 30;
pgSingleFrame.DestinationAddress = 50;
pgSingleFrame.Signals.VehicleSignal1 = 25;
pgSingleFrame.Signals.VehicleSignal2 = 1000;
pgSingleFrame.Signals
ans = struct with fields:
    VehicleSignal4: -1
    VehicleSignal3: -1
    VehicleSignal2: 1000
    VehicleSignal1: 25

Using the same approach, create a multi-frame parameter group, then set transmission details and signal data.

pgMultiFrame = j1939ParameterGroup(db, "VehicleDataMulti")
pgMultiFrame = 
  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataMulti'
                   PGN: 51200
              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 255 255 255 255]
               Signals: [1×1 struct]

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

pgMultiFrame.SourceAddress = 30;
pgMultiFrame.DestinationAddress = 255;
pgMultiFrame.Signals.VehicleSignal1 = 5;
pgMultiFrame.Signals.VehicleSignal2 = 650;
pgMultiFrame.Signals.VehicleSignal3 = 5000;
pgMultiFrame.Signals
ans = struct with fields:
    VehicleSignal6: -1
    VehicleSignal5: -1
    VehicleSignal4: -1
    VehicleSignal3: 5000
    VehicleSignal2: 650
    VehicleSignal1: 5

Start the J1939 Channels

Use the function start to start the J1939 channels for transmit and receive operations.

start(rxCh);
start(txCh);

Send J1939 Parameter Groups

The transmit function sends parameter groups onto the network. The J1939 channel automatically sends parameter groups requiring multi-frame messaging via its transport protocol.

transmit(txCh, pgSingleFrame)
transmit(txCh, pgSingleFrame)
transmit(txCh, pgMultiFrame)
transmit(txCh, pgSingleFrame)
transmit(txCh, pgSingleFrame)
pause(2);

Receive the Parameter Groups

The receive function retrieves information from the channel which represents messaging that occurred on the network.

pgRx = receive(rxCh, Inf)
pgRx=5×8 timetable
       Time              Name            PGN     Priority        PDUFormatType        SourceAddress    DestinationAddress                        Data                          Signals   
    ___________    _________________    _____    ________    _____________________    _____________    __________________    ____________________________________________    ____________

    0.13955 sec    VehicleDataSingle    40192       6        Peer-to-Peer (Type 1)         30                  50            {[              25 0 232 3 255 255 255 255]}    {1×1 struct}
    0.14347 sec    VehicleDataSingle    40192       6        Peer-to-Peer (Type 1)         30                  50            {[              25 0 232 3 255 255 255 255]}    {1×1 struct}
    0.59386 sec    VehicleDataMulti     51200       6        Peer-to-Peer (Type 1)         30                 255            {[5 0 138 2 136 19 255 255 255 255 255 255]}    {1×1 struct}
    0.76564 sec    VehicleDataSingle    40192       6        Peer-to-Peer (Type 1)         30                  50            {[              25 0 232 3 255 255 255 255]}    {1×1 struct}
    0.7702 sec     VehicleDataSingle    40192       6        Peer-to-Peer (Type 1)         30                  50            {[              25 0 232 3 255 255 255 255]}    {1×1 struct}

Inspect Received Parameter Groups Signals

View details of the received signals for an instance of the single-frame and the multiframe parameter group.

pgRx.Signals{1}
ans = struct with fields:
    VehicleSignal4: -1
    VehicleSignal3: -1
    VehicleSignal2: 1000
    VehicleSignal1: 25

pgRx.Signals{3}
ans = struct with fields:
    VehicleSignal6: -1
    VehicleSignal5: -1
    VehicleSignal4: -1
    VehicleSignal3: 5000
    VehicleSignal2: 650
    VehicleSignal1: 5

Access Signal Values

The j1939SignalTimetable function allows you to easily extract and transform signal data from a timetable of parameter groups.

sigTT = j1939SignalTimetable(pgRx)
sigTT = struct with fields:
     VehicleDataMulti: [1×6 timetable]
    VehicleDataSingle: [4×4 timetable]

sigTT.VehicleDataSingle
ans=4×4 timetable
       Time        VehicleSignal4    VehicleSignal3    VehicleSignal2    VehicleSignal1
    ___________    ______________    ______________    ______________    ______________

    0.13955 sec          -1                -1               1000               25      
    0.14347 sec          -1                -1               1000               25      
    0.76564 sec          -1                -1               1000               25      
    0.7702 sec           -1                -1               1000               25      

sigTT.VehicleDataMulti
ans=1×6 timetable
       Time        VehicleSignal6    VehicleSignal5    VehicleSignal4    VehicleSignal3    VehicleSignal2    VehicleSignal1
    ___________    ______________    ______________    ______________    ______________    ______________    ______________

    0.59386 sec          -1                -1                -1               5000              650                5       

Stop the J1939 Channels

To stop receiving data from the network, stop the J1939 channels using the stop function.

stop(rxCh);
stop(txCh);