Module serial

The serial library enables communication over the serial port.

It provides the usual open, read, write, and close file handling methods, as well as serial port configuration features.

Type serial

serial.open(port, config)

Creates and returns a new serial device instance: #serialdev.

Type config

config.baudRate

to set the baudrate of the serial port accepted values are: 1200, 9600, 19200, 38400, 57600 and 115200 (as an integer).

config.flowControl

to configure flow control on the serial port accepted values are: "none", "rtscts" or "xon/xoff" (as a string).

config.numDataBits

to set the number of data bits in each character accepted values are: 5, 6, 7 and 8 (as an integer).

config.numStopBits

to configure the serial port stop bit accepted values are: 1 to disable it, 2 to enable it (as an integer).

config.parity

to set serial port parity accepted values are (as a string): "none", "odd", "even"

Type serialdev

serialdev:close()

Closes the serial library instance.

serialdev:configure(config)

Configures the serial port.

serialdev:flush()

Flushes pending data.

serialdev:read(pattern)

Reads data from the serial port.

serialdev:write(buffer)

Writes buffer to the serial port.

Type serial

Field(s)

serial.open(port, config)

Creates and returns a new serial device instance: #serialdev.

Parameters

  • port : the serial port to open. It can be given as a port number, or as a POSIX device file path such as "/dev/ttyS0" on POSIX operating systems.

  • config : an optional #config table. Default values are { parity = "none", flowControl = "none", numDataBits = 8, numStopBits = 1, baudRate = 115200 }

Return values

  1. #serialdev: a new instance of #serialdev as a table when successful

  2. nil followed by an error message otherwise

Type config

Serial port configuration table.

Can be given to serialdev.configure and serial.open functions:

Field(s)

config.baudRate

to set the baudrate of the serial port accepted values are: 1200, 9600, 19200, 38400, 57600 and 115200 (as an integer).

config.flowControl

to configure flow control on the serial port accepted values are: "none", "rtscts" or "xon/xoff" (as a string).

config.numDataBits

to set the number of data bits in each character accepted values are: 5, 6, 7 and 8 (as an integer).

config.numStopBits

to configure the serial port stop bit accepted values are: 1 to disable it, 2 to enable it (as an integer).

config.parity

to set serial port parity accepted values are (as a string): "none", "odd", "even"

Type serialdev

Serial class.

Field(s)

serialdev:close()

Closes the serial library instance.

Once close has been successfully called, the serialdev instance becomes unusable. A new one may be created if needed.

Return values

  1. "ok" on success.

  2. nil followed by an error message otherwise.

serialdev:configure(config)

Configures the serial port.

Parameter

Return values

  1. "ok" on success.

  2. nil followed by error string otherwise.

serialdev:flush()

Flushes pending data.

Return values

  1. "ok" on success.

  2. nil followed by an error message otherwise.

serialdev:read(pattern)

Reads data from the serial port.

Parameter

  • pattern : can be a pattern string conforming to the Lua File API, or a number of bytes to read.

Return values

  1. the read data, as a string, on success.

  2. nil followed by error string otherwise.

serialdev:write(buffer)

Writes buffer to the serial port.

Parameter

  • buffer : string containing data to send over the serial port.

Return values

  1. the number of bytes written on success.

  2. nil followed by an error message otherwise.