Skip to content

Commit 921f397

Browse files
committed
tools/mpremote: Only auto connect to serial device with USB VID/PID.
On MacOS and Windows there are a few default serial devices that are returned by `serial.tools.list_ports.comports()`. For example on MacOS: ``` {'description': 'n/a', 'device': '/dev/cu.Bluetooth-Incoming-Port', 'hwid': 'n/a', 'interface': None, 'location': None, 'manufacturer': None, 'name': 'cu.Bluetooth-Incoming-Port', 'pid': None, 'product': None, 'serial_number': None, 'vid': None} {'description': 'n/a', 'device': '/dev/cu.wlan-debug', 'hwid': 'n/a', 'interface': None, 'location': None, 'manufacturer': None, 'name': 'cu.wlan-debug', 'pid': None, 'product': None, 'serial_number': None, 'vid': None} ``` Users of mpremote most likely do not want to connect to these ports. It would be desirable if mpremote did not select this ports when using the auto connect behavior. These serial ports do not have USB VID or PID values and serial ports for Micropython boards with FTDI/serial-to-USB adapter or native USB CDC/ACM support do. Check for the presence of a USB VID / PID int value when selecting a serial port to auto connect to. All serial ports will still be listed by the `list` command and can still be selected by name when connecting. Signed-off-by: Michael Mogenson <michael.mogenson@gmail.com>
1 parent 3c1a2a9 commit 921f397

3 files changed

Lines changed: 16 additions & 15 deletions

File tree

docs/reference/mpremote.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ The simplest way to use this tool is just by invoking it without any arguments:
1919
2020
mpremote
2121
22-
This command automatically detects and connects to the first available serial
23-
device and provides an interactive REPL. Serial ports are opened in exclusive
24-
mode, so running a second (or third, etc) instance of ``mpremote`` will connect
25-
to subsequent serial devices, if any are available.
22+
This command automatically detects and connects to the first available USB
23+
serial device and provides an interactive REPL. Serial ports are opened in
24+
exclusive mode, so running a second (or third, etc) instance of ``mpremote``
25+
will connect to subsequent serial devices, if any are available.
2626

2727

2828
Commands
@@ -52,7 +52,7 @@ The full list of supported commands are:
5252
``<device>`` may be one of:
5353

5454
- ``list``: list available devices
55-
- ``auto``: connect to the first available device
55+
- ``auto``: connect to the first available USB serial port
5656
- ``id:<serial>``: connect to the device with USB serial number
5757
``<serial>`` (the second entry in the output from the ``connect list``
5858
command)
@@ -186,8 +186,8 @@ Auto connection and soft-reset
186186

187187
Connection and disconnection will be done automatically at the start and end of
188188
the execution of the tool, if such commands are not explicitly given. Automatic
189-
connection will search for the first available serial device. If no action is
190-
specified then the REPL will be entered.
189+
connection will search for the first available USB serial device. If no action
190+
is specified then the REPL will be entered.
191191

192192
Once connected to a device, ``mpremote`` will automatically soft-reset the
193193
device if needed. This clears the Python heap and restarts the interpreter,

tools/mpremote/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The simplest way to use this tool is:
77

88
mpremote
99

10-
This will automatically connect to the device and provide an interactive REPL.
10+
This will automatically connect to a USB serial port and provide an interactive REPL.
1111

1212
The full list of supported commands are:
1313

tools/mpremote/mpremote/commands.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ def do_connect(state, args=None):
3232
# Don't do implicit REPL command.
3333
state.did_action()
3434
elif dev == "auto":
35-
# Auto-detect and auto-connect to the first available device.
35+
# Auto-detect and auto-connect to the first available USB serial port.
3636
for p in sorted(serial.tools.list_ports.comports()):
37-
try:
38-
state.pyb = pyboard.PyboardExtended(p.device, baudrate=115200)
39-
return
40-
except pyboard.PyboardError as er:
41-
if not er.args[0].startswith("failed to access"):
42-
raise er
37+
if p.vid is not None and p.pid is not None:
38+
try:
39+
state.pyb = pyboard.PyboardExtended(p.device, baudrate=115200)
40+
return
41+
except pyboard.PyboardError as er:
42+
if not er.args[0].startswith("failed to access"):
43+
raise er
4344
raise pyboard.PyboardError("no device found")
4445
elif dev.startswith("id:"):
4546
# Search for a device with the given serial number.

0 commit comments

Comments
 (0)