better-serial-port
TypeScript icon, indicating that this package has built-in type declarations

1.3.1 • Public • Published

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 the open 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 port
  • closePort(keepClosed: boolean = false, disconnectError?: Error): Promise<void>: Will close the port and attempt reopen if keepClosed is not set to true
  • portOpen(): 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 data
  • BetterSerialPortOptions.disconnectTimeoutMS: number | boolean: How long of no data before assuming disconnection

Included serial port methods

  • write(): Will re-open the port if not successful
  • flush(): Adds a flush function with promise
  • pipe(): 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);
}

Package Sidebar

Install

npm i better-serial-port

Weekly Downloads

2

Version

1.3.1

License

MIT

Unpacked Size

20.5 kB

Total Files

7

Last publish

Collaborators

  • haydendonald