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
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
Whether baudRate may be set to a value outside the standard set. Default is false.
shouldEchoReceivedData
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
port.setCallback((event, data) => console.log(event, data)).open()
close() -> HSSerialPort
Closes the port.
close() -> HSSerialPort
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 |
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 (seesendData()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 |
port.setCallback((event, data) => console.log(event, data))
destroy() -> None
Closes the port and releases all resources. Called automatically during shutdown.
destroy() -> None
port.destroy()