0% found this document useful (0 votes)
142 views22 pages

Vscan j1939 Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views22 pages

Vscan j1939 Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

VSCAN

VSCAN J1939
J1939 Manual
Manual
Edition:
Edition: October
October 2020
2020

Tel: +49 40 528 401 0


Fax: +49 40 528 401 99
Web: www.visionsystems.de
Support: faq.visionsystems.de
Contents

Contents

1 Introduction 3

2 Installation 4
2.1 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

3 VSCAN API Functions 5

4 VSCAN J1939 Functions 5


4.1 VSCAN J1939 Init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.2 VSCAN J1939 Free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3 VSCAN J1939 Write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.4 VSCAN J1939 Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.5 VSCAN J1939 Read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.6 VSCAN J1939 SetPgnCallback . . . . . . . . . . . . . . . . . . . . . . . . 8
4.7 VSCAN J1939 Read BufferCount . . . . . . . . . . . . . . . . . . . . . . 9
4.8 VSCAN J1939 Read BufferClear . . . . . . . . . . . . . . . . . . . . . . . 9
4.9 VSCAN J1939 ApiVersion . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5 VSCAN J1939 Types and Structures 10


5.1 VSCAN J1939 STATUS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.2 VSCAN J1939 MSG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.3 VSCAN J1939 NAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.4 VSCAN J1939 VERSION . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.5 VSCAN J1939 ADDR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.6 VSCAN J1939 PGN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

6 Source Code Examples 12

7 Test Programs 12
7.1 vs j1939 dump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
7.2 vs j1939 send . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

8 Node.js and Node-RED Module 14


8.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
8.2 Node.js Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
8.3 Node-RED Module and Example . . . . . . . . . . . . . . . . . . . . . . . 15

9 .NET Wrapper 20

Version 1.2 VSCAN J1939 Manual 2


1 INTRODUCTION

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

Version 1.2 VSCAN J1939 Manual 3


2 INSTALLATION

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.

Version 1.2 VSCAN J1939 Manual 4


4 VSCAN J1939 FUNCTIONS

3 VSCAN API Functions

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_Ioctl(handle, VSCAN_IOCTL_SET_SPEED, VSCAN_SPEED_250K);

VSCAN_J1939_...();

VSCAN_Close(handle);

4 VSCAN J1939 Functions

4.1 VSCAN J1939 Init

The VSCAN_J1939_Init function initializes the VSCAN J1939 stack.


VSCAN_J1939_STATUS VSCAN_J1939_Init(VSCAN_HANDLE Handle,
VSCAN_J1939_NAME Name,
VSCAN_J1939_ADDR_CALLBACK Callback);

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;
}

Version 1.2 VSCAN J1939 Manual 5


4 VSCAN J1939 FUNCTIONS

VSCAN_J1939_NAME name;

name.ArbAddrCapable = 1;
name.IndustryGroup = 0x01;
name.VehicleSys = 0x01;
name.Func = 0xff;
name.ManCode = 0x66;

VSCAN_J1939_Init(handle, name, callback);

4.2 VSCAN J1939 Free

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

4.3 VSCAN J1939 Write

The VSCAN_J1939_Write function writes J1939 frames to the CAN bus.


VSCAN_J1939_STATUS VSCAN_J1939_Write(VSCAN_HANDLE Handle,
VSCAN_J1939_MSG *Buf,
DWORD Size,
DWORD *Written);

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:

Version 1.2 VSCAN J1939 Manual 6


4 VSCAN J1939 FUNCTIONS

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)

VSCAN_J1939_Write(handle, msg, 1, &written);

4.4 VSCAN J1939 Request

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);

4.5 VSCAN J1939 Read

The VSCAN_J1939_Read function receives J1939 messages for the device.


VSCAN_J1939_STATUS VSCAN_J1939_Read(VSCAN_HANDLE Handle,
VSCAN_J1939_MSG *Buf,
DWORD Size,
DWORD *Read);

Parameters:
Handle

Version 1.2 VSCAN J1939 Manual 7


4 VSCAN J1939 FUNCTIONS

[in] The handle of the CAN device


Buf
[out] An array of VSCAN_J1939_MSG’s which is filled with received messages
Size
[in] The element size of the Buf parameter
Read
[out] Size of received messages in Buf
Example:
VSCAN_J1939_MSG msg[32];
DWORD read;

VSCAN_J1939_Read(handle, msg, 32, &read);

4.6 VSCAN J1939 SetPgnCallback

The callback function set over the VSCAN_J1939_SetPgnCallback function receives


J1939 messages for the device. If you have set a callback and want to remove it, you
could set the Callback parameter to NULL.
VSCAN_J1939_STATUS VSCAN_J1939_SetPgnCallback(VSCAN_HANDLE Handle,
VSCAN_J1939_PGN Pgn,
VSCAN_J1939_PGN_CALLBACK Callback);

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
}

VSCAN_J1939_SetPgnCallback(handle, 65262, MyPgnCallback);

// remove the callback function for the PGN


VSCAN_J1939_SetPgnCallback(handle, 65262, NULL);

Version 1.2 VSCAN J1939 Manual 8


4 VSCAN J1939 FUNCTIONS

4.7 VSCAN J1939 Read BufferCount

The VSCAN_J1939_Read_BufferCount function returns the number of messages in


the J1939 receive buffer.
DWORD VSCAN_J1939_Read_BufferCount(VSCAN_HANDLE Handle);

Parameters:
Handle
[in] The handle of the CAN device
Example:
DWORD count_of_received_messages = VSCAN_J1939_Read_BufferCount(handle);

4.8 VSCAN J1939 Read BufferClear

The VSCAN_J1939_Read_BufferClear function deletes all messages in the receive


buffer.
VOID VSCAN_J1939_Read_BufferClear(VSCAN_HANDLE Handle);

Parameters:
Handle
[in] The handle of the CAN device
Example:
VSCAN_J1939_Read_BufferClear(handle);

4.9 VSCAN J1939 ApiVersion

The VSCAN_J1939_ApiVersion function returns the VSCAN J1939 VERSION struc-


ture with version information.
VSCAN_J1939_VERSION VSCAN_J1939_ApiVersion();

Example:
VSCAN_J1939_VERSION version = VSCAN_J1939_ApiVersion();

Version 1.2 VSCAN J1939 Manual 9


5 VSCAN J1939 TYPES AND STRUCTURES

5 VSCAN J1939 Types and Structures

5.1 VSCAN J1939 STATUS


typedef int VSCAN_J1939_STATUS;

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)

5.2 VSCAN J1939 MSG

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,

Version 1.2 VSCAN J1939 Manual 10


5 VSCAN J1939 TYPES AND STRUCTURES

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

5.3 VSCAN J1939 NAME

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;

5.4 VSCAN J1939 VERSION

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;

5.5 VSCAN J1939 ADDR

typedef UINT8 VSCAN_J1939_ADDR;

5.6 VSCAN J1939 PGN

typedef UINT32 VSCAN_J1939_PGN;

Version 1.2 VSCAN J1939 Manual 11


7 TEST PROGRAMS

6 Source Code Examples

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

7.1 vs j1939 dump

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

7.2 vs j1939 send

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

Version 1.2 VSCAN J1939 Manual 12


7 TEST PROGRAMS

Usage: %s <COM Port/IP Address:Port> <Speed-Code> [Loop]


Speed-Code in kb/s:
1 = 20
2 = 50
3 = 100
4 = 125
5 = 250
6 = 500
7 = 800
8 = 1000

Example:
vs_j1939_send.exe COM3 5 1000

Version: 1.0
API Version: 1.8.2
J1939 Version: 1.0.0

Sent: 238

Version 1.2 VSCAN J1939 Manual 13


8 NODE.JS AND NODE-RED MODULE

8 Node.js and Node-RED Module

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.

8.2 Node.js Module

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")

var addrCallback = ffi.Callback(vscan.vsJ1939Addr, [],


function() {
return 252;
});

// make an extra reference to the callback pointer to avoid GC


process.on(’exit’, function() {
addrCallback
});

Version 1.2 VSCAN J1939 Manual 14


8 NODE.JS AND NODE-RED MODULE

// switch on debugging on console


//var status = vscan.VSCAN_Ioctl(0, VSCAN_IOCTL_SET_DEBUG, VSCAN_DEBUG_HIGH);
//status = vscan.VSCAN_Ioctl(0, VSCAN_IOCTL_SET_DEBUG_MODE, VSCAN_DEBUG_MODE_CONSOLE);

var version = vscan.VSCAN_J1939_ApiVersion();


console.log(‘J1939 Version: ${version.Major}.${version.Minor}.${version.SubMinor}‘);

var canHandle = vscan.VSCAN_Open(’/dev/ttyUSB0’, 0);


if (canHandle < 0) {
console.log(’could not open CAN channel’);
process.exit(-1);
}

var codeMask = new vscan.vsCanCodeMask;


codeMask.Code = 0;
codeMask.Mask = 0xffffffff;
status = vscan.VSCAN_Ioctl(canHandle, vscan.VSCAN_IOCTL_SET_ACC_CODE_MASK, codeMask.ref());
status = vscan.VSCAN_Ioctl(canHandle, vscan.VSCAN_IOCTL_SET_SPEED, vscan.VSCAN_SPEED_250K);

var name = new vscan.vsJ1939Name;


name.ArbAddrCapable = 0;
name.IndustryGroup = 0x01;
name.VehicleSys = 0x01;
name.Func = 0xff;
name.ManCode = 0x66;

status = vscan.VSCAN_J1939_Init(canHandle, name, addrCallback);

var msg = new vscan.vsJ1939Msg;


