Import Pyserial Module

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
User avatar
pythoncoder
Posts: 3495
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Import Pyserial Module

Post by pythoncoder » Sun Jun 16, 2019 7:13 am

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.
Peter Hinch

Post Reply