Skip to content

Commit 67d875b

Browse files
committed
Solve serial close raise problem.
1 parent f89b98b commit 67d875b

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

pymodbus/client/serial_asyncio/__init__.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -422,20 +422,23 @@ def _call_connection_lost(self, exc):
422422
assert self._closing
423423
assert not self._has_writer
424424
assert not self._has_reader
425-
try:
426-
self._serial.flush()
427-
except (serial.SerialException if os.name == "nt" else termios.error):
428-
# ignore serial errors which may happen if the serial device was
429-
# hot-unplugged.
430-
pass
431-
try:
432-
self._protocol.connection_lost(exc)
433-
finally:
434-
self._write_buffer.clear()
425+
if self._serial:
426+
try:
427+
self._serial.flush()
428+
except (serial.SerialException if os.name == "nt" else termios.error):
429+
# ignore serial errors which may happen if the serial device was
430+
# hot-unplugged.
431+
pass
435432
self._serial.close()
436433
self._serial = None
437-
self._protocol = None
438-
self._loop = None
434+
if self._protocol:
435+
try:
436+
self._protocol.connection_lost(exc)
437+
except:
438+
pass
439+
self._write_buffer.clear()
440+
self._write_buffer.clear()
441+
self._loop = None
439442

440443

441444
async def create_serial_connection(loop, protocol_factory, *args, **kwargs):

0 commit comments

Comments
 (0)