From a82763533958c23e4d043c596161e38fe3ad3d8c Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Fri, 21 Feb 2025 10:40:30 +0100 Subject: [PATCH 1/7] Update writerthread.py --- src/velbustcp/lib/connection/serial/writerthread.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/velbustcp/lib/connection/serial/writerthread.py b/src/velbustcp/lib/connection/serial/writerthread.py index c5d5db2..e4c619b 100644 --- a/src/velbustcp/lib/connection/serial/writerthread.py +++ b/src/velbustcp/lib/connection/serial/writerthread.py @@ -12,7 +12,7 @@ class WriterThread(threading.Thread): def __init__(self, serial_instance: Serial): - self.alive: bool = True + self.alive: bool = False self.__serial: Serial = serial_instance self.__logger = logging.getLogger("__main__." + __name__) self.__send_event: threading.Event = threading.Event() @@ -51,7 +51,8 @@ def run(self) -> None: """ last_send_time = time.monotonic() - + self.alive: bool = True + while self.alive and self.__serial.is_open: self.__send_event.wait() self.__send_event.clear() From 81271ed6c236664e285775f2e16f6859138092dc Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Fri, 21 Feb 2025 10:43:05 +0100 Subject: [PATCH 2/7] Update writerthread.py --- src/velbustcp/lib/connection/serial/writerthread.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/velbustcp/lib/connection/serial/writerthread.py b/src/velbustcp/lib/connection/serial/writerthread.py index e4c619b..c0ab453 100644 --- a/src/velbustcp/lib/connection/serial/writerthread.py +++ b/src/velbustcp/lib/connection/serial/writerthread.py @@ -52,7 +52,7 @@ def run(self) -> None: last_send_time = time.monotonic() self.alive: bool = True - + while self.alive and self.__serial.is_open: self.__send_event.wait() self.__send_event.clear() From 4c18718b3b4fc7bb0982ae3adcfa9c3aec88b9dc Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Fri, 21 Feb 2025 10:45:33 +0100 Subject: [PATCH 3/7] Update writerthread.py --- src/velbustcp/lib/connection/serial/writerthread.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/velbustcp/lib/connection/serial/writerthread.py b/src/velbustcp/lib/connection/serial/writerthread.py index c0ab453..dcc2b0e 100644 --- a/src/velbustcp/lib/connection/serial/writerthread.py +++ b/src/velbustcp/lib/connection/serial/writerthread.py @@ -51,7 +51,7 @@ def run(self) -> None: """ last_send_time = time.monotonic() - self.alive: bool = True + self.alive = True while self.alive and self.__serial.is_open: self.__send_event.wait() From d8fe963c0ee8d0af9993953cb8a6a00e27dbfd59 Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Fri, 21 Feb 2025 11:49:37 +0100 Subject: [PATCH 4/7] Close the serial port allocation --- src/velbustcp/lib/connection/serial/bus.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/velbustcp/lib/connection/serial/bus.py b/src/velbustcp/lib/connection/serial/bus.py index 8a041bb..01e8622 100644 --- a/src/velbustcp/lib/connection/serial/bus.py +++ b/src/velbustcp/lib/connection/serial/bus.py @@ -120,6 +120,8 @@ def __start(self) -> None: self.__writer = WriterThread(serial_port) self.__writer.start() + self.__serial_port = serial_port + self.__logger.info("Serial connection active on port %s", self.__port) def stop(self) -> None: @@ -141,6 +143,9 @@ def stop(self) -> None: if self.__writer and self.__writer.alive: self.__writer.close() + if self.__serial_port.isOpen(): + self.__serial_port.close() + def send(self, packet: bytearray) -> None: """Queues a packet to be sent on the serial connection. From 4d3b7b895a117e3b571016b8ccdca1286cfb10c8 Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Fri, 21 Feb 2025 11:57:47 +0100 Subject: [PATCH 5/7] Update bus.py --- src/velbustcp/lib/connection/serial/bus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/velbustcp/lib/connection/serial/bus.py b/src/velbustcp/lib/connection/serial/bus.py index 01e8622..ab161b9 100644 --- a/src/velbustcp/lib/connection/serial/bus.py +++ b/src/velbustcp/lib/connection/serial/bus.py @@ -120,7 +120,7 @@ def __start(self) -> None: self.__writer = WriterThread(serial_port) self.__writer.start() - self.__serial_port = serial_port + self.__serial_port = serial_port self.__logger.info("Serial connection active on port %s", self.__port) From 765dc5d1075fa0b9de673422ec786c90ab7fba18 Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Fri, 21 Feb 2025 13:33:08 +0100 Subject: [PATCH 6/7] Add cancel read and cancel_write --- src/velbustcp/lib/connection/serial/bus.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/velbustcp/lib/connection/serial/bus.py b/src/velbustcp/lib/connection/serial/bus.py index ab161b9..6da896d 100644 --- a/src/velbustcp/lib/connection/serial/bus.py +++ b/src/velbustcp/lib/connection/serial/bus.py @@ -144,6 +144,8 @@ def stop(self) -> None: self.__writer.close() if self.__serial_port.isOpen(): + self.__serial_port.cancel_read() + self.__serial_port.cancel_write() self.__serial_port.close() def send(self, packet: bytearray) -> None: From 67f4e8c67382a514b0481f7b24e640ff32eba126 Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Fri, 21 Feb 2025 15:13:03 +0100 Subject: [PATCH 7/7] Don't rely on thread safety when closing / opening serial port --- src/velbustcp/lib/connection/serial/bus.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/velbustcp/lib/connection/serial/bus.py b/src/velbustcp/lib/connection/serial/bus.py index 6da896d..9b6356d 100644 --- a/src/velbustcp/lib/connection/serial/bus.py +++ b/src/velbustcp/lib/connection/serial/bus.py @@ -51,6 +51,8 @@ def handle_on_bus_fault(sender, **kwargs): self.__do_reconnect: bool = False self.__connected: bool = False + self.__serial_port = None + def __reconnect(self) -> None: """Reconnects until active. """ @@ -104,24 +106,23 @@ def __start(self) -> None: if not self.__port: raise ValueError("Couldn't find a port to open communication on") - serial_port = construct_serial_obj(self.__port) + if self.__serial_port is None: + self.__serial_port = construct_serial_obj(self.__port) - if not serial_port.isOpen(): + if not self.__serial_port.isOpen(): raise Exception("Couldn't open port {0}".format(self.__port)) # Now that we're connected, set connected state self.__connected = True # Create reader thread - self.__reader = serial.threaded.ReaderThread(serial_port, VelbusSerialProtocol()) + self.__reader = serial.threaded.ReaderThread(self.__serial_port, VelbusSerialProtocol()) self.__reader.start() # Create write thread - self.__writer = WriterThread(serial_port) + self.__writer = WriterThread(self.__serial_port) self.__writer.start() - self.__serial_port = serial_port - self.__logger.info("Serial connection active on port %s", self.__port) def stop(self) -> None: @@ -143,11 +144,6 @@ def stop(self) -> None: if self.__writer and self.__writer.alive: self.__writer.close() - if self.__serial_port.isOpen(): - self.__serial_port.cancel_read() - self.__serial_port.cancel_write() - self.__serial_port.close() - def send(self, packet: bytearray) -> None: """Queues a packet to be sent on the serial connection.