- Home
- Documentation
- SAE J1939 ECU Programming And Vehicle Bus Simulation With Arduino Uno, Mega 2560, And Due
SAE J1939 ECU Programming And Vehicle Bus Simulation With Arduino Uno, Mega 2560, And Due
SAE J1939 ECU Programming & Vehicle Bus Simulation with Arduino
By Wilfried Voss
Copyright © by Copperhill Technologies Corporation
After writing Controller Area Networking (CAN) Prototyping with Arduino, the next logical step was looking into higher-layer protocols based on CAN, in this case the SAE J1939 vehicle network protocol. I personally consider J1939 the most effective CAN protocol in the market due to its small memory footprint combined with a bandwidth that easily surpasses other protocols such as CANopen and DeviceNet.
However, my swift choice for SAE J1939 was not only based on technical aspects but also on the great popularity the J1939 protocol enjoys in the North American market. CANopen and DeviceNet, in view of the overpowering strength of Industrial Ethernet for automation, are nearing their virtual end-of-lifetime cycle for new developments and for that reason I dismissed any thoughts of writing a book about them.
The Arduino presented itself again as the obvious choice due to its vast popularity, high-level of user-friendliness and, after all, low costs. The programming and implementation of an SAE J1939 protocol stack using the Arduino requires, of course, not only some knowledge of C/C++ programming but also some basic knowledge of the Controller Area Network (CAN) technology and the SAE J1939 protocol. While I am trying to explain details where necessary (i.e. where the information pertains to programming the code), a fully detailed description of either topic is out of the scope of this book.
For further, detailed information on Controller Area Network and SAE J1939 see my books:
Both titles are available in paperback form through Amazon.com (including all their language specific sites), Barnes & Noble (bn.com), Abebooks.com (including all their language specific sites), and other online bookstores all over the world.
In the same sense, I will also not engage into explaining the basics of the Arduino hardware and its programming. There are myriads of books on Arduino, Arduino Sketches, and Arduino Shields available on the topic, and it makes no sense repeating the information therein just to increase the page count.
I will, however, briefly refer to the hardware used in my J1939 projects, namely the Arduino Uno, the Arduino Mega 2560, and the CAN Shield, but only for the purpose of providing information that will enable the reader to replicate my projects. I deem it also important to point to the hardware’s performance limitations. After all, the Arduino may be the prefect prototyping solution, but implementing, for instance, a full-blown SAE J1939 protocol definitely meant pushing the limits.
Table Of Content
1. Introduction
1.1 The Arduino Uno And Mega 2560 plus CAN Shield
1.1.1 CAN Bus Shield
1.1.1.1 Microchip MCP2515 CAN Bus Controller
1.1.1.2 Arduino CAN Bus Shield by SK Pang electronics
1.1.1.3 CAN Bus Shield by Seeed Studio
1.1.2 Performance Restrictions
1.1.3 Serial Interface And CAN Timing Considerations
1.1.4 Connecting Arduino to a Real SAE J1939 Network
1.1.4.1 SAE J1939/13 Off-Board Diagnostic Connector
1.2 Arduino Programming (Sketches)
1.2.1 The MCP2515 Library
1.2.1.1 Function Calls
1.2.1.2 The CAN Bus Interface
1.2.2 Special Programming Topics
1.2.2.1 Hungarian Notation
1.2.2.2 Debugging Code with Macros
2. A Brief Introduction to the SAE J1939 Protocol
2.1 Introduction to Controller Area Network
2.2 The SAE J1939 Higher-Layer Protocol
2.3 Parameter Group Numbers (PGN)
2.3.1 Parameter Groups
2.3.2 Suspect Parameter Numbers (SPN)
2.3.3 PGN Range
2.4 The Two Elements of the SAE J1939 Protocol Stack
2.4.1 SAE J1939/21 - Transport Protocol (TP)
2.4.1.1 Multi-Packet Broadcast (BAM Session)
2.4.1.2 Multi-Packet Peer-to-Peer (RTS/CTS Session)
2.4.2 SAE J1939/81 - Address Claim Procedure
2.4.2.1 Technical Requirements
2.4.2.2 Device NAME
2.4.2.3 Preferred Address
2.4.2.4 Address Claiming Procedure
2.5 SAE J1939 Compliance Criteria
3. SAE J1939 Applications with the Arduino
3.1 SAE J1939 Monitoring And Simulation
3.1.1 SAE J1939 Simulation Example
3.1.1.1 Message Design
3.1.1.2 Arduino Code
3.1.1.3 Proof of Concept
3.1.1.4 Advanced Program Version
3.1.1.5 SAE J1939 Stress Simulator
3.1.2 SAE J1939 Monitoring Examples
3.1.2.1 Receiving J1939 Message Frames
3.1.2.2 Receiving and Responding to J1939 Request Frames
3.1.3 A Simple SAE J1939 to USB Gateway
3.1.3.1 J1939 Network Scanner
3.1.3.2 J1939 Data Traffic Monitoring
3.1.3.3 J1939 Data Traffic Simulation
3.1.3.4 Visual Studio Code
3.2 ARD1939 - J1939 Protocol Stack for Arduino
3.2.1 ARD1939 - Function Overview
3.2.2 ARD1939 - Implementation
3.2.2.1 Functionality Settings
3.2.2.2 Program Structure
3.2.3 Proof of Concept
3.2.3.1 Address Claim Procedure (SAE J1939/81)
3.2.3.1.1 Claiming Address With No Contending Node
3.2.3.1.2 Claiming Address With Contending Node
3.2.3.2 Sending and Receiving Messages
3.2.3.3 Transport Protocol – BAM Session (SAE J1939/21)
3.2.3.3.1 Message Timing
3.2.3.4 Transport Protocol – RTS/CTS Session (SAE J1939/21)
3.2.3.4.1 Message Timing
3.2.3.4.2 Clear to Send Timeout
3.2.3.4.3 End of Message Acknowledgment Timeout
3.2.3.4.4 RTS/CTS Session Test Program
3.2.4 ARD1939 - Sample Application
4. Conclusion
Appendix A – Debugging Macros
Appendix B - ARD1939 Protocol Stack Reference
Appendix C – Recommended Literature
ARD1939 – SAE J1939 Protocol Stack for Arduino
I deem it necessary to add a few non-technical (and maybe politically incorrect) aspects on the development of the most interesting feature of this book, the ARD1939, an SAE J1939 protocol stack for Arduino.
The implementation of an SAE J1939 protocol stack was (and in many cases still is) out of financial reach for many engineers. The software (i.e. the source code) is either grossly overpriced or comes with hefty royalties (object code, libraries), meaning you have to pay for each copy. At least in the case of the Arduino hardware, this is going to change, and the ARD1939 protocol stack project is available as a free download.
Quite honestly, the development of a J1939 protocol is not a big deal for an experienced programmer, the only obstacle being that you have to spend some good money on the official document, the SAE J1939 Standards Collection. I deem my code as good as any commercially available protocol stacks. And yes, I have successfully tested my code against a number of commercially available J1939 devices.
I had contemplated releasing ARD1939 in form of the original source code but ultimately decided against it, mostly out of respect for those small businesses that make a living from selling SAE J1939 devices and software tools. Instead, I provide a pre-compiled code.
The original source code could be easily adapted to any other embedded hardware (I have it already running on an ARM system) and even Windows or Linux PCs with CAN capabilities.
Furthermore, the costs for off-the-shelf, industrial-strength hardware with an extended temperature range are surprisingly low these days. In my experience, you can easily create an SAE J1939 node with USB and Ethernet port (gateway application) with industrial-strength enclosure for under US$200 at volume = 1.
I believe that the average Arduino user, through using the pre-compiled ARD1939 code, will be quite able to write effective J1939 applications, regardless of whether or not he/she has access to the original source code. After all, there is no real need to modify a working code. It supports all SAE J1939 protocol features, and the focus should always be on the actual application.
About the Author
I am the author of the “Comprehensible Guide” series of technical literature covering topics like Controller Area Network (CAN), SAE J1939, Industrial Ethernet, and Servo Motor Sizing. I have worked in the CAN industry since 1997 and before that was a motion control engineer in the paper manufacturing industry. I have a master’s degree in electrical engineering from the University of Wuppertal in Germany.
During the past years, I have conducted numerous seminars on industrial fieldbus systems such as CAN, CANopen, SAE J1939, Industrial Ethernet, and more during various Real Time Embedded And Computing Conferences (RTECC), ISA (Instrumentation, Systems, and Automation Society) conferences and various other events all over the United States and Canada.
I had the opportunity of traveling the world extensively, but settled in New England in 1989. I presently live in an old farmhouse in Greenfield, Massachusetts with my red-haired, green-eyed Irish-American wife and our son Patrick.
For more information on my works and to contact me, feel free to browse for more books on this website.
Contact the Author
Despite all efforts in preparing this book, there is always the possibility that some aspects or facts will not find everybody’s approval, which prompts us, the author and the publisher, to ask for your feedback. If you would like to propose any amendments or corrections, please send us your comment. We look forward to any support in supplementing this book, and we welcome all discussions that contribute to making the topic of this book as thorough and objective as possible.
To submit amendments and corrections please contact us through this website and leave a note.
Download the Code
Each programming example in this book, either Arduino C/C++ Sketches or Visual Studio C# projects, are available as a free download. This also includes the ARD1939 protocol stack project.
I contemplated listing all Arduino projects and their code in printing at the end of this book, not as an attempt of increasing the page count, but due to paying respect to those who unable to download the code (yes, they do exist). However, all Arduino sketches use the MPC2515 Library by Cory Fowler (See chapter Arduino Programming (Sketches)), which I cannot provide in printing due to copyright restraints. In all consequence, in order to fully use the code as introduced in this book, you do need an Internet connection for downloads.
All Arduino sketches and other code samples and projects as introduced in this book are free software; you can redistribute and/or modify them. The programs are introduced in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. With downloading these programs, you confirm that these code samples and projects were created for demonstration and educational purpose only.
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher.
All trademarks or copyrights mentioned herein are the possession of their respective owners and Copperhill Media makes no claim of ownership by the mention of products that contain these marks.
“Arduino” is a trademark of the Arduino team.
Disclaimer: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties or merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any loss or profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages.