Page 5 of 6

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?
https://www.byvac.com/shop/product/i2c-to-uart-gpio/
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

Massimo
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 (http://www.adafruit.com/products/1293) 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 (http://github.com/plieningerweb/esp8266-software-uart) 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 (https://github.com/p-v-o-s/micropython/ ... 8a89287a8f).
This firmware can be built according to Adafruit's helpful Feather HUZZAH guide (https://learn.adafruit.com/building-and ... 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 (https://github.com/p-v-o-s/micropython/ ... 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 https://github.com/dmascord/micropython.

I have also pulled in the performance improvements from https://github.com/juancgalvez/Arduino- ... 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 https://github.com/kirtikaran3/TM4C/blo ... 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 ?

Cheers,

Damien

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
ets_intr_lock();
.
.
.
// 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 https://github.com/esp8266/Arduino/issues/2218

SoftUART with MH-Z19B

Posted: Sun Dec 27, 2020 1:01 pm
by hcet14
Hello Damien,

I tried your softuart with a MH-Z19 Co2 sensor. Didn't work. I opened an issue on github https://github.com/dmascord/micropython/issues/2.

I really appreciate your good work.

Keep coding, hcet14

MH-Z19 with SoftUART is reading correct CO2 values

Posted: Tue Jan 05, 2021 8:19 pm
by hcet14
MH-Z19 with SoftUART from https://github.com/dmascord/micropython is reading correct CO2 values 8-)
I had some problems, cause I used a faked sensor. Took a while to find that out :evil:

That's my setup https://ibb.co/tM7n6bZ

Bad news is, the uart isn't working correctly :roll:

I got the feeling Damien (tusker) is going in the right direction. To bad, he isn't working on his fork anymore. He forked from p-v-o-s, who forked from plieningerweb. You can see the differences in the code from plieningerweb to dmascord. Did somebody tried p-v-o-s or plieningerweb?

Are there any people around here, who are interested and willing to get Damien's SoftUART working?

Would it be better to start with this https://github.com/plerup/espsoftwareserial EspSoftwareSerial? Hooking up MH-Z19 on the Arduino platform with this uart worked flawless.