Serial Port Access

The serial module provides raw access to the termios terminal API and the serial port modem control lines.

Functions can be individually imported and directly accessed using the named import syntax:

import { attr, B115200 } from 'serial';

let fd = io.open('/dev/ttyS0', io.O_RDWR);
print(attr(fd));

Alternatively, the module namespace can be imported using a wildcard import statement:

import * as serial from 'serial';

let fd = io.open('/dev/ttyS0', io.O_RDWR);
print(serial.attr(fd));

Additionally, the serial module namespace may also be imported by invoking the ucode interpreter with the -lserial switch.

Methods

attr(fd) → {object}nullable

Get terminal attributes.

Retrieves the current termios attributes for the file descriptor.

Returns an object containing the iflag, oflag, cflag and lflag flag values, the ispeed and ospeed baud rates, and a cc array of control character settings.

Returns null if an error occurred or if the descriptor is not a terminal.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

Returns: object

drain(fd) → {boolean}nullable

Wait for pending output to be written.

Blocks until all output written to the file descriptor has been transmitted.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

Returns: boolean

dtr(fd, on) → {boolean}nullable

Assert or deassert the DTR (Data Terminal Ready) modem control line.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

onboolean

Whether to assert (true) or deassert (false) the line.

Returns: boolean

error() → {string}nullable

Query error information.

Returns a string containing a description of the last occurred error or null if there is no error information.

Returns: string

flush(fd, queueopt) → {boolean}nullable

Flush the terminal input and/or output queues.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

queuenumber(optional, default: TCIOFLUSH)

The queue(s) to flush: TCIFLUSH for input, TCOFLUSH for output, or TCIOFLUSH for both.

Returns: boolean

getinfo(fd) → {object}nullable

Get the serial port configuration.

Retrieves the serial port specific configuration (setserial style) for the file descriptor.

Returns an object containing the type, line, port, irq, flags, xmit_fifo_size, custom_divisor, baud_base, close_delay, closing_wait, hub6, io_type, port_high and iomem_reg_shift properties, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

Returns: object

input_waiting(fd) → {number}nullable

Get the number of bytes waiting in the input queue.

Returns the number of bytes available for reading without blocking, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

Returns: number

isatty(fd) → {boolean}nullable

Check whether the file descriptor refers to a terminal.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

Returns: boolean

Returns true if the file descriptor refers to a terminal device, false otherwise, or null if an error occurred.

lowlatency(fd, on) → {boolean}nullable

Enable or disable the low latency mode.

Toggles the ASYNC_LOW_LATENCY flag in the serial port configuration, which reduces the latency of the serial port at the cost of increased CPU usage.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

onboolean

Whether to enable (true) or disable (false) the low latency mode.

Returns: boolean

mbic(fd, bits) → {boolean}nullable

Clear individual serial port modem control lines.

Clears the modem control lines selected by the given bitmask, leaving all other lines unchanged.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

bitsnumber

The modem control lines to clear, as a bitmask of TIOCM_* constants.

Returns: boolean

mbis(fd, bits) → {boolean}nullable

Set individual serial port modem control lines.

Sets the modem control lines selected by the given bitmask, leaving all other lines unchanged.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

bitsnumber

The modem control lines to set, as a bitmask of TIOCM_* constants.

Returns: boolean

mget(fd) → {number}nullable

Get the serial port modem control line status.

Returns a bitmask of the current modem control line states, using the TIOCM_* constants to test individual lines.

Returns null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

Returns: number

mset(fd, bits) → {boolean}nullable

Set the serial port modem control line states.

Replaces the full set of modem control line states with the given bitmask.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

bitsnumber

The modem control line states to set, as a bitmask of TIOCM_* constants.

Returns: boolean

output_waiting(fd) → {number}nullable

Get the number of bytes waiting in the output queue.

Returns the number of bytes pending transmission, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

Returns: number

rts(fd, on) → {boolean}nullable

Assert or deassert the RTS (Request To Send) modem control line.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

onboolean

Whether to assert (true) or deassert (false) the line.

Returns: boolean

