|
@@ -3,12 +3,14 @@ import time
|
|
|
|
|
|
import serial
|
|
import serial
|
|
import serial.tools.list_ports
|
|
import serial.tools.list_ports
|
|
|
|
+import Log_handler
|
|
|
|
|
|
|
|
|
|
class SerialConnection:
|
|
class SerialConnection:
|
|
def __init__(self):
|
|
def __init__(self):
|
|
self._ser = None
|
|
self._ser = None
|
|
self.port = None
|
|
self.port = None
|
|
|
|
+ self.log_handler = Log_handler.get_log_handler()
|
|
|
|
|
|
# try connecting to an available port
|
|
# try connecting to an available port
|
|
def open(self, port = None, baudrate = 1000000):
|
|
def open(self, port = None, baudrate = 1000000):
|
|
@@ -19,21 +21,27 @@ class SerialConnection:
|
|
try:
|
|
try:
|
|
self._ser = serial.Serial(self.port, baudrate)
|
|
self._ser = serial.Serial(self.port, baudrate)
|
|
print("SERIAL: connected to " + self.port)
|
|
print("SERIAL: connected to " + self.port)
|
|
|
|
+ self.log_handler.add_item("SERIAL: connected to " + self.port)
|
|
|
|
+
|
|
except serial.SerialException as e:
|
|
except serial.SerialException as e:
|
|
print("SERIAL:", e)
|
|
print("SERIAL:", e)
|
|
|
|
+ self.log_handler.add_item("SERIAL:", e)
|
|
|
|
|
|
else:
|
|
else:
|
|
for port, name, _ in serial.tools.list_ports.comports():
|
|
for port, name, _ in serial.tools.list_ports.comports():
|
|
try:
|
|
try:
|
|
self._ser = serial.Serial(port, baudrate)
|
|
self._ser = serial.Serial(port, baudrate)
|
|
print("SERIAL: connected to " + port + ", name: " + name)
|
|
print("SERIAL: connected to " + port + ", name: " + name)
|
|
|
|
+ self.log_handler.add_item("SERIAL: connected to " + port + ", name: " + name)
|
|
self.port = port
|
|
self.port = port
|
|
break
|
|
break
|
|
except serial.SerialException as e:
|
|
except serial.SerialException as e:
|
|
print("SERIAL:", e)
|
|
print("SERIAL:", e)
|
|
|
|
+ self.log_handler.add_item("SERIAL:", e)
|
|
|
|
|
|
if not self._ser:
|
|
if not self._ser:
|
|
print("SERIAL: no device available")
|
|
print("SERIAL: no device available")
|
|
|
|
+ self.log_handler.add_item("SERIAL: no device available")
|
|
|
|
|
|
def send(self, bytes):
|
|
def send(self, bytes):
|
|
if self._ser:
|
|
if self._ser:
|
|
@@ -52,9 +60,11 @@ class SerialConnection:
|
|
return self._ser.read(n)
|
|
return self._ser.read(n)
|
|
except AttributeError as e:
|
|
except AttributeError as e:
|
|
print("SERIAL:", e)
|
|
print("SERIAL:", e)
|
|
|
|
+ self.log_handler.add_item("SERIAL:", e)
|
|
return None
|
|
return None
|
|
except serial.SerialException as e:
|
|
except serial.SerialException as e:
|
|
print("SERIAL:", e)
|
|
print("SERIAL:", e)
|
|
|
|
+ self.log_handler.add_item("SERIAL:", e)
|
|
if self._ser.isOpen():
|
|
if self._ser.isOpen():
|
|
self._ser.close()
|
|
self._ser.close()
|
|
self._ser = None
|
|
self._ser = None
|
|
@@ -70,9 +80,11 @@ class SerialConnection:
|
|
return self._ser.readline()
|
|
return self._ser.readline()
|
|
except AttributeError as e:
|
|
except AttributeError as e:
|
|
print("SERIAL:", e)
|
|
print("SERIAL:", e)
|
|
|
|
+ self.log_handler.add_item("SERIAL:", e)
|
|
return None
|
|
return None
|
|
except serial.SerialException as e:
|
|
except serial.SerialException as e:
|
|
print("SERIAL:", e)
|
|
print("SERIAL:", e)
|
|
|
|
+ self.log_handler.add_item("SERIAL:", e)
|
|
if self._ser.isOpen():
|
|
if self._ser.isOpen():
|
|
self._ser.close()
|
|
self._ser.close()
|
|
self._ser = None
|
|
self._ser = None
|
|
@@ -151,6 +163,7 @@ class ArduinoSlave(SerialConnection):
|
|
cb(self.sensorData)
|
|
cb(self.sensorData)
|
|
else:
|
|
else:
|
|
print("SERIAL: ", data[:-2])
|
|
print("SERIAL: ", data[:-2])
|
|
|
|
+ self.log_handler.add_item("SERIAL: ", data[:-2])
|
|
|
|
|
|
# this allows the usage of a single instance of ArduinoSlave
|
|
# this allows the usage of a single instance of ArduinoSlave
|
|
_conn = None
|
|
_conn = None
|