After looking to this example
https://github.com/peterhinch/micropyth ... r/auart.py
I came up with this (simplified for the reference) code
Code: Select all
class SIM:
@classmethod
def init(cls):
cls.uart = UART(2, 9600, timeout=1)
cls._sreader = asyncio.StreamReader(cls.uart)
cls._swriter = asyncio.StreamWriter(cls.uart, {})
@classmethod
async def setup(cls):
await cls.command('AT+CMGF=1') # plain text SMS
@classmethod
async def command(cls, cmdstr, lines=1, waitfor=0.5 ):
# empty the uart
while True:
chunk = yield from cls._sreader.read(1)
if not chunk:
break
await cls._swriter.awrite("{}\n".format(message))
await asyncio.sleep(waitfor)
cls._buf = await cls._sreader.readline()
if not cls._buf:
return None
result = cls._buf
log.info("result::'{}'".format(result))
if lines > 1:
cls._savbuf = ''
for i in range(lines):
cls._buf = await cls._sreader.readline()
if not cls._buf:
break
if not cls._buf == b'' and not cls._buf == b'OK':
cls._savbuf += cls._buf+'\n'
return result
# APP ----------------
sim.init()
loop = asyncio.get_event_loop()
loop.create_task(sim.setup())
loop.run_forever()
StreamReader.readline() never times out, and the timeout setting in UART(2, 9600, timeout=1) is not working.
What is the right way to go with asyncio and UART?..