Main Content

Create and Use J1939 Channels

This example shows you how to use Vehicle Network Toolbox™ with J1939 to create and use J1939 channels to transmit and receive parameter groups on a network. This example uses the CAN database file, J1939.dbc. It also uses MathWorks Virtual CAN channels connected in a loopback configuration

Open the Database File

Open the database file to access the definitions.

db = canDatabase('J1939.dbc')
db = 

  Database with properties:

             Name: 'J1939'
             Path: '/tmp/Bdoc21a_1606923_186772/tpa01cb52b/ex33605241/J1939.dbc'
            Nodes: {2x1 cell}
         NodeInfo: [2x1 struct]
         Messages: {2x1 cell}
      MessageInfo: [2x1 struct]
       Attributes: {3x1 cell}
    AttributeInfo: [3x1 struct]
         UserData: []

Create the J1939 Channels

Create J1939 channels on which you can send and receive information.

txCh = j1939Channel(db, 'MathWorks', 'Virtual 1', 1)
rxCh = j1939Channel(db, 'MathWorks', 'Virtual 1', 2)
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: [0x0 datetime]
                    SilentMode: 0
               TransceiverName: 'N/A'
              TransceiverState: 'N/A'
                      BusSpeed: 500000
                           SJW: []
                         TSEG1: []
                         TSEG2: []
                  NumOfSamples: []

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


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: [0x0 datetime]
                    SilentMode: 0
               TransceiverName: 'N/A'
              TransceiverState: 'N/A'
                      BusSpeed: 500000
                           SJW: []
                         TSEG1: []
                         TSEG2: []
                  NumOfSamples: []

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

Create the J1939 Parameter Groups

You can create J1939 parameter groups to send on the network, setting some tranmission details and signal data into each.

pgSingleFrame = j1939ParameterGroup(db, 'VehicleDataSingle')
pgSingleFrame.SourceAddress = 30;
pgSingleFrame.DestinationAddress = 50;
pgSingleFrame.Signals.VehicleSignal1 = 25;
pgSingleFrame.Signals.VehicleSignal2 = 1000;
pgSingleFrame.Signals

pgMultiFrame = j1939ParameterGroup(db, 'VehicleDataMulti')
pgMultiFrame.SourceAddress = 30;
pgMultiFrame.DestinationAddress = 255;
pgMultiFrame.Signals.VehicleSignal1 = 5;
pgMultiFrame.Signals.VehicleSignal2 = 650;
pgMultiFrame.Signals.VehicleSignal3 = 5000;
pgMultiFrame.Signals
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: [1x1 struct]

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


ans = 

  struct with fields:

    VehicleSignal4: -1
    VehicleSignal3: -1
    VehicleSignal2: 1000
    VehicleSignal1: 25


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: [1x1 struct]

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


ans = 

  struct with fields:

    VehicleSignal6: -1
    VehicleSignal5: -1
    VehicleSignal4: -1
    VehicleSignal3: 5000
    VehicleSignal2: 650
    VehicleSignal1: 5

Start the J1939 Channels

To begin using channels for transmit and receive operations, place them online.

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 multiframe messaging via its transport protocol.

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

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 =

  5x8 timetable

        Time              Name            PGN     Priority        PDUFormatType        SourceAddress    DestinationAddress                        Data                          Signals   
    ____________    _________________    _____    ________    _____________________    _____________    __________________    ____________________________________________    ____________

    0.081428 sec    VehicleDataSingle    40192       6        Peer-to-Peer (Type 1)         30                  50            {[              25 0 232 3 255 255 255 255]}    {1x1 struct}
    0.085101 sec    VehicleDataSingle    40192       6        Peer-to-Peer (Type 1)         30                  50            {[              25 0 232 3 255 255 255 255]}    {1x1 struct}
    0.47238 sec     VehicleDataMulti     51200       6        Peer-to-Peer (Type 1)         30                 255            {[5 0 138 2 136 19 255 255 255 255 255 255]}    {1x1 struct}
    0.67932 sec     VehicleDataSingle    40192       6        Peer-to-Peer (Type 1)         30                  50            {[              25 0 232 3 255 255 255 255]}    {1x1 struct}
    0.68569 sec     VehicleDataSingle    40192       6        Peer-to-Peer (Type 1)         30                  50            {[              25 0 232 3 255 255 255 255]}    {1x1 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}
pgRx.Signals{3}
ans = 

  struct with fields:

    VehicleSignal4: -1
    VehicleSignal3: -1
    VehicleSignal2: 1000
    VehicleSignal1: 25


ans = 

  struct with fields:

    VehicleSignal6: -1
    VehicleSignal5: -1
    VehicleSignal4: -1
    VehicleSignal3: 5000
    VehicleSignal2: 650
    VehicleSignal1: 5

Quickly Access Signal Values

The j1939SignalTimetable function quickly extracts and transforms signal data from a timetable of parameter groups.

sigTT = j1939SignalTimetable(pgRx)
sigTT.VehicleDataSingle
sigTT.VehicleDataMulti
sigTT = 

  struct with fields:

     VehicleDataMulti: [1x6 timetable]
    VehicleDataSingle: [4x4 timetable]


ans =

  4x4 timetable

        Time        VehicleSignal4    VehicleSignal3    VehicleSignal2    VehicleSignal1
    ____________    ______________    ______________    ______________    ______________

    0.081428 sec          -1                -1               1000               25      
    0.085101 sec          -1                -1               1000               25      
    0.67932 sec           -1                -1               1000               25      
    0.68569 sec           -1                -1               1000               25      


ans =

  1x6 timetable

       Time        VehicleSignal6    VehicleSignal5    VehicleSignal4    VehicleSignal3    VehicleSignal2    VehicleSignal1
    ___________    ______________    ______________    ______________    ______________    ______________    ______________

    0.47238 sec          -1                -1                -1               5000              650                5       

Stop the J1939 Channels

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

stop(rxCh);
stop(txCh);