Skip to content

Commit 1088cc6

Browse files
committed
Merge branch 'revert-close-refactor'
2 parents f15a0bd + aeec544 commit 1088cc6

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

meshtastic/serial_interface.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,10 @@ def __repr__(self):
9494

9595
def close(self) -> None:
9696
"""Close a connection to the device"""
97-
if hasattr(self, "stream") and self.stream and getattr(self.stream, "is_open", False):
98-
try:
99-
self.stream.flush()
100-
time.sleep(0.1)
101-
except Exception as e:
102-
logger.debug(f"Exception during flush: {e}")
103-
try:
104-
self.stream.close()
105-
except Exception as e:
106-
logger.debug(f"Exception during close: {e}")
107-
self.stream = None
97+
if self.stream: # Stream can be null if we were already closed
98+
self.stream.flush() # FIXME: why are there these two flushes with 100ms sleeps? This shouldn't be necessary
99+
time.sleep(0.1)
100+
self.stream.flush()
101+
time.sleep(0.1)
108102
logger.debug("Closing Serial stream")
109103
StreamInterface.close(self)

meshtastic/stream_interface.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,15 @@ def _disconnected(self) -> None:
9393

9494
logger.debug("Closing our port")
9595
# pylint: disable=E0203
96-
if hasattr(self, "stream") and self.stream is not None and getattr(self.stream, "is_open", False):
96+
if not self.stream is None:
9797
# pylint: disable=E0203
98-
try:
99-
self.stream.close()
100-
except Exception as e:
101-
logger.debug(f"Exception during close: {e}")
98+
self.stream.close()
10299
# pylint: disable=W0201
103100
self.stream = None
104101

105102
def _writeBytes(self, b: bytes) -> None:
106103
"""Write an array of bytes to our stream and flush"""
107-
if self.stream and self.stream is not None and getattr(self.stream, "is_open", False): # ignore writes when stream is closed
104+
if self.stream: # ignore writes when stream is closed
108105
self.stream.write(b)
109106
self.stream.flush()
110107
# win11 might need a bit more time, too
@@ -116,7 +113,7 @@ def _writeBytes(self, b: bytes) -> None:
116113

117114
def _readBytes(self, length) -> Optional[bytes]:
118115
"""Read an array of bytes from our stream"""
119-
if self.stream and self.stream is not None and getattr(self.stream, "is_open", False):
116+
if self.stream:
120117
return self.stream.read(length)
121118
else:
122119
return None
@@ -226,12 +223,10 @@ def __reader(self) -> None:
226223
logger.error(
227224
f"Unexpected OSError, terminating meshtastic reader... {ex}"
228225
)
229-
traceback.print_exc()
230226
except Exception as ex:
231227
logger.error(
232228
f"Unexpected exception, terminating meshtastic reader... {ex}"
233229
)
234-
traceback.print_exc()
235230
finally:
236231
logger.debug("reader is exiting")
237232
self._disconnected()

0 commit comments

Comments
 (0)