Page 5 of 5

Re: Software serial?

Posted: Sat Mar 17, 2018 6:08 am
by pythoncoder
A full duplex software UART is quite hard to do, especially if all the features of a hardware UART are to be emulated. Further the Pyboard has five user-accessible hardware UARTs so use-cases on that platform will be few.

The main use-cases are on Espressif devices, especially the ESP8266 which has only 1.5 UARTs, one of which is used for debugging. Alas the interrupt latency of ESPx chips will limit the baudrate of any soft UART.

I think the useful user-contributed half-duplex solution discussed in this thread may be the best we'll get.

Re: Software serial?

Posted: Tue Nov 06, 2018 4:23 pm
by jimeer
Why not use an I2C UART?
This chip also has DAC/ADC and GPIO. I use a couple for two BLE gateways.

Re: Software serial?

Posted: Tue Nov 06, 2018 4:35 pm
by kevinkk525
Why bring up an ancient thread?

Re: Software serial?

Posted: Wed Nov 07, 2018 7:47 am
by pythoncoder
The datasheet for that chip is amateurish and inconsistent.

Re: Software serial?

Posted: Tue May 28, 2019 1:30 pm
by MisterVenez
Hi cversec,
I saw your evaluable job in SoftUART. Great !
I think that after 2 year there were some adjustments and experience. Below some questions:
a) I need to set the Parity (ODD) and StopBit (1). May I modify directly on the code (machine_softuart.c) ?
b) Along the code I've seen some sentences like _pyb_softuart_obj_t. Because I'm not using a PYB but another module based on ESP8266, do i need to modify something ?
c) For flashing on a ESP8266 chip, I prefer use the latest micropython code (a lot of water is passed under the bridges...), which files I need to import and modify in order to include the SoftUART in the flashing code ?
d) Do you have a some suggestions on PyCharm to manage correctly the new SoftUART (interpreter related)?

Thanks in advance

cversek wrote:
Sat Jan 07, 2017 6:12 am
Hi All,
I'm new to micropython and the esp8266, but I've been a long time Python and Arduino user. On my first IoT data logger project I was finding micropython a joy to develop in and the software machine.I2C library quite easy to adapt for an AM2315 ( humidity and temperature sensor. However, I hit a wall when I needed to integrate a UART communicating carbon dioxide sensor and found that the hardware UARTs were tied up and there was no built-in software serial module.

I actually found deshipu 's original suggestion to be quite helpful. After learning the basics of writing C extension modules (and a lot of trial-and-error hacking) I was able to successfully adapt a `softuart` C code library ( and wrap it in a micropython extension class `machine.SoftUART` that is based very closely on `machine.UART`.

I wanted to share this code to help others who may find this thread, so I put together a github micropython fork that makes the needed changes ( ... 8a89287a8f).
This firmware can be built according to Adafruit's helpful Feather HUZZAH guide ( ... d-firmware) if you substitute this repo instead of the one included on the vagrant VM.

In order initialize the SoftUART object, two Pin objects, tx & rx, are the only required arguments (similar to the I2C object's sda & scl pins). For example:
>>> import machine
>>> ser = machine.SoftUART(machine.Pin(12),machine.Pin(14), baudrate=9600, timeout=1000, timeout_char = 10)
>>> print(ser)
SoftUART(tx=12, rx=14, baudrate=9600, bits=8, parity=None, stop=1, timeout=1000, timeout_char=10)
>>> ser.
init flush read readline
readinto write

The baudrate and timeouts are configurable; however, the data bits, stop bits, and parity are fixed. The softuart.c library uses a statically allocated receive buffer with a default size of 64 bytes ( ... tuart.h#L4). The new `flush` method resets the receive buffer to empty state. One major shortcoming of the `machine.SoftUART` implementation is that only one instance of a SoftUART class can function in overlapping code since it uses a global Softuart struct instance `softuartDevice` - I was unable to get multiple instances working properly using dynamic allocation.

Please let me know if you find this useful or need help to get it working.

Re: Software serial?

Posted: Wed Jul 17, 2019 5:32 am
by tusker
Hi MisterVenez,

I have been looking at the SoftUART support for Micropython (for integrating with RS485 adapters), and therefore have pulled in the code changes and adapted the patches for the current micropython code base. You can see on

I have also pulled in the performance improvements from ... tware-UART, so hopefully it is better performing now.

In any case, in terms of your questions:
a) I am not sure that you will get away from just modifying the parity and stop bit, because parity and stop bit isn't implemented in softuart.c (you can see alternative softuart implementations that do implement parity, such as ... softuart.c)
b) This doesn't look specific to PYB and should be generic across all ESP8266.
c) As per above comment, you are welcome to try out my fork (I have only compile tested it so far)
d) What do you mean about manage correctly ?



Re: Software serial?

Posted: Fri Jul 26, 2019 8:57 am
by liuyuelin
Do you know how to turn off an external interrupt setting?I want to write softuart, but I want external interrupt mode to detect the start bit, and turn off the interrupt after data transmission starts, but how to turn off the interrupt?

Re: Software serial?

Posted: Fri Aug 16, 2019 3:34 am
by tusker
liuyuelin wrote:
Fri Jul 26, 2019 8:57 am
Do you know how to turn off an external interrupt setting?I want to write softuart, but I want external interrupt mode to detect the start bit, and turn off the interrupt after data transmission starts, but how to turn off the interrupt?
In the softuart.c, you can see the following lines:

// disable all interrupts
// disable interrupt for GPIO0
gpio_pin_intr_state_set(GPIO_ID_PIN(s->pin_rx.gpio_id), GPIO_PIN_INTR_DISABLE);

You can see some discussion on the topic at