From 72040f2c44880ac8a86a13fcd0537b71e84acff0 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Mon, 14 Oct 2019 08:06:43 +0200 Subject: [PATCH] idf_monitor: Autodetect port Closes https://github.com/espressif/esp-idf/issues/4191 --- tools/idf_monitor.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/idf_monitor.py b/tools/idf_monitor.py index ff34019dab..8fdcecd835 100755 --- a/tools/idf_monitor.py +++ b/tools/idf_monitor.py @@ -46,6 +46,7 @@ import shlex import time import sys import serial +import serial.tools.list_ports import serial.tools.miniterm as miniterm import threading import ctypes @@ -764,12 +765,24 @@ class Monitor(object): def main(): + + def _get_default_serial_port(): + """ + Same logic for detecting serial port as esptool.py and idf.py: reverse sort by name and choose the first port. + """ + + try: + ports = list(reversed(sorted(p.device for p in serial.tools.list_ports.comports()))) + return ports[0] + except Exception: + return '/dev/ttyUSB0' + parser = argparse.ArgumentParser("idf_monitor - a serial output monitor for esp-idf") parser.add_argument( '--port', '-p', help='Serial port device', - default=os.environ.get('ESPTOOL_PORT', '/dev/ttyUSB0') + default=os.environ.get('ESPTOOL_PORT', _get_default_serial_port()) ) parser.add_argument( -- 2.40.0