var read = ref.alloc(’uint32’);
for (;;) {
var cnt = vscan.VSCAN_J1939_Read_BufferCount(canHandle);
if (cnt > 0) {
vscan.VSCAN_J1939_Read(canHandle, msg.ref(), 1, read);
console.log("S:%d,D:%d,P:%d,PGN:%d,L:%d,%s",
msg.Src, msg.Dst, msg.Priority, msg.Pgn, msg.Size, msg.Data.toString(’hex’, 0, msg.Size));
}
}

vscan.VSCAN_J1939_Free(canHandle);
vscan.VSCAN_Close(canHandle);

Listing 1: Node.js Testprogram

8.3 Node-RED Module and Example

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).

Version 1.2 VSCAN J1939 Manual 15


8 NODE.JS AND NODE-RED MODULE

Figure 1: VSCAN J1939 Truck Example (Import)

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.

Version 1.2 VSCAN J1939 Manual 16


8 NODE.JS AND NODE-RED MODULE

Figure 2: VSCAN J1939 Truck Example (Config)

To take a look at the filtering code based on the PGNs, please double-click the function
node (Figure 3).

Version 1.2 VSCAN J1939 Manual 17


8 NODE.JS AND NODE-RED MODULE

Figure 3: VSCAN J1939 Truck Example (Code)

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).

Version 1.2 VSCAN J1939 Manual 18


8 NODE.JS AND NODE-RED MODULE

Figure 4: VSCAN J1939 Truck Example (Dashboard)

Version 1.2 VSCAN J1939 Manual 19


9 .NET WRAPPER

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;
}

public static string ByteArrayToString(byte[] ba)


{
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}

static void PrintFrame(VSCAN_J1939.VSCAN_J1939_MSG msg)


{
Console.WriteLine(String.Format("S:{0},D:{1},P:{2},PGN:{3},L:{4},{5}",
msg.Src, msg.Dst, msg.Priority, msg.PGN, msg.Size, ByteArrayToString(msg.Data)));
}

static void Main(string[] args)


{
VSCAN CanDevice = new VSCAN();
VSCAN_J1939 j1939 = new VSCAN_J1939();
VSCAN_J1939.VSCAN_J1939_ADDR_CALLBACK addrCallback = new VSCAN_J1939.
VSCAN_J1939_ADDR_CALLBACK(AddressCallback);

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);

VSCAN_J1939.VSCAN_J1939_NAME name = new VSCAN_J1939.VSCAN_J1939_NAME();


name.ArbAddrCapable = 0;
name.IndustryGroup = 0x01;
name.VehicleSys = 0x01;
name.Func = 0xff;
name.ManCode = 0x66;

j1939.Init(CanDevice.Handle, name, addrCallback);

VSCAN_J1939.VSCAN_J1939_MSG[] msg = new VSCAN_J1939.VSCAN_J1939_MSG[1];

UInt32 read = 0;

Version 1.2 VSCAN J1939 Manual 20


9 .NET WRAPPER

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

Private Shared Function AddressCallback() As Byte


Return 252
End Function

Public Shared Function ByteArrayToString(ByVal ba() As Byte) As String


Dim hex As String = BitConverter.ToString(ba)
Return hex.Replace("-", "")
End Function

Private Shared Sub PrintFrame(ByVal msg As VSCAN_J1939.VSCAN_J1939_MSG)


Console.WriteLine(String.Format("S:{0},D:{1},P:{2},PGN:{3},L:{4},{5}", msg.Src, msg.Dst
, msg.Priority, msg.PGN, msg.Size, Test.ByteArrayToString(msg.Data)))
End Sub

Public Shared Sub Main(ByVal args() As String)


Dim CanDevice As VSCAN = New VSCAN
Dim j1939 As VSCAN_J1939 = New VSCAN_J1939
Dim addrCallback As VSCAN_J1939.VSCAN_J1939_ADDR_CALLBACK = New VSCAN_J1939.
VSCAN_J1939_ADDR_CALLBACK(AddressOf AddressCallback)
Try
CanDevice.Open("COM3", VSCAN.VSCAN_MODE_NORMAL)
CanDevice.SetSpeed(VSCAN.VSCAN_SPEED_250K)
Dim codeMask As VSCAN_CODE_MASK = New VSCAN_CODE_MASK
codeMask.Code = 0
codeMask.Mask = &HFFFFFFFFUI
CanDevice.SetACCCodeMask(codeMask)
Dim name As VSCAN_J1939.VSCAN_J1939_NAME = New VSCAN_J1939.VSCAN_J1939_NAME
name.ArbAddrCapable = 0
name.IndustryGroup = 1
name.VehicleSys = 1
name.Func = 255

Version 1.2 VSCAN J1939 Manual 21


9 .NET WRAPPER

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

Listing 3: VB.NET Testprogram

Version 1.2 VSCAN J1939 Manual 22

You might also like