Created
April 30, 2011 20:46
-
-
Save biomood/949981 to your computer and use it in GitHub Desktop.
Example script for sending and receiving data via an arduino
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/python | |
import serial | |
import time | |
# try connecting to serial port | |
try: | |
print 'MSG: Connecting to arduino' | |
arduino = serial.Serial('/dev/tty.usbserial-A600agDn', 9600) | |
# wait for the arduino to reset | |
time.sleep(2) | |
except: | |
print 'ERR: Unable to connect to arduino' | |
try: | |
print 'MSG: Sending data to arduino' | |
# write a single char to the arduino | |
arduino.write('3') | |
print 'MSG: Sent data to arduino' | |
except: | |
print 'ERR: Unable to send data' | |
arduino.close() | |
try: | |
# wait for arduino to reply | |
time.sleep(1) | |
print 'MSG: Attempting to read from arduino' | |
# read one byte from the arduino | |
print arduino.read(1) | |
except: | |
print 'ERR: Unable to read data properly' | |
arduino.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment