Does ESP8266 has softuart module
Posted: Mon Dec 12, 2016 4:49 am
Does ESP8266's micropython has softuart module? Becasue I need more uart.
Please see the new forum at
https://forum.micropython.org/
I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.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.
That sounds very interesting. Are you willing to share this code with the community?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.shaoziyang wrote: I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.
It is work fine with 1200bps, I have not test it for other baudrate, it maybe need to adjust self.bitdelay value.Roberthh wrote:That sounds very interesting. Are you willing to share this code with the community?I hvae write a soft uart module today, it can works, but it can't receive a lot of 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
please see above, I have only test 1200bps.JonHylands wrote:I would be interested in this also. I need to connect to a GPS, running at 9600 baud.shaoziyang wrote: I hvae write a soft uart module today, it can works, but it can't receive a lot of continuous data.