Browse Source

fixed serial reconnect loop

subDesTagesMitExtraKaese 3 years ago
parent
commit
2152b36e12
1 changed files with 16 additions and 6 deletions
  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):