@csllc/j1939
v1.1.2
Published
J1939 transport layer for CANBUS communication
Downloads
40
Readme
Javascript J1939 CANBUS Package
This package implements a J1939 protocol. It is intended to be used on top of a CANBUS interface package (like can-usb-com
).
The main purpose of this package is for testing and diagnostic purposes. The implementation is not fully J1939 compliant, does not necessarily implement strict packet timings, and may have missing or broken features.
Quick Start
Getting right to sending and receiving messages, let's set up a simple system. We will assume the actual BUS is 250K bits/seconds, and we are using the can-usb-com package, so we install and set up the bus interface
npm install can-usb-com
npm install @csllc/j1939
const Canbus = require('can-usb-com');
const J1939 = require('j1939');
// Create the bus interface
let bus = new Canbus({
canRate: 250000
});
// Create the J1939 protocol instance
let j1939 = new J1939( bus, {
// My ID on the network
address: 0x80
});
// Catch the 'open' event to know when the bus is ready to send and receive
j1939.on('open', (address) => {
console.log('J1939 ready with address ', address);
// Print out any received message
// Of course, in order for this to do anything, you need a device
// on the bus to send a message to our address (0x80)
j1939.on('data', function(msg) {
console.log('Received', msg);
});
// Send a message to device #100
j1939.write({
pgn: 0xEF00,
dst: 100,
priority: 7,
buf: Buffer.alloc(200).fill(0x55),
});
});
Examples
Check the example
or test
folders for more ideas on how to use this module
Methods
write( msg )
Events
address( addressClaimStatus, attemptedSourceAddress)
indicates the progress of the address claim procedure. If it is successful, the open
event will be emitted.
open( address )
signals that the bus is online, and the address claim procedure has completed.
data
signals an incoming PGN, to either the local address or broadcast address
rx
signals an incoming non-J1939 message (11-bit id)
close
indicates that the bus (and protocol) are shut down
error( err )
indicates an error in either the J1939 processing or detected from the CANBUS interface. The error event is NOT triggered for messages that fail to send or be properly received.
Development
eslint rules are stored in .eslintrc, please lint any changes and update/verify proper unit testing
Unit testing
use npm test
to run the unit tests