|Since||Origin / Contributor||Maintainer||Source|
|2019-12-27||pleningerweb, juancgalvez, crasu, galjonsfigur||galjonsfigur||softuart.c|
The SoftUART module offers bit-banged serial ports over GPIO pins.
Software implementation of serial port can be unreliable and some reception errors are to be expected.
ESP8266 has only 1 full hardware UART port that is used to program the chip and communicate with NodeMCU firmware. The second port is transmit-only. More information can be found in uart module documentation. This module provides access to more UART ports and can be used to communicate with devices like GSM or GPS modules. The code is based on esp8266-software-uart and Arduino-esp8266-Software-UART projects. Currently doesn't support inverted serial data logic or modes other than 8N1. It's important to notice that this is a software implementation of the serial protocol. There could be some interrupts that make the transmission or reception fail due to invalid timing.
SoftUART cannot be used on D0 pin.
Creates new SoftUART instance. Note that rx pin cannot be shared between instances but tx pin can.
softuart.setup(baudrate, txPin, rxPin)
baudrate: SoftUART baudrate. Maximum supported is 230400.
txPin: SoftUART tx pin. If set to
writemethod will not be supported.
rxPin: SoftUART rx pin. If set to
on("data")method will not be supported.
-- Create new software UART with baudrate of 9600, D2 as Tx pin and D3 as Rx pin s = softuart.setup(9600, 2, 3)
Sets up the callback function to receive data.
softuart.port:on(event, trigger, function(data))
event: Event name. Currently only
trigger: Can be a character or a number. If character is set, the callback function will only be run when that character gets received. When a number is set, the callback function will only be run when buffer will have as many characters as number.
function(data): Callback function. the
dataparameter is software UART receiving buffer.
-- Create new software UART with baudrate of 9600, D2 as Tx pin and D3 as Rx pin s = softuart.setup(9600, 2, 3) -- Set callback to run when 10 characters show up in the buffer s:on("data", 10, function(data) print("Lua handler called!") print(data) end)
Transmits a byte or sequence of them.
data: Can be a number or string. When a number is passed, only one byte will be sent. When a string is passed, whole sequence will be transmitted.
-- Create new software UART with baudrate of 9600, D2 as Tx pin and D3 as Rx pin s = softuart.setup(9600, 2, 3) s:write("Hello!") -- Send character 'a' s:write(97)