npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

can-usb-com

v5.0.1

Published

NodeJS interface to CAN-USB-COM Adapter

Downloads

14

Readme

NodeJS CAN-USB-COM Module

This module provides a nodejs interface to the gridconnect CAN-USB Converter.

The module implements the device-specific serial protocol and is not compatible with any other device or adapter.

Getting Started

The following assumes that NodeJS is already installed. To install this module, use

npm install can-usb-com

The usb device appears as a serial port; depending on your platform you may or may not need to install drivers. If a serial port does not appear when you plug in the board, refer to the board installation documents and/or download drivers from https://gridconnect.com/usb-can-interface.html and correct the problem. Note: on Windows, connecting the can-usb-com may cause the mouse to act erratically. To correct this, go to Device Manager, Ports, and choose the COM port that represents the CAN-USB-COM device. Under Properties | Advanced, there is a checkbox called 'Serial Enumerator'. Uncheck that box and the problem will go away.

Simple script to use the board (change the COM port number as required):

const Can = require('can-usb-com');

// Use default settings (do not filter messages)
let board = new Can();


// Handle each incoming message
board.on('data', function( msg ) {
  console.log( 'Msg: ', msg.id.toString(16), msg.ext, msg.buf );
});

// Open the com port and configure...
board.open( 'COM3' )

.then( function() {

  console.log('Listening....');

  // listen for 5 seconds, then end the script
  setTimeout( function() {
    board.close();
    process.exit(0); 
  }, 5000 );

  // send an extended (29-bit ID) message with some data.
  board.write( { id: 0x10EF8001, ext: true, buf: [0x49, 0x2B, 0x06, 0x00, 0x02]);

})
.catch( function( err ) {
  // If anything goes wrong, report the error and exit
  console.error( err );
  board.close();
  process.exit(-1);
});

Streaming

The CAN-USB-COM extends the NodeJS stream interface, so it can be piped to other stream instances. See basic.test.js in the test folder.

Examples

Several complete examples can be found in the example folder.

Configuration

The constructor accepts an object that specifies the desired configuration. The board/ports are set up before the 'open' command resolves (so once the open operation is complete, the CAN interface is ready to use).

The options are as shown in the following example (if you are happy with the option, you can omit it from the options object and the default will be used).

let board = new Can({

  // Serial port baud rate
  baudRate: 115200,

  // bit rate on the CAN bus
  canRate: 250000,

  // typical CAN sample point
  samplePoint: 75,

  // filters for incoming packets
  filters: [
  ],

  // useful for testing, each sent packet is also received
  loopback: false,
  });

Filters

By default, all CAN packets are captured. You can limit the number of incoming frames by using filters. They are specified as an array of up to 10 filter definitions. A filter has these fields:

{
  // true if the filter applies to 29-bit CAN IDs
  ext: true,

  // The ID to accept.  May also be specified as a range:
  // for example '10FF1100 10FF11FF'
  id: '10FF1122'
}

Events

The board object emits the following events:

  • open when the serial port is successfully opened
  • error if an error occurs (like the serial port could not be opened)
  • data when an incoming CANBUS frame is received
  • write when an outgoing CANBUS frame is sent to the device (the event occurs before the frame is actually put on the wire)
  • close when the port is closed

To listen for the events, use the typical NodeJS EventEmitter pattern:

  board.on('open', function(){
    console.log( 'Port opened');
  })

  board.on('data', function(pgn){
    console.log( pgn );
  })

  board.on('close', function(err){
    if( err && err.disconnected ) {
      console.log( 'Port Disconnected' );
    }
    else {
      console.log( 'Port Closed by application' );
    }
  })

  board.on('error', function(err){
    console.log( 'Serial Port error: ', err );
  })

API

API functions generally return Promises, which are resolved or rejected when the request is complete. Refer to NodeJS Promise documentation for details on how to chain requests together, detect errors, etc. Refer to the CAN-USB-COM protocol document for additional details on the use of the commands and valid parameter ranges.

Development

Please note the following if you wish to update or modify this package:

  • eslint rules are included, please lint any changes.
  • Confirm that unit tests are working. To run the tests, use the 'npm test' command.

In order to run tests, you will need at least one CAN-USB-COM device connected to your computer. I think you may have to have the CAN-USB-COM device connected to a properly terminated bus. However, do not run the tests on a bus with active traffic, since receiving unexpected CAN packets will probably confuse the tests.