Skip to content

Commit b466225

Browse files
authored
Merge pull request #841 from SpudGunMan/master
refactor close()
2 parents 2065598 + 6f35eb3 commit b466225

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

meshtastic/serial_interface.py

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

9595
def close(self) -> None:
9696
"""Close a connection to the device"""
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)
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
102108
logger.debug("Closing Serial stream")
103109
StreamInterface.close(self)

0 commit comments

Comments
 (0)