Last active
August 29, 2015 14:20
-
-
Save MrYsLab/86e488b1b27a613b8a6c 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
#!/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