Created
November 11, 2016 20:34
-
-
Save aequitas/b10b71264ae75eb35d71ee3cacbc1441 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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