Page 2 of 2

Re: Import Pyserial Module

Posted: Sun Jun 16, 2019 7:13 am
by pythoncoder
A good way to handle concurrency is to use uasyncio. The following demo can be pasted into your Pyboard REPL:

Code: Select all

import uasyncio as asyncio
from pyb import USB_VCP
uart = USB_VCP()

swriter = asyncio.StreamWriter(uart, {})
sreader = asyncio.StreamReader(uart)

async def sender():
    while True:
        await swriter.awrite('Hello uart\r\n')
        await asyncio.sleep(4)

async def receiver():
    while True:
        res = await sreader.read()
        await swriter.awrite('Recieved {:s}\r\n'.format(res))

loop = asyncio.get_event_loop()
loop.create_task(sender())
loop.create_task(receiver())
loop.run_forever()
Your continuous loop would be another task for concurrent execution. A guide to using uasyncio may be found in the tutorial here.