Преглед на файлове

updated serial reconnect to work with raspberry pi

subDesTagesMitExtraKaese преди 3 години
родител
ревизия
99b99943d9
променени са 3 файла, в които са добавени 31 реда и са изтрити 29 реда
  1. 2 1
      raspberry-pi/config.ini
  2. 1 1
      raspberry-pi/sensors/acusticSensor.py
  3. 28 27
      raspberry-pi/sensors/connection.py

+ 2 - 1
raspberry-pi/config.ini

@@ -7,8 +7,9 @@
   y = 450
 
 [arduino]
+  # leave port empty for auto config
+  # port = 
   port = /dev/ttyACM0
-  baudrate = 1000000
 
 # acoustic sensor config
 [ac_sensor]

+ 1 - 1
raspberry-pi/sensors/acusticSensor.py

@@ -35,7 +35,7 @@ class AcusticSensor:
 
   def start(self):
     if not conn.isConnected():
-      conn.open(port = self.conf["arduino"]["port"], baudrate = int(self.conf["arduino"]["baudrate"]))
+      conn.open(port = self.conf["arduino"]["port"])
     conn.addRecvCallback(self._readCb)
     # generate dummy values until arduino is ready
     self.dummyActive = True

+ 28 - 27
raspberry-pi/sensors/connection.py

@@ -2,6 +2,7 @@ import threading
 import time
 
 import serial
+import serial.tools.list_ports
 
 
 class SerialConnection:
@@ -11,36 +12,28 @@ class SerialConnection:
   
   # try connecting to an available port
   def open(self, port = None, baudrate = 1000000):
-    self.port = port
+    if port != None:
+      self.port = port
 
-    if self.port == None:
-      for port in ["/dev/ttyUSB{}".format(p) for p in range(4)]:
-        try:
-          self._ser = serial.Serial(port, baudrate)
-          print("SERIAL: connected to " + port)
-          self.port = port
-          break
-        except serial.SerialException:
-          pass
-      
-    if self.port == None:
-      for port in ["COM{}".format(p) for p in range(3,20)]:
+    if self.port:
+      try:
+        self._ser = serial.Serial(self.port, baudrate)
+        print("SERIAL: connected to " + self.port)
+      except serial.SerialException as e:
+        print("SERIAL:", e)
+
+    else:
+      for port, name, _ in serial.tools.list_ports.comports():
         try:
           self._ser = serial.Serial(port, baudrate)
-          print("SERIAL: connected to " + port)
+          print("SERIAL: connected to " + port +  ", name: " + name)
           self.port = port
           break
-        except serial.SerialException:
-          pass
-    else:
-      try:
-        self._ser = serial.Serial(self.port, baudrate)
-        print("SERIAL: connected to " + self.port)
-      except serial.SerialException:
-        pass
+        except serial.SerialException as e:
+          print("SERIAL:", e)
 
     if not self._ser:
-        print("SERIAL: connection failed")
+        print("SERIAL: no device available")
     
   def send(self, bytes):
     if self._ser:
@@ -57,9 +50,13 @@ class SerialConnection:
     if self._ser:
       try:
         return self._ser.read(n)
-      except AttributeError:
+      except AttributeError as e:
+        print("SERIAL:", e)
         return None
-      except serial.SerialException:
+      except serial.SerialException as e:
+        print("SERIAL:", e)
+        if self._ser.isOpen():
+          self._ser.close()
         self._ser = None
         return None
     else:
@@ -71,9 +68,13 @@ class SerialConnection:
     if self._ser:
       try:
         return self._ser.readline()
-      except AttributeError:
+      except AttributeError as e:
+        print("SERIAL:", e)
         return None
-      except serial.SerialException:
+      except serial.SerialException as e:
+        print("SERIAL:", e)
+        if self._ser.isOpen():
+          self._ser.close()
         self._ser = None
         return None
     else: