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.

J1939 Examples

Create and Use J1939 Parameter Groups

This example shows you how to use Vehicle Network Toolbox™ with J1939 to create and manage J1939 parameter groups using information stored in CAN database files. This example uses the CAN database file, J1939.dbc.

Open the Database File

Open the database file to access the definitions.

db = canDatabase('J1939.dbc');

Create a Parameter Group

Use the j1939ParameterGroup function to create a parameter group using the 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.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: 254

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

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


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.Signals.VehicleSignal2 = 100
pg.Signals.VehicleSignal3 = 1000
pg.Signals.VehicleSignal4 = 10000
pg.Signals
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 = 

  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 = 

  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 = 

  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: []


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.

pg.Data(1:2) = [50 0]
pg.Signals
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: []


ans = 

  struct with fields:

    VehicleSignal4: 10000
    VehicleSignal3: 1000
    VehicleSignal2: 100
    VehicleSignal1: 50

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');

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

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

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

Create the J1939 Parameter Groups

You can create J1939 parameter groups to send on the network.

pgSingleFrame = j1939ParameterGroup(db, 'VehicleDataSingle')
pgSingleFrame.SourceAddress = 30;
pgSingleFrame.DestinationAddress = 50;
pgMultiFrame = j1939ParameterGroup(db, 'VehicleDataMulti')
pgMultiFrame.SourceAddress = 30;
pgMultiFrame.DestinationAddress = 255;
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: []


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: []

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 = 

  1×5 ParameterGroup array with properties:

   Protocol Data Unit Details:
   ---------------------------
    Name
    PGN
    Priority
    PDUFormatType
    SourceAddress
    DestinationAddress

   Data Details:
   -------------
    Timestamp
    Data
    Signals

   Other Information:
   ------------------
    UserData

Inspect Received Parameter Groups

View details of the received information.

pgRx(1)
ans = 

  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.1530
                  Data: [255 255 255 255 255 255 255 255]
               Signals: [1×1 struct]

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

Note Multiframe Reception

Parameter groups sent using the transport protocol are reconstructed in full detail by the channel.

pgRx(3)
ans = 

  ParameterGroup with properties:

   Protocol Data Unit Details:
   ---------------------------
                  Name: 'VehicleDataMulti'
                   PGN: 51200
              Priority: 6
         PDUFormatType: 'Peer-to-Peer  Type 1 '
         SourceAddress: 30
    DestinationAddress: 255

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

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

Stop the J1939 Channels

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

stop(rxCh);
stop(txCh);

Related Topics

Was this topic helpful?