Site Information

 Loading... Please wait...

SAE J1939 Programming with Arduino - Arduino Code (Sketch) Advanced Program Version

Posted by Wilfried Voss on

This post is part of a series about SAE J1939 ECU Programming & Vehicle Bus Simulation with Arduino.

SAE J1939 ECU Programming & Vehicle Bus Simulation with Arduino

While the previous programming sample proves the simplicity of developing an SAE J1939 data traffic simulator, I deemed it necessary to improve the code in way that makes simulation extensions and modifications easier. The two elements that needed improvement were the message transmission (which requires assembling the message ID from PGN, priority and source address) and the timer control (which is quite rudimentary).

In this advanced version I replaced the canTransmit function with j1939Transmit, which allows passing the PGN, priority, and source address as separate parameters.

So, instead of:

canTransmit(0x18FEF530, msgAmbientConditions, 8);
canTransmit(0x18FEF320, msgVehiclePosition, 8);

we use:

j1939Transmit(65269, 6, 0x30, msgAmbientConditions, 8);
j1939Transmit(65267, 6, 0x20, msgVehiclePosition, 8);

The timer control adds a higher level of complexity, but it will improve the adaptability of the code. First of all, we need to declare the timers to be used:

struct j1939Timer pTimerVehiclePosition;

In the setup routine, the timers need to be initialized:

j1939TimerReset(&pTimerVehiclePosition);
pTimerVehiclePosition.nCount = 5000;
pTimerVehiclePosition.bStart = true;

In the loop function, we must call the timer control function, which uses a time base of 1 millisecond:

j1939TimerControl();

Also, in the loop function, we must check the timer:

if(pTimerVehiclePosition.bExpired == true)
{

j1939TimerReset(&pTimerVehiclePosition); // Reset the timer
j1939Transmit(65267, 6, 0x20, msgVehiclePosition, 8); // Transmit
pTimerVehiclePosition.nCount = 5000; // Restart the timer
pTimerVehiclePosition.bStart = true;

}// end if

The modified timer version provides a finer timer resolution, which also improves any reaction time of the application, for instance, when it comes to receiving SAE J1939 message frames and modifying the transmitted data according to the received information.

In this case, I refrain from showing another screen shot as a proof of concept. The image is identical to the one in the previous chapter.

Note: This Arduino project is available through the download page at ARD1939 - SAE J1939 Protocol Stack for Aduino.


A Comprehensible Guide to J1939

SAE J1939 has become the accepted industry standard and the vehicle network technology of choice for off-highway machines in applications such as construction, material handling, and forestry machines. J1939 is a higher-layer protocol based on Controller Area Network (CAN). It provides serial data communications between microprocessor systems (also called Electronic Control Units - ECU) in any kind of heavy duty vehicles. The messages exchanged between these units can be data such as vehicle road speed, torque control message from the transmission to the engine, oil temperature, and many more.

A Comprehensible Guide to J1939 is the first work on J1939 besides the SAE J1939 standards collection. It provides profound information on the J1939 message format and network management combined with a high level of readability.

Read more...