Skip to content

Instantly share code, notes, and snippets.

@jeppeburchardt
Last active April 13, 2018 16:03
Show Gist options
  • Save jeppeburchardt/4609602 to your computer and use it in GitHub Desktop.
Save jeppeburchardt/4609602 to your computer and use it in GitHub Desktop.
python arduino usb serial example
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