API Docs

Communicate with devices connected to serial ports (RS-232, USB-serial adapters, etc).

IMPORTANT NOTE: This module is not currently very well tested with real hardware. Please provide feedback (positive or negative!) via GitHub Issues.

Enumerate available ports with availablePortNames()/availablePortPaths(), then create a port object with createPortNamed()/createPortAtPath(). The returned object is not open until you call open() on it.

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

Types

This module provides the following types:

Properties

This module has no properties.

Methods

hs.serial.availablePortNames() -> string[]

Returns the names of all currently connected serial ports.
hs.serial.availablePortNames() -> string[]
string[]
An array of port name strings (e.g. `"usbserial-1420"`)
console.log(hs.serial.availablePortNames())

hs.serial.availablePortPaths() -> string[]

Returns the device paths of all currently connected serial ports.
hs.serial.availablePortPaths() -> string[]
string[]
An array of path strings (e.g. `"/dev/cu.usbserial-1420"`)
console.log(hs.serial.availablePortPaths())

hs.serial.availablePortDetails() -> [String: [String: Any]]

Returns IOKit registry details for all currently connected serial ports.
hs.serial.availablePortDetails() -> [String: [String: Any]]
[String: [String: Any]]
An object keyed by port name, whose values are objects containing that port's IOKit registry properties.
const details = hs.serial.availablePortDetails()
Object.keys(details).forEach(name => console.log(name, details[name]))

hs.serial.createPortNamed(name) -> HSSerialPort

Creates a serial port object for a port discovered via `availablePortNames()`.
hs.serial.createPortNamed(name) -> HSSerialPort
Name Type Description
name string The port name, as returned by `availablePortNames()`
HSSerialPort
A new `HSSerialPort`, or `null` if no port with that name is currently connected
const port = hs.serial.createPortNamed("usbserial-1420")

hs.serial.createPortAtPath(path) -> HSSerialPort

Creates a serial port object for an arbitrary device path. Unlike `createPortNamed()`, the path does not need to correspond to a port currently discoverable via IOKit — it is only validated when you call `open()`.
hs.serial.createPortAtPath(path) -> HSSerialPort
Name Type Description
path string The device path (e.g. `"/dev/cu.usbserial-1420"`)
HSSerialPort
A new `HSSerialPort`
const port = hs.serial.createPortAtPath("/dev/cu.usbserial-1420")

hs.serial.addWatcher(listener) -> None

Register a listener for serial port connection and disconnection events. The listener is called with two arguments: the event type string (`"added"` or `"removed"`) and a port-info object with `name` and `path` fields.
hs.serial.addWatcher(listener) -> None
Name Type Description
listener function The function to call when a serial port is added or removed
None
hs.serial.addWatcher((event, port) => console.log(event + ": " + port.name))

hs.serial.removeWatcher(listener) -> None

Remove a previously registered serial port event listener.
hs.serial.removeWatcher(listener) -> None
Name Type Description
listener function The function originally passed to `addWatcher`
None
hs.serial.removeWatcher(myHandler)