HSSerialPort
TypeA 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
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`.
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.
Declaration
open() -> HSSerialPort
Returns
HSSerialPort
self, for chaining
Example
port.setCallback((event, data) => console.log(event, data)).open()
close() -> HSSerialPort
Closes the port.
Declaration
close() -> HSSerialPort
Returns
HSSerialPort
self, for chaining
Example
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.
Declaration
sendData(value) -> HSSerialPort
Parameters
| Name | Type | Description |
|---|---|---|
| value | string | The data to send |
Returns
HSSerialPort
self, for chaining
Example
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.
Declaration
setCallback(fn) -> HSSerialPort
Parameters
| Name | Type | Description |
|---|---|---|
| fn | function | Called on port events |
Returns
HSSerialPort
self, for chaining
Example
port.setCallback((event, data) => console.log(event, data))
destroy() -> None
Closes the port and releases all resources. Called automatically during shutdown.
Declaration
destroy() -> None
Returns
None
Example
port.destroy()