From da4f3fb4bb8d5bfc65bde97f0abe8c840e2b8fa6 Mon Sep 17 00:00:00 2001 From: Andrew O'Shei Date: Wed, 2 Feb 2022 17:20:14 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20Add=20Try/Except=20test=20to=20p?= =?UTF-8?q?revent=20I/O=20errors=E2=80=A6=20(#4384)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added Try/Except test to prevent I/O errors when connecting to a nonexistent serial port * Added an additional possible failure condition to log message * Fixed log message formatting --- src/octoprint/util/comm.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 4a893cbc1..61cfc4a01 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -3710,23 +3710,30 @@ class MachineCom: if settings().getBoolean(["serial", "exclusive"]): serial_port_args["exclusive"] = True - serial_obj = serial.Serial(**serial_port_args) - serial_obj.port = str(p) + try: + serial_obj = serial.Serial(**serial_port_args) + serial_obj.port = str(p) - use_parity_workaround = settings().get(["serial", "useParityWorkaround"]) - needs_parity_workaround = get_os() == "linux" and os.path.exists( - "/etc/debian_version" - ) # See #673 + use_parity_workaround = settings().get(["serial", "useParityWorkaround"]) + needs_parity_workaround = get_os() == "linux" and os.path.exists( + "/etc/debian_version" + ) # See #673 + + if use_parity_workaround == "always" or ( + needs_parity_workaround and use_parity_workaround == "detect" + ): + serial_obj.parity = serial.PARITY_ODD + serial_obj.open() + serial_obj.close() + serial_obj.parity = serial.PARITY_NONE - if use_parity_workaround == "always" or ( - needs_parity_workaround and use_parity_workaround == "detect" - ): - serial_obj.parity = serial.PARITY_ODD serial_obj.open() - serial_obj.close() - serial_obj.parity = serial.PARITY_NONE - serial_obj.open() + except serial.SerialException: + self._logger.info( + "Failed to connect: Port {} is busy or does not exist".format(p) + ) + return None # Set close_exec flag on serial handle, see #3212 if hasattr(serial_obj, "fd"):