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

discobus

v1.0.2

Published

The Disco Bus protocol, is a versatile master/slave protocol well suited for multidrop networks, like RS485.

Downloads

34

Readme

Disco Bus communication library

Build Status

The Disco Bus protocol, is a versatile master/slave protocol well suited for multidrop networks, like RS485. Put simply, it's an easy way to communication with many devices over a pair of twisted wires. For example, maybe you want to communicate with up to 254 arduino devices.

This library implements the master device side of the communication protocol. You can read more about the Disco Bus protocol spec here

Bus Topology



Usage Examples

Basic message

This example simply sends a single 3-byte message to node 5 on the bus. In this case we're sending RGB color values.

const DiscoBusMaster = require('discobus.js').DiscoBusMaster;

// Create master device and connect it to a serial port  
var master = new DiscoBusMaster();
master.on('error', console.error);
master.connectTo('/dev/ttyUSB0', {baudRate: 9600});

// Send a message 
//  + command: 0x09 (CMD_RGB)
//  + length: 3
//  + destination node address: 0x05
//  + message data: 0x00, 0x66, 0x20
const CMD_RGB = 0x09
master.startMessage(CMD_RGB, 3, { destination: 0x05 })
  .sendData([0x00, 0x66, 0x20])
  .endMessage();

Use an open serial port

You can use an existing serial port with your master device:

const DiscoBusMaster = require('discobus').DiscoBusMaster;
const SerialPort = require("serialport");

// Open serial port
var port = new SerialPort("/dev/tty-usbserial1", {baudRate: 9600});

// Connect the bus with this port
var master = new DiscoBusMaster();
master.on('error', console.error);
master.connectWith(port);

Addressing

Dynamically assigns an address to all the slaves on the bus. Without slave addresses, the only messages that will be received are broadcast messages (destination: 0).

const DiscoBusMaster = require('discobus.js').DiscoBusMaster;

var master = new DiscoBusMaster();
master.on('error', console.error);
master.connectTo('/dev/ttyUSB0', {baudRate: 9600});

bus.startAddressing()
.subscribe(null, null, () => {
  console.log('Found nodes:', master.nodeNum);
});

Request responses

Asks node 9 to send a 3-byte response for message command 0x06. (In this case we could be getting te sensor values back from the node)

const DiscoBusMaster = require('discobus.js').DiscoBusMaster;

// Create master device and connect it to a serial port  
var master = new DiscoBusMaster();
master.on('error', console.error);
master.connectTo('/dev/ttyUSB0', {baudRate: 9600});

// Get a 2-byte response from node 0x09
const CMD_SENSORS = 0x06
master.startMessage(CMD_SENSORS, 2, { 
  destination: 0x09,
  responseMsg: true
})
.subscribe(
  null,
  (err) => { console.error(err); },
  () => {
    console.log('Response', master.messageResponse);
  }
);

Batch messages

We can send RGB values to all two nodes on the bus at once by using a batch message.

const DiscoBusMaster = require('discobus.js').DiscoBusMaster;

// Create master device and connect it to a serial port  
var master = new DiscoBusMaster();
master.connectTo('/dev/ttyUSB0', {baudRate: 9600});

// Send the message to all nodes 
const CMD_RGB = 0x09
master.startMessage(CMD_RGB, 3, { batchMode: true })
.sendData([ 0x00, 0x66, 0x20 ]) // to node 1
.sendData([ 0x00, 0x66, 0x20 ]) // to node 2
.endMessage();

Batch response messages

You can received responses from all the messages on the bus with a batch response message.

const DiscoBusMaster = require('discobus.js').DiscoBusMaster;

// Create master device and connect it to a serial port  
var master = new DiscoBusMaster();
master.connectTo('/dev/ttyUSB0', {baudRate: 9600});

// Send the message to all nodes 
const CMD_SENSORS = 0x06
master.startMessage(CMD_SENSORS, 2, { 
  responseMsg: true,
  batchMode: true
})
.subscribe(
  null,
  (err) => { console.error(err); },
  () => {
    console.log('Node 1', master.messageResponse[0]);
    console.log('Node 2', master.messageResponse[1]); 
  }
);

Methods

DiscoBusMaster() constructor

Creates a Disco Bus Master device.

connectTo (port, options, callback)

Connect to a serial device via node-serialport

Parameters:

These are the same as passed to the serialport contructor

  • port: A string to the serial port to open.
  • options: Port configuration options. (options list)
  • openCallback: Called when a connection has been opened.

Returns: The DiscoBusMaster instance.

connectWith (port)

Connect with an existing open port connection.

Parameters

The port object passed in needs to have the following methods, which behave like an open node-serialport port:

  • write(number[])
  • drain(callback)
  • on('load', callback)
  • on('data', callback)

Returns: The DiscoBusMaster instance.

startMessage (command, length, options)

Start a new message.

Parameters

  • command: The message command.
  • length: The length of the data (per node, for batchMode) we're planning to send.
  • options: Message options:
    • destination: The node we're sending this message to (default: broadcast to all)
    • batchMode: Send unique data sections for all nodes in this one message. (i.e. a different RGB color for each node)
    • responseMsg: Ask one or more nodes to return some data.
    • responseDefault: If a node doesn't response, this is the default response. (used with responseMsg).

Returns: The DiscoBusMaster instance.

startAddressing (startFrom)

Start dynamically addressing all nodes.

Parameters

  • startFrom: (optional) The first address to start from.

Returns: The DiscoBusMaster instance.

subscribe (nextCallback, errorCallback, completeCallback)

Subscribe to the current message observer stream. (this is a wrapper to messageSubscription.subscribe)

Parameters

  • nextCallback: Called with the next value (received data or address)
  • errorCallback: Called when there is an error
  • completeCallback: Called when the message is complete.

Returns: The DiscoBusMaster instance.

sendData (data)

Write bytes to the data section of the message.

Parameters

  • data: An array of bytes to send.

Returns: The DiscoBusMaster instance.

endMessage ()

Finish the message and send the CRC bytes. This will be called automatically for response messages, and should not be called directly, in that case.

Returns: The DiscoBusMaster instance.

setDaisyLine (enabled)

Set's the outgoing daisy line to enabled or disabled, by toggling the port's RTS line. Override this method to use your own implementation.

Parameters

  • enabled: true to set the daisy line to enabled.

Returns: A promise which resolves when the daisy line has been set.

Properties

  • nodeNum: Number of nodes in the bus.
  • messageResponse: The response data from the current/last message.
  • messageSubscription: An RXJS hot observable use to watch the process of the current message.
  • messageCommand: Get the current message command.
  • timeouts: The bus timeout options
    • nodeResponse: Number of milliseconds to wait for a node's response. After this timeout, default values will be used instead.
    • addressing: End the addressing message if we haven't received a new address in at least this many milliseconds.

License

MIT License

Copyright (c) 2016 Jeremy Gillick

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.