An open serial port.
@eryx/serial Module
Serial port I/O module backed by OS serial APIs.
Supports full-duplex communication over RS-232, USB CDC, and similar serial interfaces. Reads are asynchronous and yield the calling coroutine without blocking the event loop.
local serial = require("@eryx/serial")
local port = serial.open("COM3", { baudRate = 115200 })
port:write("hello\r\n")
local response = port:read(64, 1000) -- up to 64 bytes, 1 s timeout
port:close()
Summary
Classes
Functions
API Reference
Classes
SerialPort
Properties
Number of bytes currently in the OS receive buffer (available to read).
Number of bytes currently in the OS transmit buffer (not yet sent).
SerialPort:read
⚠ YieldsRead bytes from the port.
:read()— non-blocking; returns all bytes currently in the OS receive buffer, or""if none are available.:read(n)— blocks until between 1 andnbytes arrive.:read(n, timeout)— blocks up totimeoutmilliseconds; returnsnilif no bytes arrived before the timeout. Pass0for an instant non-blocking check.:read(nil, timeout)— waits up totimeoutms for any data, then drains the receive buffer (up to 64 KB); returnsnilon timeout.
Throws on device errors (e.g. disconnection).
Parameters
maximum bytes to read; omit to drain the buffer non-blocking
Parameters
SerialPort:flush
Discard buffered data in the OS receive or transmit queue.
Parameters
defaults to "both"
SerialPort:setDTR
Set the DTR (Data Terminal Ready) output line.
Parameters
boolean
SerialPort:setRTS
Set the RTS (Request To Send) output line.
Parameters
boolean
SerialPort:getSignals
Read the state of the modem-control input lines (CTS, DSR, DCD, RI).
Functions
serial.open
Open a serial port.
local port = serial.open("/dev/ttyUSB0", { baudRate = 9600, parity = "even" })
Parameters
device name, e.g. "COM3" or "/dev/ttyUSB0"
port configuration; all fields are optional
serial.list
Enumerate available serial ports on the system.
Returns a list of PortInfo tables. USB-to-serial adapters typically
populate the extra fields (vid, pid, manufacturer, etc.); built-in
UARTs usually only have name.
for _, info in serial.list() do
print(info.name, info.description)
end
Types
PortOptions
Options passed to serial.open.
Baud rate in bits per second. Common values: 9600, 19200, 38400,
57600, 115200, 230400. Defaults to 9600.
Number of data bits per character: 5, 6, 7, or 8. Defaults to 8.
Number of stop bits: 1 or 2. Defaults to 1.
Parity mode. Defaults to "none".
PortInfo
Information about a serial port returned by serial.list.
Fields beyond name are present only when the OS can supply them
(typically USB-to-serial adapters; native UARTs usually only have name).
Device path, e.g. "COM3" on Windows or "/dev/ttyUSB0" on Linux.
Human-readable product description, e.g. "USB Serial Port (COM3)".
Raw hardware ID string. On Windows this is the SetupAPI hardware ID;
on Linux it is a "USB VID:PID=xxxx:yyyy" style string.
USB vendor ID as an integer.
USB product ID as an integer.
USB manufacturer string reported by the device.
USB serial number reported by the device.