Skip to content

Instantly share code, notes, and snippets.

@vishalseshagiri
Forked from eraclitux/py-arduino.py
Last active September 20, 2015 17:05
Show Gist options
  • Save vishalseshagiri/0057c3cea5859e5b0dad to your computer and use it in GitHub Desktop.
Save vishalseshagiri/0057c3cea5859e5b0dad to your computer and use it in GitHub Desktop.
Simple code to test Arduino to Python serial communication
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Sample code to test Arduino to Python serial communication
"""
import serial
error_message="################ !!! Communication Error !!! ################\n"
def mainloop():
while True:
try:
try:
#Create a connection with Arduino board and set 1 sec timeout
conn = serial.Serial('/dev/ttyUSB0', timeout=1)
tbytes = raw_input('Write something (exit does what you think): ') + '\n'
if tbytes == 'exit\n' :
break
conn.write(tbytes)
print '\n' + 'Arduino received: ' + conn.readline()
except serial.SerialException:
raw_input(error_message)
break
except KeyboardInterrupt:
conn.close()
break
if __name__ == '__main__':
mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment