Page 1 of 1

Does ESP8266 has softuart module

Posted: Mon Dec 12, 2016 4:49 am
by shaoziyang
Does ESP8266's micropython has softuart module? Becasue I need more uart.

Re: Does ESP8266 has softuart module

Posted: Mon Dec 12, 2016 6:08 am
by Roberthh
No, it does not have a software UART. Implementing that in Python would possible, if 2400 Baud (or less) is sufficient. Anything faster has to be implemented in the firmware.

Re: Does ESP8266 has softuart module

Posted: Mon Dec 12, 2016 12:25 pm
by mcauser
The ESP8266 has a 2nd hardware UART, but it is TX only.

Re: Does ESP8266 has softuart module

Posted: Mon Dec 12, 2016 1:15 pm
by shaoziyang
Roberthh wrote:No, it does not have a software UART. Implementing that in Python would possible, if 2400 Baud (or less) is sufficient. Anything faster has to be implemented in the firmware.
I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.

Re: Does ESP8266 has softuart module

Posted: Tue Dec 13, 2016 8:08 pm
by Roberthh
I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.
That sounds very interesting. Are you willing to share this code with the community?

Re: Does ESP8266 has softuart module

Posted: Sat Dec 31, 2016 2:37 pm
by JonHylands
shaoziyang wrote: I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.
I would be interested in this also. I need to connect to a GPS, running at 9600 baud.

Re: Does ESP8266 has softuart module

Posted: Fri Jan 06, 2017 2:49 pm
by shaoziyang
Roberthh wrote:
I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.
That sounds very interesting. Are you willing to share this code with the community?
It is work fine with 1200bps, I have not test it for other baudrate, it maybe need to adjust self.bitdelay value.

And in receive mode, it will loss data if receive continuous data.

Code: Select all

import time
import machine
from machine import Pin

class SOFTUART(object):
    def __init__(self, tx, rx, baud = 1200):
        self.tx = tx
        self.rx = rx
        self.tx.init(Pin.OUT)
        self.rx.init(Pin.IN)
        self.baud(baud)
        self.timeout = False

    def baud(self, baud):
        self.bitdelay = 1000000 // baud - 200

    def dt(self, dt=''):
        if dt=='':
            return self.bitdelay
        else:
            self.bitdelay = dt

    def putc(self, dat):
        self.tx(0)
        time.sleep_us(self.bitdelay)
        for i in range(8):
            self.tx(dat%2)
            dat = dat >> 1
            time.sleep_us(self.bitdelay)
        self.tx(1)
        time.sleep_us(self.bitdelay)

    def puts(self, str, cr=1):
        for i in range(len(str)):
            time.sleep_ms(1)
            self.putc(ord(str[i]))
        if cr > 0:
            time.sleep_ms(1)
            self.putc(0x0D)

    def put(self, buf):
        for i in range(len(buf)):
            time.sleep_ms(1)
            self.putc(buf[i])

    def timeout(self):
        return self.timeout

    def getc(self, timeout=20):
        t = 0
        self.timeout = False
        while self.rx():
            time.sleep_us(20)
            t = t + 1
            if t < timeout*5:
                pass
            else:
                self.timeout = True
                return -1
        time.sleep_us(self.bitdelay + self.bitdelay//8)
        dat = 0
        for i in range(8):
            dat = dat >> 1
            if self.rx():
                dat = dat | 0x80
            time.sleep_us(self.bitdelay)
        time.sleep_us(self.bitdelay)
        return dat

    def get(self, num=0):
        dat=bytearray(0)
        while True:
            t = self.getc()
            if self.timeout:
                return dat
            else:
                dat.append(t)
            if num > 0:
                if num > 1:
                    num = num - 1
                else:
                    return dat
softuart.zip
(695 Bytes) Downloaded 731 times

Re: Does ESP8266 has softuart module

Posted: Fri Jan 06, 2017 2:49 pm
by shaoziyang
JonHylands wrote:
shaoziyang wrote: I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.
I would be interested in this also. I need to connect to a GPS, running at 9600 baud.
please see above, I have only test 1200bps.

Re: Does ESP8266 has softuart module

Posted: Sat Jan 07, 2017 7:55 am
by pythoncoder
See also http://forum.micropython.org/viewtopic.php?f=16&t=2204. I haven't tested it but it looks very promising.