Skip to main content
Version: 8.x.x

SerialPort

const SerialPort = require('serialport')

This package provides everything you need to start talking over your serialport. It provides a high level Stream Interface, auto detecting bindings, and a set of parser streams.

Most of the api is covered in the Stream Interface docs.

Historically this was the only package involved and it contained everything. Since version 7 the internals have been split into their own modules and can be required separately, allowing a user to only install what they need.

This allows for smaller installs and alternative interfaces, bindings and parsers.

SerialPort

This is the Stream Interface constructor. It comes pre-populated with Binding and Parsers

const serialport = new SerialPort(path)
serialport.write('ROBOT POWER ON')

SerialPort.Binding

This package includes the @serialport/bindings package already attached to the stream interface.

SerialPort.Binding = require('@serialport/bindings)

SerialPort.parsers

Comes with the following parsers available for use.

SerialPort.parsers = {
ByteLength: require('@serialport/parser-byte-length'),
CCTalk: require('@serialport/parser-cctalk'),
Delimiter: require('@serialport/parser-delimiter'),
Readline: require('@serialport/parser-readline'),
Ready: require('@serialport/parser-ready'),
Regex: require('@serialport/parser-regex'),
}

These Parsers are all Transform streams that process incoming data. To use the parsers, you must create them and then pipe the Serialport to the parser. Be careful to only write to the SerialPort object and not the parser.

const SerialPort = require('serialport')
const Readline = SerialPort.parsers.Readline
const port = new SerialPort(path)
const parser = new Readline()
port.pipe(parser)
parser.on('data', console.log)
port.write('ROBOT PLEASE RESPOND\n')
// ROBOT ONLINE

// Creating the parser and piping can be shortened to
const parser = port.pipe(new Readline())