better-serial-port
v1.3.1
Published
A wrapper for the node serial port project that adds useful functionality
Downloads
30
Maintainers
Readme
Better Serial Port
This is a basic wrapper around Node Serial Port that adds some extra functionality that i feel it is lacking.
Features
- The port is monitored. If it disconnects it will emit the
close
event and when it is detected again it will automatically reconnect and emit theopen
event - Can auto reopen the port on error
- If there is no data for a specific period of time it will assume the port disconnected
How to use
This is just an extension of Node Serial Port so check out the docs for that project.
However, this project does add some extra functionality:
Extra methods
openPort(keepOpen?: boolean): Promise<void>
: Will open the portclosePort(keepClosed: boolean = false, disconnectError?: Error): Promise<void>
: Will close the port and attempt reopen if keepClosed is not set to trueportOpen(): boolean
: If the port is currently open
Extra Options
BetterSerialPortOptions.keepOpen: boolean
: Should the port be kept open?BetterSerialPortOptions.closeOnNoData: number | boolean
: Should we close (and reopen) the port if we don't get any dataBetterSerialPortOptions.disconnectTimeoutMS: number | boolean
: How long of no data before assuming disconnection
Included serial port methods
write()
: Will re-open the port if not successfulflush()
: Adds a flush function with promisepipe()
: Adds the pipe method which will be moved to the new serial port when created
If you need more feel free to ask or add them via a pull request. Otherwise you can call them directly BetterSerialPort.port
Example
const BetterSerialPort = require("better-serial-port");
//Create the port and keep it open
const serialport = new BetterSerialPort.BetterSerialPort({
path: "/dev/example",
baudRate: 9600,
keepOpen: true,
closeOnNoData: true,
disconnectTimeoutMS: 1000
});
//Write example
serialport.write("Hello World!");
//Print out any data
serialport.on("data", (data) => {
console.log(data);
});
//When the port is connected
serialport.on("open", () => {
console.log("Port connected!");
});
//When the port is disconnected
serialport.on("close", () => {
console.log("Port disconnected");
});
//Close the port
function close() {
serialport.closePort();
}
//Close the port and don't re-open it
function stayClosed() {
serialport.closePort(true);
}