J1939 I/O - Using Transport Protocol

This example shows the transmission and reception of J1939 data through a loopback connection of two CAN ports in a single target PC. The message data is Trip Distance Information. The Parameter Group Number and corresponding signals for the message are defined in the SAE J1939-71 document.


J1939 CANdb database: Specify the dbc file in the J1939 Database Setup block.

J1939 CANdb database file is not included in the example. A valid database file with the following message and signal definitions is required:


PGN: 65210 (0xFEBA) - Trip Distance Information / Transmission Repetition: On Request (for this example we use periodic triggering) / Data Length: 12 (that is, Transport Protocol is used) / Data Page: 0 / PDU Format: 254 / PDU Specific: 186 / Default Priority: 7


SPN: 998 - Trip Distance on VSL / Data Length: 4 bytes / Resolution: 0.125 km/bit, 0 offset / Data Range: 0 to 526385151.9 km / Type: Measured

SPN: 999 - Trip Gear Down Distance / Data Length: 4 bytes / Resolution: 0.125 km/bit, 0 offset / Data Range: 0 to 526385151.9 km / Type: Measured

SPN: 1000 - Trip Distance in Top Gear / Data Length: 4 bytes / Resolution: 0.125 km/bit, 0 offset / Data Range: 0 to 526385151.9 km / Type: Measured

CAN FIFO mode I/O: - Make a loopback connection for the two CAN ports.

This example uses Softing CAN PCI card for CAN communication. Configure the model for custom CAN I/O if needed.

Open the model

Click here to open the model: dJ1939_TransportProtocol_SoftingCANdJ1939_TransportProtocol_SoftingCAN.

% Open the model.
mdl = 'dJ1939_TransportProtocol_SoftingCAN';
mdlOpen = 0;
systems = find_system('type', 'block_diagram');
if isempty(strmatch(mdl, systems, 'exact'))
  mdlOpen = 1;

Figure 1: J1939 model using Softing CAN PCI FIFO mode I/O

% The J1939 Protocol Stack block handles J1939 messaging over CAN bus and is connected to CAN FIFO Write and CAN FIFO Read blocks for sending and receiving messages. The transmit and receive CAN ports are chosen
% as 1 and 2 respectively. The protocol stack runs with 10ms sample time. Since this message is a transport protocol message (i.e. data is larger than 8 bytes), the transmission will take place in more than one iteration of the
% protocol stack block. Transport protocol can be enabled or disabled from the J1939 Protocol Stack block dialog.
% In order for a J1939 node to send and receive messages, it needs a corresponding J1939 Controller Application block. In this demo, there are two nodes, indicated as Node 1 and Node 2. Note that each controller application
% (i.e. node) in a J1939 bus needs to have a unique node address. If there are conflicts, new node addresses will be claimed by the Address Claiming layer, which can be enabled in the Network Management tab of the J1939
% Protocol Stack block dialog.
% For this example, the J1939 Message Trigger block outputs a TRUE signal every 50ms and consequently triggers the transmission of the message by the J1939 Transmit Message block.

Build, Download and Run the model

% Build the model and download to the target computer.
set_param(mdl,'RTWVerbose','off'); % Configure for a non-Verbose build.
rtwbuild(mdl);                     % Build and download application.

% Run the model
pause(10); % Let the model run for 10 seconds
### Starting Simulink Real-Time build procedure for model: dJ1939_TransportProtocol_SoftingCAN
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### Successful completion of build procedure for model: dJ1939_TransportProtocol_SoftingCAN
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

Display the Target Computer Scopes

Take a snapshot of the target computer video display. Shown are the signals transmitted from CAN Port1 and received from CAN Port 2. As expected, the transmitted and received signals displayed on the two scopes are identical (see Figure 2).

tg.viewTargetScreen; %Take a snapshot of the target scopes

Figure 2: Target scopes view for the model of Figure 1.

Stop the model

%Stop the model

Close the model

% Close the model if we opened it.
if (mdlOpen)

% EOF dFlexRayLoopback.m
Was this topic helpful?