Last active
April 13, 2018 16:03
-
-
Save jeppeburchardt/4609602 to your computer and use it in GitHub Desktop.
python arduino usb serial example
This file contains hidden or 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
import serial | |
from time import sleep | |
usb_ports = ('/dev/ttyUSB0', '/dev/ttyUSB1') | |
while 1: | |
for usb in usb_ports: | |
try: | |
#print('Connecting to arduino at ' + usb) | |
auduino = serial.Serial(usb, 9600) | |
print('Connected to arduio at ' + usb) | |
while 1: | |
try: | |
output = auduino.readline() | |
print(output) | |
except KeyboardInterrupt: | |
exit() | |
except serial.SerialException: | |
print('Lost connection. Trying to reconnect...') | |
break | |
except IOError: | |
print('IOError...') | |
pass | |
except serial.SerialException: | |
print('Arduino not found at ' + usb) | |
pass | |
sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment