UART Module¶
| 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.
If the UART is in use as the system console, it is unavailable for use by this
module. Instead, refer to the console module.
If your IDE does not yet support uploading files via the console module,
consider using the utility script scripts/upload-file.py, e.g.
scripts/upload-file.py init.lua (use scripts/upload-file.py -h for help).
Before using a UART, you must call uart.setup and uart.start to set it up.
uart.on()¶
Sets the callback function to handle UART events. For a UART used by the
console, refer to the console module instead.
Syntax¶
uart.on([id], method, [number/end_char], [function])
Parameters¶
iduart id, except console uart. Default value is uart 0.method"data", data has been received on the UART. "error", error occurred on the UART.number/end_char. Only for eventdata.- if pass in a number n, 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.
functioncallback function.- event "data" has a callback like this:
function(data) end - event "error" has a callback like this:
function(err) end.errcould be one of "out_of_memory", "break", "rx_error".
To unregister the callback, provide only the "method" 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)
-- 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)
-- uart 2
uart.on(2, "data", "\r",
function(data)
print("receive from uart:", data)
end)
-- error handler
uart.on(2, "error",
function(data)
print("error from uart:", data)
end)
uart.setup()¶
(Re-)configures the communication parameters of the UART.
Syntax¶
uart.setup(id, baud, databits, parity, stopbits, pins)
Parameters¶
iduart id, except console uartbaudone of 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400databitsone of 5, 6, 7, 8parityuart.PARITY_NONE,uart.PARITY_ODD, oruart.PARITY_EVENstopbitsuart.STOPBITS_1,uart.STOPBITS_1_5, oruart.STOPBITS_2pins- table with the following entries:
txint. TX pin. Requiredrxint. RX pin. Requiredctsin. CTS pin. Optionalrtsin. RTS pin. Optionaltx_inverseboolean. Inverse TX pin. Default:falserx_inverseboolean. Inverse RX pin. Default:falsects_inverseboolean. Inverse CTS pin. Default:falserts_inverseboolean. Inverse RTS pin. Default:falseflow_controlint. Combination ofuart.FLOWCTRL_NONE,uart.FLOWCTRL_CTS,uart.FLOWCTRL_RTS. Default:uart.FLOWCTRL_NONE
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.setup(2, 115200, 8, uart.PARITY_NONE, uart.STOPBITS_1, {tx = 17, rx = 16})
uart.getconfig()¶
Returns the current configuration parameters of the UART.
Syntax¶
uart.getconfig(id)
Parameters¶
iduart id, except console uart
Returns¶
Four values as follows:
baudone of 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400databitsone of 5, 6, 7, 8parityuart.PARITY_NONE,uart.PARITY_ODD, oruart.PARITY_EVENstopbitsuart.STOPBITS_1,uart.STOPBITS_1_5, oruart.STOPBITS_2
Example¶
print (uart.getconfig(0))
-- prints 9600 8 0 1 for 9600, 8N1
uart.start()¶
Start the UART.
Syntax¶
uart.start(id)
Parameters¶
iduart id, except console uart
Returns¶
Boolean. true if uart is started.
uart.stop()¶
Stop the UART.
Syntax¶
uart.stop(id)
Parameters¶
iduart id, except console uart
Returns¶
nil
uart.setmode()¶
Set UART controllers communication mode
Syntax¶
uart.setmode(id, mode)
Parameters¶
iduart id, except console uartmodevalue should be one ofuart.MODE_UARTdefault UART mode, is set after uart.setup() calluart.MODE_RS485_COLLISION_DETECTreceiver must be always enabled, transmitter is automatically switched using RTS pin, collision is detected by UART hardware (note: no event is generated on collision, limitation of esp-idf)uart.MODE_RS485_APP_CONTROLreceiver/transmitter control is left to the applicationuart.MODE_RS485_HALF_DUPLEXreceiver/transmitter are controlled by RTS pinuart.MODE_IRDA
Returns¶
nil
uart.txflush()¶
Wait for any data currently in the UART transmit buffers to be written out. It can be useful to call this immediately before a call to node.sleep() because otherwise data might not get written until after wakeup.
Syntax¶
uart.txflush(id)
Parameters¶
iduart id, except console uart
Returns¶
nil
Example¶
uart.write(0, "I want this to show up now not in 5 seconds")
uart.txflush(0)
node.sleep({secs=5})
See also¶
uart.wakeup()¶
Configure the light sleep wakeup threshold. This is the number of positive edges that must be seen on the UART RX pin before a light sleep wakeup will be triggered. The minimum value is 3. The default value is undefined, therefore you should always call this function before the first time you call node.sleep() with the uart option set.
Syntax¶
uart.wakeup(id, val)
Parameters¶
iduart id, except console uartvalthe new value
Returns¶
nil
Example¶
uart.wakeup(0, 5)
See also¶
uart.write()¶
Write string or byte to the UART.
Syntax¶
uart.write(id, data1 [, data2, ...])
Parameters¶
iduart id, except console uartdata1... string or byte to send via UART
Returns¶
nil
Example¶
uart.write(0, "Hello, world\n")