serialport interface for batch style commands
This module is a promise based wrapper for the node-serialport package.
It allows you to interact with devices on a command & response basis. Based on a defined terminator
or timing
behavior the end of a response is deterimed and the complete response returned as a resolved Promise
.
$ npm install --save serial-io
const serialIo = require('serial-io');
// this will send HELLO to the device and resolve with
// anything that gets received within 100ms
serialIo.send('thisPortName', 'HELLO')
.then(response => console.log(`device responded:\n${response}`))
// if you don't know the devices port name try
serialIo.ports().then(console.log.bind(console))
// will show the response of a device on port '/dev/cu.usbmodem1411' (assuming it reacts to 'version\n')
serialIo.send('/dev/cu.usbmodem1411', 'version\n').then(console.log.bind(console))
The package exposes two APIs with different abstraction levels.
Directly interacting with the serialIo
objects gives you the highest level of abstraction.
If you use serialIo.connect()
you get a Connection
object returned that allows you to do things like multiple requests without re-connecting every time.
Using the Connection
API is recommended for programmatic use cases, whereas the high level serialIO.send()
API is great for handling events that are triggered by humans i.e. low frequency.
All methods are promise-based.
Resolves to a list of available serial ports. Refer to the serialport documentation for a specification of the returned data.
Single interaction with a device
. Opens up a connection, transmits the content
and waits for the response to resolve the returned Promise
.
Type: string
A valid portname.
Type: string
Payload to send.
Type: string
Default: none
If the specified terminator
string/character is found within the response the Promise
is resolved immediately with the capture data. The returned data includes the terminator
.
Type: number
in ms
Default: 100
Time to listen to the device for a (first) response.
Type: number
in ms
Default: 10
Time to wait after receiving a data chunk until the next one arrives. If the timer runs out the response is resolved
.
Opens up a Connection
to the given port.
This method is NOT promise-based it will return the Connection
object directly.
Type: string
A valid portname.
Refer to serialport openOptions for an overview of available options.
Closes the Connection
instance.
Send content
over the connection and wait for an answer. The method returns a Promise
that will resolve to the received answer. Using options
you can define how listening to answer is done.
Type: string
Payload to send.
Type: string
Default: none
If the specified terminator
string/character is found within the response the Promise
is resolved immediately with the capture data. The returned data includes the terminator
.
Type: number
in ms
Default: 100
Time to listen to the device for a (first) response.
Type: number
in ms
Default: 10
Time to wait after receiving a data chunk until the next one arrives. If the timer runs out the response is resolved
.
Tells you something about the current connection.
MIT © anoff