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

1.0.2 • Public • Published

Better Port

This is a basic wrapper for ports that adds some extra functionality to provide a constant connection that i feel is lacking.

This project originally started as a wrapper called Better-Serial-Port for Node Serial Port to provide a constant connection but is now written to support different protocols such as UDP, thus a rename to Better Port.

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

Currently this project supports the following ports:

//Create the port
const BetterPort = require("./dist/index.js");
const BetterPortEvent = BetterPort.BetterPortEvent;
var port = new BetterPort.BetterSerialPort({
    path: "",
    baudRate: 912600,
    keepOpen: true,
    autoOpen: true
    //Any extra open options for serial port
});

//Setup our events
port.on(BetterPortEvent.open, () => {
    console.log("Port opened");
});
port.on(BetterPortEvent.close, () => {
    console.log("Port closed");
});
port.on(BetterPortEvent.error, (err) => {
    console.log("Port error: ", err);
});
port.on(BetterPortEvent.data, (data) => {
    console.log("Port data: " + data.toString());
});

//Open the port
port.openPort().then(() => {
    console.log("Port created!");
}).catch((err) => {
    console.log("Port open error: ", err);
});
const BetterPort = require("./dist/index.js");
const BetterPortEvent = BetterPort.BetterPortEvent;

//Open a UDP server on port 7000
var port = new BetterPort.BetterUDPPort({
    recPort: 7000,
    keepOpen: true,
    autoOpen: true
    //Any extra open options for dram
});

//Setup our events
port.on(BetterPortEvent.open, () => {
    console.log("Port opened");
});
port.on(BetterPortEvent.close, () => {
    console.log("Port closed");
});
port.on(BetterPortEvent.error, (err) => {
    console.log("Port error: ", err);
});
port.on(BetterPortEvent.data, (data) => {
    console.log("Port data: " + data.toString());
});

//Open the port
port.openPort().then(() => {
    console.log("Port created!");
}).catch((err) => {
    console.log("Port open error: ", err);
});

This project does add some extra functionality to these as well:

Extra methods

portExists(): Promise<boolean> //Does the port exist
portOpen(): boolean //Is the port currently open
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

  autoOpen?: boolean; //Should the port be opened automatically on creation
  keepOpen?: boolean; //Should we keep the port open
  closeOnNoData?: boolean; //Should we close the port if no data is received
  disconnectTimeoutMS?: number | undefined; //How long should we wait before disconnecting on no data

Overridden methods

The following methods cab be used normally but are replaced by this project

write(chunk: any, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean
write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean
write(data: any, encoding?: any, callback?: any): boolean
flush()
pipe<T extends NodeJS.WritableStream>(destination: T, options?: { end?: boolean; }): T

Package Sidebar

Install

npm i better-port

Weekly Downloads

5

Version

1.0.2

License

MIT

Unpacked Size

37 kB

Total Files

11

Last publish

Collaborators

  • haydendonald