浏览代码

fixed serial reconnect loop

subDesTagesMitExtraKaese 3 年之前
父节点
当前提交
2152b36e12
共有 1 个文件被更改,包括 16 次插入6 次删除
  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):