Vscan j1939 Manual
Vscan j1939 Manual
VSCAN J1939
J1939 Manual
Manual
Edition:
Edition: October
October 2020
2020
Contents
1 Introduction 3
2 Installation 4
2.1 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
7 Test Programs 12
7.1 vs j1939 dump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
7.2 vs j1939 send . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
9 .NET Wrapper 20
1 Introduction
The SAE J1939 protocol is commonly used in the commercial vehicle area for communi-
cation in the commercial vehicle. But there are also other protocols based on J1939 like
NMEA2000 for ships, ISOBUS for agricultural vehicles, MilCAN for military use and in
the FMS (Fleet Management System) standard.
The Controller Area Bus (CAN) is used as the underlying hardware layer which is robust
and has a priority based message protocol. It is also used in retail customer vehicles,
but mostly with proprietary higher level protocols.
The CAN standard was upgraded from 11 bit to 29 bit identifiers to fulfill the need to
embed all J1939 general message information (Protocol Data Unit) in the CAN identifier
field. And a more complex layer was implemented to send more than 8 bytes per J1939
message to its destination. It is the Transport Protocol which is natively supported by
the VSCAN J1939 stack.
You could use all these J1939 features with the rich family set of VScom CAN converters,
which will allow you to use your J1939 device over the USB bus or from all over the
world with our VScom NET-CAN and its VPN functionality.
Please note that the devices must be ordered with the J1939 feature in order to be able
to work with this library. This activation cannot be done after the purchase.
This chart will show you all the layers needed for the use of the VScom J1939 stack:
User
Application
J1939 API
CAN API
CAN
Hardware
2 Installation
2.1 Windows
Copy the files vs can j1939.dll and vs can j1939.lib into your project directory and add
the library definition file (vs can j1939.lib) with the DLL symbols to your project. You
will also need the VSCAN API (vs can api.dll ) which you will find on the CD or on our
website. Finally, you must include the header files vs can j1939.h and vs can api.h.
2.2 Linux
You can chose between a static and shared library. Therefore include the header files
vs can j1939.h and vs can api.h in your project. In the linker stage add one of libvs can j1939.so
(shared) or libvs can j1939.a (static) to the linker options.
The J1939 API relies on the underlying VSCAN API and before you can use the J1939
functions, you have to open the CAN channel with these calls to get a handle to your
VSCAN converter. Please take a look at our VSCAN Manual for further information.
Example:
VSCAN_HANDLE handle = VSCAN_Open("COM3", VSCAN_MODE_NORMAL);
VSCAN_J1939_...();
VSCAN_Close(handle);
Parameters:
Handle
[in] The handle of the CAN device
Name
[in] Describes the name field which will be used to uniquely identify nodes (ECUs) and
their function in a J1939 network. This information will be used in the address claim
procedure (ArbAddrCapable set to 1). Then a lower name field will win an arbitration
process and claims the chosen address. If the ArbAddrCapable is set to 0, the device is
not supporting an arbitration mechanism.
Callback
[in] A callback function which will return addresses for the address claim procedure. If
the first address could not be acquired, the callback is called again and you have to
return a second one. This step could be repeated if the next one also failed.
Example:
VSCAN_J1939_ADDR callback()
{
return 252;
}
VSCAN_J1939_NAME name;
name.ArbAddrCapable = 1;
name.IndustryGroup = 0x01;
name.VehicleSys = 0x01;
name.Func = 0xff;
name.ManCode = 0x66;
The VSCAN_J1939_Free function deallocates all resources that where acquired for the
J1939 stack
VSCAN_J1939_STATUS VSCAN_J1939_Free(VSCAN_HANDLE Handle);
Parameters:
Handle
[in] The handle of the CAN device
Parameters:
Handle
[in] The handle of the CAN device
Buf
[in] An array of VSCAN_J1939_MSG’s that should be written to the CAN bus.
Size
[in] The element size of the Buf parameter.
Written
[out] Size of written J1939 frames to the CAN bus.
Example:
VSCAN_J1939_MSG msg[1];
DWORD written;
msg[0].Dst = 247;
msg[0].Priority = 0x5;
msg[0].PGN = 65262;
msg[0].Data = "testdata";
msg[0].Size = 8;
msg[0].Status = VSCAN_J1939_STATUS_FILL_IN; // make only sense for
// transport protocol (>8 bytes)
The VSCAN_J1939_Request function requests a PGN from another device. You can
read the answer with VSCAN_J1939_Read or prior set up a callback function with
VSCAN_J1939_SetPgnCallback.
VSCAN_J1939_STATUS VSCAN_J1939_Request(VSCAN_HANDLE Handle,
VSCAN_J1939_PGN Pgn,
VSCAN_J1939_ADDR Addr);
Parameters:
Handle
[in] The handle of the CAN device
Pgn
[in] The PGN which should be requested from the destination device.
Addr
[in] The address of the destination device.
Example:
VSCAN_J1939_Request(handle, 65262, 247);
Parameters:
Handle
Parameters:
Handle
[in] The handle of the CAN device
Pgn
[in] PGN number for which a user callback function is set
Callback
[in] A pointer to the user callback function for the specified PGN.
Example:
VOID MyPgnCallback(VSCAN_J1939_MSG *msg)
{
// work with the message
}
Parameters:
Handle
[in] The handle of the CAN device
Example:
DWORD count_of_received_messages = VSCAN_J1939_Read_BufferCount(handle);
Parameters:
Handle
[in] The handle of the CAN device
Example:
VSCAN_J1939_Read_BufferClear(handle);
Example:
VSCAN_J1939_VERSION version = VSCAN_J1939_ApiVersion();
The type definition VSCAN_J1939_STATUS can have one of the following status values.
• VSCAN J1939 ERR OK - indicates that everything is okay
• VSCAN J1939 ERR ERR - indicates a general error
• VSCAN J1939 ERR INVALID HANDLE - indicates that the handle which
is used is not valid (e.g. CAN channel closed)
• VSCAN J1939 ERR INVALID PARAMETER - a parameter which was passed
to the function was invalid
• VSCAN J1939 ERR MEMORY - indicates that there is not enough memory
to complete the function
• VSCAN J1939 ERR SUBAPI - indicates that an error occurred in a subordi-
nated library
• VSCAN J1939 ERR ADDR UNAVAILABLE - indicates that no free ad-
dress was returned from the users callback function
• VSCAN J1939 ERR ADDR CLAIM - indicates that the address claiming
process failed
• VSCAN J1939 ERR DATA TO SMALL - indicates that the passed data was
to small (must be >= 8 bytes)
The structure is used for the information of each J1939 message which will be received
or transmitted.
typedef struct
{
UINT32 PGN; // Parameter Group Number
UINT8 *Data; // data which should be transmitted
UINT16 Size; // size of the data (max 1785 bytes / 7 * 255)
UINT8 Dst; // destination address
UINT8 Src; // our source address
UINT8 Priority; // message priority (max value of 7 / 0 = highest priority)
UINT8 Status; // Tx: one of VSCAN_J1939_STATUS_...
} VSCAN_J1939_MSG;
When you transmit a J1939 message, you must not set the Src field. If you send more
than 8 bytes over the transport protocol, the field Status shows the progress of the mes-
sage on the CAN bus, which consists of multiple CAN messages. To retrieve the Status,
you must set it to VSCAN J1939 STATUS FILL IN prior the VSCAN J1939 Write
call. Then the Status will change during the write to the actual one.
#define VSCAN_J1939_STATUS_SENDING 0x01 // send is in progress
#define VSCAN_J1939_STATUS_SENT 0x02 // send was okay
#define VSCAN_J1939_STATUS_ERROR 0x03 // error during send
#define VSCAN_J1939_STATUS_FILL_IN 0xff // use this status field during write
The structure is used for the initialization of the J1939 stack. Take a look at VS-
CAN J1939 Init for further information.
typedef struct
{
UINT32 Id; // Identity Number
UINT16 ManCode; // Manufacturer Code
UINT8 EcuInst; // ECU (Electronic Control Unit) Instance
UINT8 FuncInst; // Function Instance
UINT8 Func; // Function
UINT8 VehicleSys; // Vehicle System
UINT8 VehicleSysInst; // Vehicle System Instance
UINT8 IndustryGroup; // Industry Group
UINT8 ArbAddrCapable; // Arbitrary Address Capable
} VSCAN_J1939_NAME;
The structure retrieves the J1939 API version information over the function
VSCAN J1939 ApiVersion.
typedef struct
{
UINT8 Major;
UINT8 Minor;
UINT8 SubMinor;
} VSCAN_J1939_VERSION;
Our programming examples repository1 on GitHub provides various J1939 related ex-
amples. README.md gives instructions on how to build and use the source code samples.
7 Test Programs
The vs j1939 dump.exe will open a VScom CAN channel, read and display all J1939
messages on the console.
Usage: vs_j1939_dump.exe <COM Port/IP Address:Port> <Speed-Code>
Speed-Code in kb/s:
1 = 20
2 = 50
3 = 100
4 = 125
5 = 250
6 = 500
7 = 800
8 = 1000
Example:
vs_j1939_dump.exe COM3 5
Version: 1.0
API Version: 1.8.2
J1939 Version: 1.0.0
Source,Destination,Priority,PGN,DataLength,Data
S:000,D:255,P:3,PGN:61443,L:8,FF00FFFFFFFFFFFF
S:000,D:255,P:3,PGN:65132,L:8,FFFFFFFFFFFF4001
S:000,D:255,P:3,PGN:61444,L:8,FFFFFF4001FFFFFF
S:000,D:255,P:3,PGN:61443,L:8,FF00FFFFFFFFFFFF
S:000,D:255,P:3,PGN:65132,L:8,FFFFFFFFFFFF4001
S:000,D:255,P:3,PGN:61443,L:8,FF00FFFFFFFFFFFF
S:000,D:255,P:3,PGN:65132,L:8,FFFFFFFFFFFF4001
S:000,D:255,P:3,PGN:61444,L:8,FFFFFF0001FFFFFF
S:000,D:255,P:3,PGN:61443,L:8,FF00FFFFFFFFFFFF
S:000,D:255,P:3,PGN:65132,L:8,FFFFFFFFFFFF4001
S:000,D:255,P:3,PGN:61443,L:8,FF00FFFFFFFFFFFF
Received 11 frames
The vs j1939 send.exe sends a J1939 test frame over a VScom channel to the CAN bus.
1
https://github.com/visionsystemsgmbh/programming examples/tree/master/CAN/c/vs can api
Example:
vs_j1939_send.exe COM3 5 1000
Version: 1.0
API Version: 1.8.2
J1939 Version: 1.0.0
Sent: 238
8.1 Installation
To install Node.js (JavaScript runtime), you can use the package manager of your dis-
tribution (e.g. apt-get install nodejs”). Many distributions will not supply a recent
”
version, but you can install a newer one directly from the packages of the Node.js site.
Node.js itself has a package manager called Node Package Manager” (npm), which will
”
be installed with the main Node.js package.
Then you can install Node-RED with this package manager globally over sudo npm
”
install -g –unsafe-perm node-red”. For detailed information please take a look at this
page.
Now you can download and extract the VScom Node-RED package node-red-contrib-
”
vscan-wrapper-1.0.0.tgz”. If you want to install it globally, you could run sudo npm
”
install -g <directory>”. If you want to install it locally for your user, you should change
in the directory ˜/.node-red/” and run npm install <directory>”. Then you will find
” ”
the module in the sub-directory node modules”.
”
Afterwards you must install the VScom package node-red-contrib-vscan-1.0.0.tgz” and
”
like the package before, you could install it globally or locally. The wrapper package con-
tains native Linux libraries, which you can copy to your global library path or set an en-
vironment variable to tell your system, that it should also look in this directory, with set
”
LD LIBRARY PATH=˜./node-red/node modules/node-red-contrib-vscan-wrapper”.
Now you’re ready to start node-red for a test with node-red -v” and make a connection
”
to the server with the browser of your choice ( http://<ip address>:1880”). There you
”
will see the vscan node in the input nodes list.
You can use all Node.js methods in the same way as you do with the general API
functions in the chapter VSCAN J1939 Functions.
Example:
var ref = require(’ref’);
var ffi = require(’ffi’);
var vscan = require("./node.js_wrapper.js")
vscan.VSCAN_J1939_Free(canHandle);
vscan.VSCAN_Close(canHandle);
The following example use a hardware curcuit, which emulates a J1939 truck.
The VSCAN node will output all J1939 messages as a VSCAN J1939 MSG structure.
You can retrieve the message in a function node and work with the data program-
matically. You will find an example in the Node-RED menu Import/Examples/vscan”
”
(Figure 1).
If you have placed it on your working area, you should set-up the VSCAN node (Figure
2). Therefore you must double-click it and enter the serial port of your USB-CAN or
enter the IP address and port number of your NET-CAN. When you use the Node-
RED daemon as a normal user, ensure that you are in the group that has access to tty
interfaces, when you use the USB-CAN. In the setting tab, you could also change the
interval at which new messages are passed to the function routine.
To take a look at the filtering code based on the PGNs, please double-click the function
node (Figure 3).
If you have everything set-up and ready, you could deploy the flow to the Node-RED
server and switch over to the dashboard ( http://<ip address>:1880/ui”) to see the
”
output (Figure 4).
9 .NET Wrapper
You can use VB.NET oder C# with the VScom library by including the desired vs can api wrapper.cs/vb
and vs can j1939 api wrapper.cs/vb file to your project. Additionally you must put both
VScom DLLs (vs can api.dll, vs can j1939.dll ) into your application directory.
Here you see a simple example for each language:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using VSCom.CanApi;
namespace VSCom.J1939Api
{
class Test
{
static byte AddressCallback()
{
return 252;
}
try
{
CanDevice.Open("COM3", VSCAN.VSCAN_MODE_NORMAL);
CanDevice.SetSpeed(VSCAN.VSCAN_SPEED_250K);
VSCAN_CODE_MASK codeMask = new VSCAN_CODE_MASK();
codeMask.Code = 0;
codeMask.Mask = 0xffffffff;
CanDevice.SetACCCodeMask(codeMask);
UInt32 read = 0;
for (;;)
{
if (j1939.Read_BufferCount() > 0)
{
j1939.Read(ref msg, 1, ref read);
PrintFrame(msg[0]);
}
}
j1939.Free();
CanDevice.Close();
}
catch(Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
Listing 2: C# Testprogram
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Namespace VSCom.J1939Api
Class Test
name.ManCode = 102
j1939.Init(CanDevice.handle, name, addrCallback)
Dim msg() As VSCAN_J1939.VSCAN_J1939_MSG = New VSCAN_J1939.VSCAN_J1939_MSG((1) - 1)
{}
Dim read As UInt32 = 0
Do While True
If (j1939.Read_BufferCount > 0) Then
j1939.Read(msg, 1, read)
Test.PrintFrame(msg(0))
End If
Loop
j1939.Free()
CanDevice.Close()
Catch ex As Exception
Console.WriteLine(("Error: " + ex.Message))
End Try
End Sub
End Class
End Namespace