class SerialConnection: def __init__(self): self._ser = None self.port = None def open(self, port = None, baudrate = 115200): 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("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)]: try: self._ser = serial.Serial(port, baudrate) print("connected to " + port) self.port = port break except serial.SerialException: pass else: try: self._ser = serial.Serial(self.port, baudrate) print("connected to " + self.port) except serial.SerialException: pass else: print("connection to port {} failed".format(port)) def send(self, bytes): if self._ser: try: return self._ser.write(bytes) except serial.SerialException: self._ser = None if not self._ser: self.open() if not self._ser: time.sleep(30) def read(self, n=64): if self._ser: try: if self._ser.in_waiting: return self._ser.read(n) except serial.SerialException: self._ser = None if not self._ser: self.open() def isConnected(self): return self._ser != None def close(self): if self._ser: self._ser.close() class ArduinoSlave(SerialConnection): def __init__(self): self.sensorData = (0) * 12 def open(self): #thread: sensorData = read() pass def getAcusticRTTs(self): return self.sensorData[0:2] def getMagneticField(self): return self.sensorData[2:5]