Explorar o código

fixed serial reconnect loop

subDesTagesMitExtraKaese %!s(int64=3) %!d(string=hai) anos
pai
achega
2152b36e12
Modificáronse 1 ficheiros con 16 adicións e 6 borrados
  1. 16 6
      raspberry-pi/connection.py

+ 16 - 6
raspberry-pi/connection.py

@@ -43,25 +43,35 @@ class SerialConnection:
         return self._ser.write(bytes)
       except serial.SerialException:
         self._ser = None
-    if not self._ser:
+    else:
       self.open()
       if not self._ser:
-        time.sleep(30)
+        time.sleep(10)
           
   def read(self, n=64):
     if self._ser:
       try:
-        if self._ser.in_waiting:
-          return self._ser.read(n)
+        return self._ser.read(n)
       except serial.SerialException:
         self._ser = None
-    if not self._ser:
+        return None
+    else:
       self.open()
+      if not self._ser:
+        time.sleep(10)
+      return None
 
   def readline(self):
     if self._ser:
-      return self._ser.readline()
+      try:
+        return self._ser.readline()
+      except serial.SerialException:
+        self._ser = None
+        return None
     else:
+      self.open()
+      if not self._ser:
+        time.sleep(10)
       return None
 
   def isConnected(self):