API Docs

A serial port, created via hs.serial.createPortNamed() or hs.serial.createPortAtPath().

The port is not open until you call open(). Configure it (baud rate, data bits, etc.) either before or after opening — configuration changes made while open are applied immediately.

Received data, and lifecycle events, are delivered via the callback registered with setCallback().

  • Example:
const port = hs.serial.createPortNamed(hs.serial.availablePortNames()[0])
port.baudRate = 9600
port.setCallback((event, data) => {
    if (event === "received") console.log("Received: " + data)
    if (event === "error") console.log("Error: " + data)
}).open()
port.sendData("AT\r\n")

Properties

identifier

string

The unique identifier assigned to this port object.

name

string

The port's name (e.g. "usbserial-1420").

path

string

The port's device path (e.g. "/dev/cu.usbserial-1420").

isOpen

boolean

Whether the port is currently open.

baudRate

number

The baud rate, in bits per second. Default is 115200.

Setting a non-standard value (i.e. not one of 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200, 230400) is rejected unless allowNonStandardBaudRates is true.

allowNonStandardBaudRates

boolean

Whether baudRate may be set to a value outside the standard set. Default is false.

dataBits

number

The number of data bits, 5–8. Default is 8.

stopBits

number

The number of stop bits, 1 or 2. Default is 1.

parity

string

The parity mode: "none", "odd", or "even". Default is "none".

dtr

boolean

The state of the DTR (Data Terminal Ready) control line. Default is false.

rts

boolean

The state of the RTS (Request To Send) control line. Default is false.

usesRTSCTSFlowControl

boolean

Whether to use hardware RTS/CTS flow control. Default is false.

usesDTRDSRFlowControl

boolean

Whether to use hardware DTR/DSR flow control. Default is false.

shouldEchoReceivedData

boolean

Whether data sent with sendData() is also delivered back to the callback as a "received" event, simulating local echo. Default is false.

Methods

open() -> HSSerialPort

Opens the port using its current configuration.

open() -> HSSerialPort
HSSerialPort
self, for chaining
An open port, and its callback, stay alive until it's closed, even if you don't keep a reference to it
port.setCallback((event, data) => console.log(event, data)).open()

close() -> HSSerialPort

Closes the port.

close() -> HSSerialPort
HSSerialPort
self, for chaining
port.close()

sendData(value) -> HSSerialPort

Sends data through the port.

The string is transmitted as raw bytes: each character's code point (0–255) becomes one byte on the wire. This lets you round-trip arbitrary binary data — build the string with String.fromCharCode() for non-text payloads.

sendData(value) -> HSSerialPort
Name Type Description
value string The data to send
HSSerialPort
self, for chaining
port.sendData("AT\r\n")

setCallback(fn) -> HSSerialPort

Sets the callback invoked for port lifecycle events and received data.

The callback receives two arguments: an event type string and a data string.

  • "opened" — the port was opened; data is empty
  • "closed" — the port was closed; data is empty
  • "received" — data arrived; data holds the received bytes (see sendData() for encoding)
  • "removed" — the underlying device disappeared; the port is closed automatically
  • "error" — an error occurred; data holds a description
setCallback(fn) -> HSSerialPort
Name Type Description
fn function Called on port events
HSSerialPort
self, for chaining
port.setCallback((event, data) => console.log(event, data))

destroy() -> None

Closes the port and releases all resources. Called automatically during shutdown.

destroy() -> None
None
port.destroy()