From 891a17ea6263510d4fb1641dad4a0a613c2ccd07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ga=C5=88o?= Date: Mon, 1 Feb 2021 11:40:03 +0100 Subject: [PATCH] Fixed automatic connection in idf_monitor Closes https://github.com/espressif/esp-idf/issues/6415 --- tools/idf_py_actions/errors.py | 5 +++++ tools/idf_py_actions/serial_ext.py | 21 +++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tools/idf_py_actions/errors.py b/tools/idf_py_actions/errors.py index e5b8eabbe7..9876d4d526 100644 --- a/tools/idf_py_actions/errors.py +++ b/tools/idf_py_actions/errors.py @@ -2,9 +2,14 @@ class FatalError(RuntimeError): """ Wrapper class for runtime errors that aren't caused by bugs in idf.py or the build process. """ + def __init__(self, message, ctx=None): super(RuntimeError, self).__init__(message) # if context is defined, check for the cleanup tasks if ctx is not None and 'cleanup' in ctx.meta: # cleans up the environment before failure ctx.meta['cleanup']() + + +class NoSerialPortFoundError(FatalError): + pass diff --git a/tools/idf_py_actions/serial_ext.py b/tools/idf_py_actions/serial_ext.py index a7f5760851..3aa7fe8f6a 100644 --- a/tools/idf_py_actions/serial_ext.py +++ b/tools/idf_py_actions/serial_ext.py @@ -3,7 +3,7 @@ import os import sys import click -from idf_py_actions.errors import FatalError +from idf_py_actions.errors import FatalError, NoSerialPortFoundError from idf_py_actions.global_options import global_options from idf_py_actions.tools import ensure_build_directory, get_sdkconfig_value, run_target, run_tool @@ -19,12 +19,21 @@ def action_extensions(base_actions, project_path): sys.path.insert(0, esptool_path) import esptool ports = list(sorted(p.device for p in serial.tools.list_ports.comports())) - esp = esptool.get_default_connected_device(serial_list=ports, port=None, connect_attempts=3, - initial_baud=args.baud) + # high baud rate could cause the failure of creation of the connection + esp = esptool.get_default_connected_device(serial_list=ports, port=None, connect_attempts=4, + initial_baud=115200) + if esp is None: + raise NoSerialPortFoundError( + "No serial ports found. Connect a device, or use '-p PORT' option to set a specific port.") - return esp.serial_port - except Exception: - raise FatalError("No serial ports found. Connect a device, or use '-p PORT' option to set a specific port.") + serial_port = esp.serial_port + esp._port.close() + + return serial_port + except NoSerialPortFoundError: + raise + except Exception as e: + raise FatalError('An exception occurred during detection of the serial port: {}'.format(e)) def _get_esptool_args(args): esptool_path = os.path.join(os.environ['IDF_PATH'], 'components/esptool_py/esptool/esptool.py')