2.6 KiB
2.6 KiB
UART Module
The UART (Universal asynchronous receiver/transmitter) module allows configuration of and communication over the UART serial port.
uart.alt()
Change UART pin assignment.
Syntax
uart.alt(on)
Parameters
on
:
0
use standard pins1
use alternate pins GPIO13 and GPIO15
Returns
nil
uart.on()
Sets the callback function to handle UART events.
Currently only the "data" event is supported.
Syntax
uart.on(method, [number/end_char], [function], [run_input])
Parameters
method
"data", data has been received on the UARTnumber/end_char
- if pass in a number n<255, the callback will called when n chars are received.
- if n=0, will receive every char in buffer.
- if pass in a one char string "c", the callback will called when "c" is encounterd, or max n=255 received.
function
callback function, event "data" has a callback like this:function(data) end
run_input
0 or 1. If 0, input from UART will not go into Lua interpreter, can accept binary data. If 1, input from UART will go into Lua interpreter, and run.
To unregister the callback, provide only the "data" parameter.
Returns
nil
Example
-- when 4 chars is received.
uart.on("data", 4,
function(data)
print("receive from uart:", data)
if data=="quit" then
uart.on("data") -- unregister callback function
end
end, 0)
-- when '\r' is received.
uart.on("data", "\r",
function(data)
print("receive from uart:", data)
if data=="quit\r" then
uart.on("data") -- unregister callback function
end
end, 0)
uart.setup()
(Re-)configures the communication parameters of the UART.
Syntax
uart.setup(id, baud, databits, parity, stopbits, echo)
Parameters
id
always zero, only one uart supportedbaud
one of 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400databits
one of 5, 6, 7, 8parity
uart.PARITY_NONE
,uart.PARITY_ODD
, oruart.PARITY_EVEN
stopbits
uart.STOPBITS_1
,uart.STOPBITS_1_5
, oruart.STOPBITS_2
echo
if 0, disable echo, otherwise enable echo
Returns
configured baud rate (number)
Example
-- configure for 9600, 8N1, with echo
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
uart.write()
Write string or byte to the UART.
Syntax
uart.write(id, data1 [, data2, ...])
Parameters
id
always 0, only one UART supporteddata1
... string or byte to send via UART
Returns
nil
Example
uart.write(0, "Hello, world\n")