docs/modules/uart.md
| Since | Origin / Contributor | Maintainer | Source |
|---|---|---|---|
| 2014-12-22 | Zeroday | Zeroday | uart.c |
The UART (Universal asynchronous receiver/transmitter) module allows configuration of and communication over the UART serial port.
The default setup for the uart is controlled by build-time settings. The default rate is 115,200 bps. In addition, auto-baudrate detection is enabled for the first two minutes
after platform boot. This will cause a switch to the correct baud rate once a few characters are received. Auto-baudrate detection is disabled when uart.setup is called.
!!! important Although there are two UARTs(0 and 1) available to NodeMCU, UART 1 is not capable of receiving data and is therefore transmit only.
Change UART pin assignment.
uart.alt(on)
on
nil
Sets the callback function to handle UART events.
Currently only the "data" event is supported.
!!! note Due to limitations of the ESP8266, only UART 0 is capable of receiving data.
uart.on(method, [number/end_char], [function], [run_input])
method "data", data has been received on the UARTnumber/end_char
function callback function, event "data" has a callback like this: function(data) endrun_input 0 or 1. If 0, input from UART will not go into Lua interpreter, and this can accept binary data. If 1, input from UART is treated as a text stream with the DEL, BS, CR and LF characters processed as normal. Completed lines will be passed to the Lua interpreter for execution. Note that the interpreter only processes complete lines.To unregister the callback, provide only the "data" parameter.
nil
-- 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)
(Re-)configures the communication parameters of the UART.
!!! note
Bytes sent to the UART can get lost if this function re-configures the UART while reception is in progress.
uart.setup(id, baud, databits, parity, stopbits[, echo])
id UART id (0 or 1).baud one of 300, 600, 1200, 2400, 4800, 9600, 19200, 31250, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400databits one of 5, 6, 7, 8parity uart.PARITY_NONE, uart.PARITY_ODD, or uart.PARITY_EVENstopbits uart.STOPBITS_1, uart.STOPBITS_1_5, or uart.STOPBITS_2echo if 0, disable echo, otherwise enable echo (default if omitted)configured baud rate (number)
-- configure for 9600, 8N1, with echo
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
Returns the current configuration parameters of the UART.
uart.getconfig(id)
id UART id (0 or 1).Four values as follows:
baud 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, or uart.PARITY_EVENstopbits uart.STOPBITS_1, uart.STOPBITS_1_5, or uart.STOPBITS_2print (uart.getconfig(0))
-- prints 9600 8 0 1 for 9600, 8N1
Write string or byte to the UART.
uart.write(id, data1 [, data2, ...])
id UART id (0 or 1).data1... string or byte to send via UARTnil
uart.write(0, "Hello, world\n")
Report the depth, in bytes, of TX or RX hardware queues associated with the UART.
uart.fifodepth(id, dir)
id UART id (0 or 1).dir uart.DIR_RX for the RX FIFO, uart.DIR_TX for TX FIFO.The number of bytes in the selected FIFO.