Skip to content

Instantly share code, notes, and snippets.

@aequitas
Created November 11, 2016 20:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aequitas/b10b71264ae75eb35d71ee3cacbc1441 to your computer and use it in GitHub Desktop.
Save aequitas/b10b71264ae75eb35d71ee3cacbc1441 to your computer and use it in GitHub Desktop.
# 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