πŸ“¦ Archived documentation: r1 (committed 2026-08-05) View current documentation β†’

serial

RS-485 serial ports: initialise, transmit, and receive delimited messages with a callback.

The serial object drives the device's RS-485 serial ports:

Port Hardware
1 Expansion Serial TTL Port
2 RS-485 port

Initialise the port with a baud rate, register a delimiter-based receive callback, and transmit with serial.tx(). For device-to-device links over untrusted wiring, combine with aes.enc/aes.dec and bin.toHex/ bin.fromHex to send encrypted, checksummed messages β€” see the Serial Port: Encrypted Device-to-Device example.

The same ports can also speak Modbus RTU directly via modbus.read() / modbus.write() with the matching port number β€” see the Serial Modbus (RS-485) example.

Functions

serial.init()

serial.init(port, baud) β†’ number

Initialises a serial port at the given baud rate.

Specify a parameter object for advanced usage

serial.init(port,{baud:9600,stopBits:1,parity:"odd",mode:"0,swap:false,invert:false});

:Mode 0: tx+rx, 1: rx only, 2: tx only, 3: Activates direction signalling on the expansion IO port for use with RS485 transceivers

:Swap Swap the TX RX lines on the expansion port

:Invert Invert the RS485 high low signaling (Experimental)

Arguments

NameTypeRequiredDefaultDescription
port number yes β€” 1 = isolated RS-485, 2 = non-isolated RS-485.
baud | config number | object yes β€” Baud rate, e.g. 9600, 19200. for standard 8n1 settings otherwise supply a json object with parameters

Returns

number Status code β€” 0 on success.

Example

let port = 1;             // isolated RS-485
serial.init(port, 19200);

serial.listen()

serial.listen(port, delimiter, callback)

Buffers received bytes and calls the callback whenever the delimiter character arrives.

Fills the receive buffer while watching for the delimiter character, then executes the callback with a message object:

Field Meaning
msg.port Port the message arrived on.
msg.ovf true if the receive buffer overflowed β€” discard the message.
msg.data The received data.

Arguments

NameTypeRequiredDefaultDescription
port number yes β€” 1 = isolated RS-485, 2 = non-isolated RS-485.
delimiter string yes β€” End-of-message character, e.g. "\r".
callback function yes β€” Callback function(msg){ … } β€” see the field table.

Example

function serialRxCallback(msg){
  print("Received Serial Msg");
  print("Port:", msg.port);
  print("Overflowed:", msg.ovf);
  print("Data:", msg.data);
}

serial.listen(port, "\r", serialRxCallback);

serial.tx()

serial.tx(port, data)

Transmits data on a serial port.

Sends data out the port. Remember to append your protocol's end-of-message delimiter (e.g. "\r") so the receiver's serial.listen() fires.

Arguments

NameTypeRequiredDefaultDescription
port number yes β€” 1 = isolated RS-485, 2 = non-isolated RS-485.
data string yes β€” Data to send β€” hex-encode binary payloads with bin.toHex().

Example

serial.tx(port, "Hello World over serial!\r");

serial.rx()

serial.rx(port,[bytes],[timeout]) β†’ {data,ovf}

Read the current serial receive buffer contents.

After initialization, the serial ports are asynchronously read in the background.

This method allows you to read the buffer contents or optionally wait (timeout) for requested bytes to be available.

Alternatively the listen() method is also available which executes a callback following a specified character.

Arguments

NameTypeRequiredDefaultDescription
port yes β€” Serial Port #
bytes optional 0 Number of bytes to wait for
timeout optional 1000 Milliseconds: How long to wait for the requested data

Returns

object {data:"hello world",ovf:false}

Example

let rxData = serial.rx(0); // Returns immediately with the current serial receive buffer contents. Useful for clearing the receive buffer

let rxData = serial.rx(0,24,1000); // Wait up to 1000mS for 24 bytes of data.