sendbreak(fd, durationopt) → {boolean}nullable

Send a break signal on the serial line.

If the terminal is in canonical mode, the break is sent after the current input line has been processed, otherwise it is sent immediately.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

durationnumber(optional, default: 0)

The break duration in seconds, or 0 to send a standard length break.

Returns: boolean

setattr(fd, attrs, whenopt) → {boolean}nullable

Set terminal attributes.

Updates the termios attributes for the file descriptor. The given object may contain any of the following properties:

  • iflag: input flags
  • oflag: output flags
  • cflag: control flags
  • lflag: local flags
  • ispeed: input baud rate
  • ospeed: output baud rate
  • cc: array of control character settings

Only the provided properties are modified, all other attributes are left unchanged.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

attrsobject

The terminal attributes to set.

whennumber(optional, default: 0)

When to apply the changes (TCSANOW, TCSADRAIN or TCSAFLUSH).

Returns: boolean

setblocking(fd, vmin, vtime, whenopt) → {boolean}nullable

Configure the terminal read blocking behaviour.

Sets the VMIN and VTIME control character settings which determine how reads from the terminal block:

  • vmin = 0, vtime = 0: non-blocking reads
  • vmin = 0, vtime > 0: reads time out after vtime tenths of a second
  • vmin > 0, vtime = 0: reads block until at least vmin bytes arrive
  • vmin > 0, vtime > 0: reads block until vmin bytes arrive or the inter-byte vtime timeout expires

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

vminnumber

Minimum number of bytes to read before a read operation returns.

vtimenumber

Read timeout in tenths of a second.

whennumber(optional, default: 0)

When to apply the changes (TCSANOW, TCSADRAIN or TCSAFLUSH).

Returns: boolean

setinfo(fd, opts) → {boolean}nullable

Set the serial port configuration.

Updates the serial port specific configuration (setserial style) for the file descriptor. The given object may contain any of the following properties:

  • type: port type
  • port: I/O port address
  • irq: interrupt line
  • flags: port flags
  • xmit_fifo_size: transmit FIFO size
  • custom_divisor: custom baud rate divisor
  • baud_base: base clock frequency
  • close_delay: delay before closing the port
  • closing_wait: wait time when closing the port
  • hub6: HUB6 port selection
  • port_high: high I/O port address
  • iomem_reg_shift: I/O memory register shift

Only the provided properties are modified, all other settings are left unchanged.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

optsobject

The serial port configuration options to set.

Returns: boolean

setraw(fd, whenopt) → {boolean}nullable

Put the terminal into raw mode.

Disables input and output processing, canonical mode, signal generation and echo, following the behaviour of cfmakeraw().

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

whennumber(optional, default: 0)

When to apply the changes (TCSANOW, TCSADRAIN or TCSAFLUSH).

Returns: boolean

setspeed(fd, speed, whenopt) → {boolean}nullable

Set the terminal baud rate.

Sets both the input and output baud rate of the file descriptor to the given speed.

Returns true on success, or null if an error occurred.

Parameters:
NameTypeDescription
fdnumber | object

The file descriptor, or an object with a fileno() method.

speednumber

The baud rate to set, e.g. B115200.

whennumber(optional, default: 0)

When to apply the changes (TCSANOW, TCSADRAIN or TCSAFLUSH).

Returns: boolean

Type Definitions

Apply Modes

The TCS* constants are used as the optional when argument of setattr(), setspeed(), setraw() and setblocking() to control when the attribute changes take effect.

Properties
NameTypeDescription
TCSANOWnumber

Apply the changes immediately.

TCSADRAINnumber

Apply the changes after all pending output has been transmitted (default).

TCSAFLUSHnumber

Apply the changes after all pending output has been transmitted, discarding any unread input.

Baud Rates

The B* constants select the port baud rate and are used with the ispeed/ospeed properties of setattr() or as the speed argument of setspeed().

Properties
NameTypeDescription
B0number

Hang up (no carrier).

B50number

50 baud.

B75number

75 baud.

B110number

110 baud.

B134number

134.5 baud.

B150number

150 baud.

B200number

