Serial Communication 3.1.11

Set Timeout

lebai:set_serial_timeout(device, timeout)
  • Parameters
    • device Device name
    • timeout Timeout duration in milliseconds (ms). Default 800ms

Set Baud Rate

lebai:set_serial_baud_rate(device, baud_rate)

Set serial port baud rate

  • Parameters
    • device Device name
    • baud_rate. Serial port baud rate. Default 115200

Returns empty or throws connection error

Set Parity

lebai:set_serial_parity(device, parity)

Set parity bit

  • Parameters
    • device Device name
    • parity. Parity bit (None: No parity; Odd: Odd parity; Even: Even parity). Default no parity

Returns empty or throws connection error

Send Data

lebai:write_serial(device, data)

Send u8 array through serial port

  • Parameters
    • device Device name
    • data. u8 array to send.

Returns empty or throws connection error

Receive Data

data = lebai:read_serial(device, len)

Receive u8 array through serial port

  • Parameters
    • device Device name
    • len. Maximum buffer length for single reception, optional, default 64 bytes.

Returns u8 array or throws connection error

Example

local device = "/dev/ttyS1"
lebai:set_serial_baud_rate(device, 9600)

local str = "123"
lebai:write_serial(device, {string.byte(str, 1, #str)}) -- Send string
lebai:write_serial(device, {0x01, 0x02, 0x03}) -- Send HEX
local data = lebai:read_serial(device)
print(data) -- Print HEX
print(string.char(table.unpack(data))) -- Print string