Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# example reading two serial devices using pyserial, asyncio and coroutines
import serial_asyncio
import asyncio
@asyncio.coroutine
def serial_read(device, **kwargs):
reader, writer = yield from serial_asyncio.open_serial_connection(url=device, **kwargs)
while True:
line = yield from reader.readline()
print(line.decode('ascii').strip())
loop = asyncio.get_event_loop()
tasks = [
serial_read('/dev/ttyACM0', baudrate=57600),
serial_read('/dev/ttyUSB0', baudrate=9600, bytesize=7),
]
asyncio.gather(*tasks)
loop.run_forever()
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.