Created
October 6, 2011 18:23
-
-
Save julsam/1268186 to your computer and use it in GitHub Desktop.
Arduino Python SerialDialog
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
int incomingByte = 0; | |
void | |
setup() | |
{ | |
Serial.begin(9600); | |
} | |
void | |
loop() | |
{ | |
if (Serial.available() > 0) | |
{ | |
Serial.print("Data received: "); | |
do | |
{ | |
incomingByte = Serial.read(); | |
Serial.print(incomingByte, BYTE); | |
} while (Serial.available() > 0); | |
} | |
delay(100); | |
} |
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/env python | |
# -*- coding: utf-8 -*- | |
import serial | |
class SerialDialog: | |
def __init__(self, serialPort, ): | |
self.serialPort = serialPort | |
self.serial = None | |
self.quit = False | |
self.bind() | |
def bind(self): | |
try: | |
self.serial = serial.Serial(self.serialPort, 9600, timeout=2) | |
print "\q to quit." | |
print "Connected to", self.serial | |
except: | |
print "Failed to connect to", self.serialPort | |
def close(self): | |
self.serial.close() | |
def write(self): | |
_input = raw_input("input: ") | |
self.serial.write(_input) | |
if _input == "\q": | |
self.quit = True | |
def read(self): | |
data = self.serial.readline() | |
if data: | |
print "output:", data | |
data = "" | |
def loop(self): | |
while not self.quit: | |
self.read() | |
self.write() | |
if __name__ == "__main__": | |
s = SerialDialog('/dev/ttyACM0') | |
s.loop() | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment