-
-
Save vishalseshagiri/0057c3cea5859e5b0dad to your computer and use it in GitHub Desktop.
Simple code to test Arduino to Python serial communication
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
#!/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