From b6c193c4d20b7fcd71e91b673d47f649b299c13e Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Wed, 10 Sep 2025 17:14:17 +0900 Subject: [PATCH 1/5] update moxa_serial.py to use specific exceptions (TimeoutError and BlockingIOError) in reading data --- socs/common/moxa_serial.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/socs/common/moxa_serial.py b/socs/common/moxa_serial.py index 8e5f6021c..18facf49f 100644 --- a/socs/common/moxa_serial.py +++ b/socs/common/moxa_serial.py @@ -1,3 +1,5 @@ +# 9/2025 Shun +# Change all BaseExeption --> TimeoutError # 4/2009 BAS # read() replicates behavior of pyserial # readexactly() added, which is probably more useful @@ -87,7 +89,7 @@ def readexactly(self, n): self.settimeout(newtimeout) try: msg = self.sock.recv(n, socket.MSG_PEEK) - except BaseException: + except (TimeoutError, BlockingIOError): pass # Flush the message out if you got everything if len(msg) == n: @@ -114,7 +116,7 @@ def readbuf_slow(self, n): try: for i in range(n): msg += self.sock.recv(1) - except BaseException: + except (TimeoutError, BlockingIOError): pass self.sock.setblocking(1) # belt and suspenders self.settimeout(self.__timeout) @@ -132,9 +134,9 @@ def readbuf(self, n): return '' try: msg = self.sock.recv(n) - except BaseException: + except (TimeoutError, BlockingIOError): msg = '' - n2 = min(n - len(msg), n / 2) + n2 = min(n - len(msg), (int)(n / 2)) return msg + self.readbuf(n2) def readpacket(self, n): @@ -147,7 +149,7 @@ def readpacket(self, n): """ try: msg = self.sock.recv(n) - except BaseException: + except (TimeoutError, BlockingIOError): msg = '' return msg @@ -221,7 +223,7 @@ def flushInput(self): try: while len(self.sock.recv(1)) > 0: pass - except BaseException: + except (TimeoutError, BlockingIOError): pass self.sock.setblocking(1) self.sock.settimeout(self.__timeout) From 3d309ea4c3fed9687a7fc4d0140d486e9a5dc633 Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi Date: Thu, 11 Sep 2025 00:01:52 +0000 Subject: [PATCH 2/5] update --- socs/common/moxa_serial.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/socs/common/moxa_serial.py b/socs/common/moxa_serial.py index 18facf49f..79448ee19 100644 --- a/socs/common/moxa_serial.py +++ b/socs/common/moxa_serial.py @@ -1,5 +1,3 @@ -# 9/2025 Shun -# Change all BaseExeption --> TimeoutError # 4/2009 BAS # read() replicates behavior of pyserial # readexactly() added, which is probably more useful @@ -122,7 +120,7 @@ def readbuf_slow(self, n): self.settimeout(self.__timeout) return msg - def readbuf(self, n): + def readbuf(self, n, max_loop=10): """Returns whatever is currently in the buffer. Suitable for large buffers. @@ -130,14 +128,19 @@ def readbuf(self, n): n: Number of bytes to read. """ - if n == 0: - return '' - try: - msg = self.sock.recv(n) - except (TimeoutError, BlockingIOError): - msg = '' - n2 = min(n - len(msg), (int)(n / 2)) - return msg + self.readbuf(n2) + n_current = n + msg = '' + for i in range(max_loop): + if n_current <= 0: + return '' + try: + msg_current = self.sock.recv(n) + except (TimeoutError, BlockingIOError): + msg_current = '' + msg += msg_current + n_current -= len(msg_current) + + return msg def readpacket(self, n): """Like ``read()``, but may not return everything if the moxa box From 58056c87df60d2fff173d68329a3774e8a590c9b Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi Date: Thu, 11 Sep 2025 00:12:41 +0000 Subject: [PATCH 3/5] update moxa_serial.py --- socs/common/moxa_serial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/socs/common/moxa_serial.py b/socs/common/moxa_serial.py index 79448ee19..002b46294 100644 --- a/socs/common/moxa_serial.py +++ b/socs/common/moxa_serial.py @@ -132,7 +132,7 @@ def readbuf(self, n, max_loop=10): msg = '' for i in range(max_loop): if n_current <= 0: - return '' + return msg try: msg_current = self.sock.recv(n) except (TimeoutError, BlockingIOError): From 8c95060784000c12320752bc34e2c1d129a28b1d Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi <38268229+sadachi5@users.noreply.github.com> Date: Thu, 25 Sep 2025 17:20:08 +0900 Subject: [PATCH 4/5] Update moxa_serial.py In `readbuf()`, use a byte type string for `msg` and `msg_current`. --- socs/common/moxa_serial.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/socs/common/moxa_serial.py b/socs/common/moxa_serial.py index 002b46294..109375f79 100644 --- a/socs/common/moxa_serial.py +++ b/socs/common/moxa_serial.py @@ -129,14 +129,14 @@ def readbuf(self, n, max_loop=10): """ n_current = n - msg = '' + msg = b'' for i in range(max_loop): if n_current <= 0: return msg try: msg_current = self.sock.recv(n) except (TimeoutError, BlockingIOError): - msg_current = '' + msg_current = b'' msg += msg_current n_current -= len(msg_current) From b00294461b1b3b7a9f7f5d93f43c14e73ad0369a Mon Sep 17 00:00:00 2001 From: Brian Koopman Date: Mon, 3 Nov 2025 10:51:44 -0500 Subject: [PATCH 5/5] Add general exception handling Since we were catching BaseException before, there's some chance we were catching something that isn't a TimeoutError or BlockingIOError. This catches anything else and logs it, just in case. --- socs/common/moxa_serial.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/socs/common/moxa_serial.py b/socs/common/moxa_serial.py index 109375f79..95129a0ee 100644 --- a/socs/common/moxa_serial.py +++ b/socs/common/moxa_serial.py @@ -89,6 +89,9 @@ def readexactly(self, n): msg = self.sock.recv(n, socket.MSG_PEEK) except (TimeoutError, BlockingIOError): pass + except Exception as e: + print(f"Caught unexpected {type(e).__name__} exception:") + print(f" {e}") # Flush the message out if you got everything if len(msg) == n: if self.encoded: @@ -116,6 +119,9 @@ def readbuf_slow(self, n): msg += self.sock.recv(1) except (TimeoutError, BlockingIOError): pass + except Exception as e: + print(f"Caught unexpected {type(e).__name__} exception:") + print(f" {e}") self.sock.setblocking(1) # belt and suspenders self.settimeout(self.__timeout) return msg @@ -137,6 +143,10 @@ def readbuf(self, n, max_loop=10): msg_current = self.sock.recv(n) except (TimeoutError, BlockingIOError): msg_current = b'' + except Exception as e: + print(f"Caught unexpected {type(e).__name__} exception:") + print(f" {e}") + msg_current = b'' msg += msg_current n_current -= len(msg_current) @@ -154,6 +164,10 @@ def readpacket(self, n): msg = self.sock.recv(n) except (TimeoutError, BlockingIOError): msg = '' + except Exception as e: + print(f"Caught unexpected {type(e).__name__} exception:") + print(f" {e}") + msg = '' return msg def read(self, n): @@ -228,6 +242,9 @@ def flushInput(self): pass except (TimeoutError, BlockingIOError): pass + except Exception as e: + print(f"Caught unexpected {type(e).__name__} exception:") + print(f" {e}") self.sock.setblocking(1) self.sock.settimeout(self.__timeout)