200 baud.

B300number

300 baud.

B600number

600 baud.

B1200number

1200 baud.

B1800number

1800 baud.

B2400number

2400 baud.

B4800number

4800 baud.

B9600number

9600 baud.

B19200number

19200 baud.

B38400number

38400 baud.

B57600number

57600 baud.

B115200number

115200 baud.

B230400number

230400 baud.

B460800number

460800 baud.

B500000number

500000 baud.

B576000number

576000 baud.

B921600number

921600 baud.

B1000000number

1000000 baud.

B1152000number

1152000 baud.

B1500000number

1500000 baud.

B2000000number

2000000 baud.

B2500000number

2500000 baud.

B3000000number

3000000 baud.

B3500000number

3500000 baud.

B4000000number

4000000 baud.

Control Character Indices

The V* constants are indices into the cc array returned by attr() and accepted by setattr(). NCCS is the number of control characters in the array.

Properties
NameTypeDescription
VINTRnumber

Interrupt character (sends SIGINT).

VQUITnumber

Quit character (sends SIGQUIT).

VERASEnumber

Erase character (erases the last character).

VKILLnumber

Kill character (erases the current line).

VEOFnumber

End-of-file character.

VTIMEnumber

Read timeout in tenths of a second.

VMINnumber

Minimum number of bytes for a read.

VSWTCnumber

Switch character (XON/XOFF switching).

VSTARTnumber

Restart character (XON).

VSTOPnumber

Stop character (XOFF).

VSUSPnumber

Suspend character (sends SIGTSTP).

VEOLnumber

End-of-line character (first).

VREPRINTnumber

Reprint character (reprints the line).

VDISCARDnumber

Discard mode toggle character.

VWERASEnumber

Word-erase character.

VLNEXTnumber

Literal next character (disables special characters).

VEOL2number

End-of-line character (second).

NCCSnumber

Number of control characters.

Control Flags

The cflag constants select the control mode of the port and are used with the cflag property of setattr().

Properties
NameTypeDescription
CSIZEnumber

Mask for the character size bits.

CS5number

Use 5 data bits per character.

CS6number

Use 6 data bits per character.

CS7number

Use 7 data bits per character.

CS8number

Use 8 data bits per character.

CSTOPBnumber

Use two stop bits (one if clear).

CREADnumber

Enable the receiver.

PARENBnumber

Enable parity generation and detection.

PARODDnumber

Use odd parity (even if clear).

HUPCLnumber

Hang up (drop the carrier) when the last file descriptor is closed.

CLOCALnumber

Ignore the modem status lines.

CRTSCTSnumber

Enable in-band (hardware) flow control.

CMSPARnumber

Use "stick" (space/mark) parity.

CBAUDnumber

Mask for the baud rate bits.

CBAUDEXnumber

Extended baud rate bits.

Flush Queues

The TC*FLUSH constants are used as the optional queue argument of flush() to select the queue(s) to flush.

Properties
NameTypeDescription
TCIFLUSHnumber

Flush data received but not read.

TCOFLUSHnumber

Flush data written but not yet transmitted.

TCIOFLUSHnumber

Flush both received and written data (default).

Input Flags

The iflag constants control input processing and are used with the iflag property of setattr().

Properties
NameTypeDescription
IGNBRKnumber

Ignore the break condition.

BRKINTnumber

If IGNBRK is not set, a break causes an interrupt signal.

IGNPARnumber

Ignore characters with parity errors.

PARMRKnumber

Mark parity errors with a three-byte sequence.

INPCKnumber

Enable input parity checking.

ISTRIPnumber

Strip the eighth bit of input characters.

INLCRnumber

Map NL to CR on input.

IGNCRnumber

Ignore CR on input.

ICRNLnumber

Map CR to NL on input.

IUCLCnumber

Map uppercase to lowercase on input.

IXONnumber

Enable XON/XOFF flow control output.

IXANYnumber

Allow any character to restart output.

IXOFFnumber

Enable XON/XOFF flow control input.

IMAXBELnumber

Ring the bell when the input queue is full.

IUTF8number

Input characters are UTF-8 encoded.

Local Flags

The lflag constants control local (non-modem) behaviour and are used with the lflag property of setattr().

Properties
NameTypeDescription
ISIGnumber

Enable signal generation (INTR, QUIT, SUSP).

ICANONnumber

Enable canonical mode (line-buffered input).

ECHOnumber

Enable echoing of input characters.

ECHOEnumber

Erase the last character on ERASE.

ECHOKnumber

Ring the bell on the kill character.

ECHONLnumber

Echo NL even if ECHO is not set.

ECHOCTLnumber

Echo control characters in hat notation.

ECHOKEnumber

Erase a killed line.

NOFLSHnumber

Disable flushing on signal.

TOSTOPnumber

Generate SIGTTOU for background writes.

IEXTENnumber

Enable implementation-defined input extensions.

Modem Control Line Bits

The TIOCM_* constants identify the serial port modem control lines and are used with mget(), mset(), mbis() and mbic().

Properties
NameTypeDescription
TIOCM_LEnumber

Loopback output.

TIOCM_DTRnumber

Data Terminal Ready.

TIOCM_RTSnumber

Request To Send.

TIOCM_STnumber

Secondary transmit (TX2).

TIOCM_SRnumber

Secondary receive (RX2).

TIOCM_CTSnumber

Clear To Send.

TIOCM_CARnumber

Carrier Detect.

TIOCM_CDnumber

Carrier Detect (alias).

TIOCM_RNGnumber

Ring Indicator.

TIOCM_RInumber

Ring Indicator (alias).

TIOCM_DSRnumber

Data Set Ready.

Output Flags

The oflag constants control output processing and are used with the oflag property of setattr().

Properties
NameTypeDescription
OPOSTnumber

Enable implementation-defined output processing.

OLCUCnumber

Map lowercase to uppercase on output.

ONLCRnumber

Map NL to CR-NL on output.

OCRNLnumber

Map CR to NL on output.

ONOCRnumber

Translate CR to NUL in the first column.

ONLRETnumber

Do not transmit CR.

OFILLnumber

Use fill characters for timing.

OFDELnumber

Use DEL characters for fill (NUL if clear).

Serial Port Flags

The ASYNC_* constants are flags of the flags property returned by getinfo() and accepted by setinfo().

Properties
NameTypeDescription
ASYNC_HUP_NOTIFYnumber

Send SIGHUP when the port is closed.

ASYNC_FOURPORTnumber

Enable four-port mode.

ASYNC_SAKnumber

Enable special "SAK" character handling.

ASYNC_SPD_HInumber

High speed serial support (> 115200).

ASYNC_SPD_VHInumber

Very high speed serial support (> 230400).

ASYNC_SPD_SHInumber

Shigh speed serial support (1.5-3 Mbit).

ASYNC_SPD_CUSTnumber

Custom divisor baud rate.

ASYNC_SPD_WARPnumber

Warp speed serial (bit-banged).

ASYNC_SPD_MASKnumber

Mask for the speed selection bits.

ASYNC_SKIP_TESTnumber

Skip the UART presence test.

ASYNC_AUTO_IRQnumber

Auto-detect the IRQ line.

ASYNC_CALLOUT_NOHUPnumber

Do not send SIGHUP on close.

ASYNC_LOW_LATENCYnumber

Enable low latency mode.

ASYNC_BUGGY_UARTnumber

Workaround for buggy UARTs.

ASYNC_CLOSING_WAIT_INFnumber

Infinite closing wait.

ASYNC_CLOSING_WAIT_NONEnumber

No closing wait.

Serial Port Types

The PORT_* constants identify the serial port type of the type property returned by getinfo() and accepted by setinfo().

Properties
NameTypeDescription
PORT_UNKNOWNnumber

Unknown port type.

PORT_8250number

Generic 8250 UART.

PORT_16450number

16450 UART.

PORT_16550number

16550 UART.

PORT_16550Anumber

16550A UART.

PORT_16650number

16650 UART.

PORT_16650V2number

16650V2 UART.

PORT_16750number

16750 UART.