|
@@ -14,36 +14,19 @@ class Ardvindo:
|
|
|
|
|
|
def __init__(self, comPort):
|
|
def __init__(self, comPort):
|
|
self.comPort = comPort
|
|
self.comPort = comPort
|
|
- self.comConnection = None
|
|
|
|
- self.connect()
|
|
|
|
-
|
|
|
|
- def connected(self):
|
|
|
|
- return self.comConnection != None and self.ping()
|
|
|
|
-
|
|
|
|
- def reconnect(self):
|
|
|
|
- while True:
|
|
|
|
- if self.comConnection != None:
|
|
|
|
- self.comConnection.close()
|
|
|
|
- self.comConnection = serial.Serial(self.comPort, 9600, timeout = 0.5)
|
|
|
|
- # establishing an serial connection automatically resets the arduino
|
|
|
|
- # wait for arduino to restart
|
|
|
|
- time.sleep(4)
|
|
|
|
- if self._readline() == "ardvindo started":
|
|
|
|
- log("Connection established!")
|
|
|
|
- break
|
|
|
|
- else:
|
|
|
|
- log("Failed to connect!")
|
|
|
|
-
|
|
|
|
- def connect(self):
|
|
|
|
- if not self.connected():
|
|
|
|
- self.reconnect()
|
|
|
|
-
|
|
|
|
- def ping(self):
|
|
|
|
- return self.sendCommand("i") == "ardvindo"
|
|
|
|
|
|
+ self.comConnection = serial.Serial(self.comPort, 9600, timeout = 0.5)
|
|
|
|
+ # establishing an serial connection automatically resets the arduino
|
|
|
|
+ # wait for arduino to restart
|
|
|
|
+ time.sleep(4)
|
|
|
|
+ if self._readline() == "ardvindo started":
|
|
|
|
+ log("Connection established!")
|
|
|
|
+ else:
|
|
|
|
+ log("Failed to connect!")
|
|
|
|
+ raise Exception("Failed to connect!")
|
|
|
|
|
|
def _writeline(self, line):
|
|
def _writeline(self, line):
|
|
- log("Sending '" + str(line) + "'... ")
|
|
|
|
- self.comConnection.write(line + b"\n")
|
|
|
|
|
|
+ log("Sending '" + line + "'... ")
|
|
|
|
+ self.comConnection.write(line.encode() + b"\n")
|
|
|
|
|
|
def _readline(self):
|
|
def _readline(self):
|
|
line = self.comConnection.readline().decode().strip()
|
|
line = self.comConnection.readline().decode().strip()
|
|
@@ -51,11 +34,11 @@ class Ardvindo:
|
|
return line
|
|
return line
|
|
|
|
|
|
def sendCommand(self, cmd):
|
|
def sendCommand(self, cmd):
|
|
- self._writeline(cmd.encode())
|
|
|
|
- log("Command '" + cmd + "' sent!")
|
|
|
|
- return self._readline()
|
|
|
|
|
|
+ self.comConnection.flushInput()
|
|
|
|
+ self._writeline(cmd)
|
|
|
|
|
|
-ardvindo = Ardvindo(serial.tools.list_ports.comports()[0][0])
|
|
|
|
|
|
+#ardvindo = Ardvindo(serial.tools.list_ports.comports()[0][0])
|
|
|
|
+ardvindo = Ardvindo("/dev/ardvindo")
|
|
|
|
|
|
class ServerRequestHandler(socketserver.BaseRequestHandler):
|
|
class ServerRequestHandler(socketserver.BaseRequestHandler):
|
|
|
|
|