micropython icon indicating copy to clipboard operation
micropython copied to clipboard

tools/mpremote: Fix auto-connection bug in macOS and add config option to filter ports eligible for auto-connection.

Open mdaeron opened this issue 1 year ago • 3 comments

Signed-off-by: Mathieu Daëron [email protected]

mdaeron avatar Aug 29 '22 16:08 mdaeron

This PR corrects a bug on macOS, where cu.Bluetooth-Incoming-Port is considered an eligible port for mpremote auto connect (#8485). The fix is perhaps brittle (check sys.platform), and could need to be extended to correct similar bugs on other operating systems.

It also introduces a new configuration option, autoconnect_regexp, which is a regular expression used to filter ports eligible for auto connection. This option will generally not be needed, but is useful when more granular control of which ports to use is called for, e.g. when debugging code in several boards communicating/working together

mdaeron avatar Aug 29 '22 16:08 mdaeron

I don't think that PR is a good idea. Since the docs say "This command automatically detects and connects to the first available serial device" and Macs for some reason are not doing that then surely the solution is to fix that automatic connection logic rather than add an obscure "autoconnect_regexp" option which users are required to discover and configure?

bulletmark avatar Aug 30 '22 10:08 bulletmark

I don't think that PR is a good idea. Since the docs say "This command automatically detects and connects to the first available serial device" and Macs for some reason are not doing that then surely the solution is to fix that automatic connection logic rather than add an obscure "autoconnect_regexp" option which users are required to discover and configure?

Fair point, thanks. I agree that solving the auto connect bug and adding an option to filter ports are two separate issues. I've edited the PR accordingly.

  • mpremote connect auto now works out of the box on my Mac. Something similar might be needed for Windows.
  • autoconnect_regexp remains as an (obscure, yet documented) option for those few who might need it. In my experience, it can save a lot of time and frustration when debugging two or more boards working/communicating together (in which case auto connect is great) without interference from other ports which might be used for other things.

mdaeron avatar Aug 30 '22 11:08 mdaeron

rshell has about 30 lines of heuristics for autoconnection that work pretty well and may be worth borrowing: https://github.com/dhylands/rshell/blob/b87878c01aa02d6cd871ff4666983dee5584af16/rshell/main.py#L227-L254

ide avatar Oct 14 '22 05:10 ide

Thanks for the contribution. The ideas in this PR are good, namely:

  1. improve auto connection on mac
  2. allow the user to have more control over auto connection

The solution to 1, restricting to /dev/cu.usb seems pretty good.

But I think for 2, since it's going to be a rarely used command and therefore advanced, how about making it fully general by allowing an arbitrary filter function to be provided by the user? Eg something like

             port_filter = lambda p: True # default filter
             ...
             for p in sorted(serial.tools.list_ports.comports()):
                 if not port_filter(p):
                     continue
                 ...

dpgeorge avatar Oct 31 '22 01:10 dpgeorge