Main Content

Basic J1939 Communication Over CAN

This example shows how to perform basic J1939 communication over CAN protocol.

Simulink Real-Time provides J1939 blocks for transmitting and receiving Parameter Groups in Simulink models over Controller Area Networks (CAN). This example performs data transfer over a CAN bus using the J1939 Network Configuration, J1939 Node Configuration, J1939 CAN Transport Layer, J1939 Transmit, and J1939 Receive blocks. The communication takes place on a Speedgoat IO612 module in the target computer.

Open Model

Connect to the target computer. Identify the system target file (STF) for the target computer. Open the model. Configure the STF for the model.

tg = slrealtime;
connect(tg);
modelSTF = getSTFName(tg);
model = 'slrt_ex_j1939_can_communication';
open_system(model);
Warning: Unrecognized function or variable 'CloneDetectionUI.internal.CloneDetectionPerspective.register'.
set_param(model,"SystemTargetFile",modelSTF);

model-j1933-can-communication.png

J1939 Block Parameters

The model sets up J1939 communication over the network. The model is configured to transmit and receive a single-frame message between two nodes defined in the J1939 DBC file.

  • The J1939 Network Configuration block selects file J1939.dbc. This J1939 database file defines two network nodes Node1 and Node2, a single-frame message VehicleDataSingle, and a multi-frame message VehicleDataMulti.

  • The J1939 CAN Transport Layer block sets the Device to a Speedgoat IO612 module. The transport layer is configured to transfer J1939 messages over CAN via the specified channel.

  • The Simulink source blocks such as Counter Limited and Constant connect to the inputs of a J1939 Transmit block. The J1939 Transmit block is set to queue data for transmission at each timestep when the Trigger port is enabled. For this example, a periodic trigger subsystem sends a high pulse every 50 milliseconds.

  • The J1939 Receive block receives the messages transmitted over the network.

Build, Load, and Start Application

Build the real-time application from the model. Load and run the application.

The start(tg) function is commented. To start the application, a Speedgoat IO612 module must be available on the PCI bus.

evalc('slbuild(model)');
load(tg,model);
% start(tg);

Stop Application and Close Model

Stop the real-time application. Close the model.

The stop(tg) function is commented because the start(tg) function is commented above.

% stop(tg);
bdclose(model);