Skip to content

Instantly share code, notes, and snippets.

@MrYsLab
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrYsLab/86e488b1b27a613b8a6c to your computer and use it in GitHub Desktop.
Save MrYsLab/86e488b1b27a613b8a6c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import asyncio
import serial
class ArduinoSerial():
def __init__(self):
"""
Open the serial port at 57600
:return: Instance reference
"""
self.arduino = serial.Serial("/dev/ttyACM1", 57600, timeout=.05, writeTimeout=None)
@asyncio.coroutine
def async_read_line(self):
"""
This method keeps retrieving a line of text from the serial port
:return: Should never return
"""
try:
while True:
the_string = self.arduino.readline()
print(the_string)
except GeneratorExit:
print("Should never get here.")
if __name__ == "__main__":
# create an instance of the ArduinoSerial class
the_board = ArduinoSerial()
# get the event loop
loop = asyncio.get_event_loop()
# create an asyncio Task to read serial data
asyncio.Task(the_board.async_read_line())
# start the loop
try:
loop.run_forever()
loop.close()
except serial.SerialException():
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment