Skip to content

serialport interface for batch style commands

License

Notifications You must be signed in to change notification settings

anoff/serial-io

Repository files navigation

serial-io

npm version Build Status Coverage Status Greenkeeper badge Known Vulnerabilities Outdated dependencies

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.

Install

$ npm install --save serial-io

Usage

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

API

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.

serialIo.ports()

Resolves to a list of available serial ports. Refer to the serialport documentation for a specification of the returned data.

serialIo.send(portName, content, {options})

Single interaction with a device. Opens up a connection, transmits the content and waits for the response to resolve the returned Promise.

portName

Type: string

A valid portname.

content

Type: string

Payload to send.

options

terminator

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.

timeoutInit

Type: number in ms
Default: 100

Time to listen to the device for a (first) response.

timeoutRolling

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.

serialIo.connect(portName, {options})

Opens up a Connection to the given port. This method is NOT promise-based it will return the Connection object directly.

portName

Type: string

A valid portname.

options

Refer to serialport openOptions for an overview of available options.

connection.close()

Closes the Connection instance.

connection.send(content, {options})

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.

content

Type: string

Payload to send.

options

terminator

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.

timeoutInit

Type: number in ms
Default: 100

Time to listen to the device for a (first) response.

timeoutRolling

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.

connection.getState()

Tells you something about the current connection.

License

MIT © anoff