Home Reference Source Repository
public class | source

Serial

Extends:

events~EventEmitter → Serial

A node-serialport wrapper that keeps track of port state and allows from on-the-fly port path/name changes (automatically attaches/detaches event listeners and keeps config).

Constructor Summary

Public Constructor
public

constructor(parser: Function, baud: Number)

Constructor: the place for setting the baud rate and parser.

Member Summary

Private Members
private

Holds the desired serial baud rate.

private

_log: *

Logger instance.

private

Holds the parsing function.

private

Holds the current port path.

private

_port: SerialPort

Holds the current node-serialport instance.

private

Holds current state.

Method Summary

Public Methods
public

Closes the serialport.

public

Opens the serialport, creating it if needed.

public

setPath(path: *): Promise

Sets a new path to all future serialport instances.

Protected Methods
protected

Creates the serialport instance and attaches all relevant event listeners that forward data and errors and keep track of state.

protected

Destroys the current serialport instance.

protected

Shortcut for updating state that changes this._state and emits a stateChange event with one call.

Public Constructors

public constructor(parser: Function, baud: Number) source

Constructor: the place for setting the baud rate and parser.

Params:

NameTypeAttributeDescription
parser Function
  • optional
  • default: raw

A node-serialport parser.

baud Number
  • optional
  • default: 19200

Desired baud rate.

Private Members

private _baud: Number source

Holds the desired serial baud rate.

private _log: * source

Logger instance. (Bunyan API).

private _parser: Function source

Holds the parsing function.

private _path: String source

Holds the current port path.

private _port: SerialPort source

Holds the current node-serialport instance.

private _state: String source

Holds current state. Possible values: disconnect, open, close, disconnect_force.

Public Methods

public close(): Promise source

Closes the serialport.

Return:

Promise

Resolved when the serialport is closed.

Emit:

*

stateChange(state): Serial#_state} has changed.

*

error(error): An error ocurred in node-serialport.

public open(): Promise source

Opens the serialport, creating it if needed.

Return:

Promise

Resolved when the serial port is open.

Emit:

*

data(packet): New data arrived (after being parsed by Serial#_parser).

*

stateChange(state): Serial#_state has changed.

*

error(error): An error ocurred in node-serialport.

public setPath(path: *): Promise source

Sets a new path to all future serialport instances. If a port is already open, it is automatically closed and a new one is opened with the new path (keeps event listeners).

Params:

NameTypeAttributeDescription
path *

Return:

Promise

When path is changed and the port recreated/reopened.

Emit:

*

data(packet): New data arrived (after being parsed by Serial#_parser).

*

stateChange(state): Serial#_state has changed.

*

error(error): An error ocurred in node-serialport.

Protected Methods

protected _createPort(): Promise source

Creates the serialport instance and attaches all relevant event listeners that forward data and errors and keep track of state.

Return:

Promise

A resolved Promise for easy chaining in Serial#setPath.

Emit:

*

data(packet): New data arrived (after being parsed by Serial#_parser).

*

stateChange(state): Serial#_state has changed.

*

error(error): An error ocurred in node-serialport.

protected _destroyPort(): Promise source

Destroys the current serialport instance. Removing all listeners and closing it beforehand.

Return:

Promise

Resolves when all is done.

Emit:

*

stateChange(state): Serial#_state has changed.

protected _updateState(state: String) source

Shortcut for updating state that changes this._state and emits a stateChange event with one call.

Params:

NameTypeAttributeDescription
state String

New state.

Emit:

*

stateChange(state): Serial#_state has changed.