Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

mpremote: Add automatic reconnect feature.#17322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
andrewleech wants to merge2 commits intomicropython:master
base:master
Choose a base branch
Loading
fromandrewleech:mpremote_reconnect

Conversation

andrewleech
Copy link
Contributor

Summary

This PR adds automatic reconnection capability to the mpremote tool. When a MicroPython device disconnects, mpremote can now automatically reconnect to the same device, eliminating the need to manually
restart the tool during development.

The newreconnect command enables automatic reconnection to the exact same port, even when usingauto mode. This is particularly important in multi-device environments where you want to ensure reconnection to the same physical device.

Key features:

  • Newreconnect command that works likeresume (standalone command, not a flag)
  • Always reconnects to the exact same port that was previously connected when originally started inconnect auto mode
  • Always reconnects to the exact same device (on any port) when started inconnect id:abc123 mode.
  • Preserves resume state through reconnections
  • Handles Windows COM port timing issues with retry logic

Usage:

mpremote reconnectmpremote connect id:cc8da28c04c40000 reconnect replmpremote connect /dev/ttyACM0 reconnect

This PR builds on top of#17321 which improves disconnect message handling.

Testing

Testing was performed on:

  • Windows 11 with ESP32S2 devices connected via USB (COM ports)
  • Linux (Ubuntu WSL) with ESP32S2 devices connected via USB (/dev/ttyACM*)

Tested scenarios:

  • Basic reconnection after device unplug/replug cycles
  • Using auto mode and verifying it reconnects to the same specific port (e.g., only /dev/ttyACM2)
  • Windows-specific COM port timing issues requiring multiple connection attempts
  • Multiple devices present to verify it only reconnects to the original device
  • Connection types: auto, specific ports, and device serial numbers (id:)

All tests confirmed that the tool reliably reconnects to the same physical device.

Trade-offs and Alternatives

The implementation adds minimal code complexity and has no impact on users who don't use the reconnect feature. The reconnect command is opt-in and doesn't affect existing behavior.

Design decisions:

  • Implemented as a standalone command rather than a --reconnect flag for consistency with other mpremote commands like resume
  • Always reconnects to the exact same port (even in auto mode) rather than any available device, prioritizing safety in multi-device environments
  • Added retry logic with delays specifically for Windows COM port timing issues

The only trade-off is a small increase in code size from the reconnect logic, which is justified by the significant improvement in developer workflow.

robert-hh reacted with thumbs up emoji
- Handle SerialException on Windows when device disconnects- Print clean 'device disconnected' message instead of stack trace- Fix terminal formatting issues on Linux after disconnect- Return disconnected state after console cleanup to avoid terminal issuesThis ensures proper disconnect messages on both platforms withoutshowing confusing error traces to users.Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@codecovCodecov
Copy link

codecovbot commentedMay 19, 2025
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.54%. Comparing base(45e4deb) to head(70057c8).
Report is 56 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@##           master   #17322      +/-   ##==========================================- Coverage   98.54%   98.54%   -0.01%==========================================  Files         169      169                Lines       21898    21897       -1     ==========================================- Hits        21580    21579       -1  Misses        318      318

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report?Share it here.

🚀 New features to boost your workflow:
  • ❄️Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actionsGitHub Actions
Copy link

Code size report:

   bare-arm:    +0 +0.000% minimal x86:    +0 +0.000%    unix x64:    +0 +0.000% standard      stm32:    +0 +0.000% PYBV10     mimxrt:    +0 +0.000% TEENSY40        rp2:    +0 +0.000% RPI_PICO_W       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS  qemu rv32:    +0 +0.000% VIRT_RV32

@Josverl
Copy link
Contributor

Josverl commentedMay 19, 2025
edited
Loading

Thanks for the effort.

Implemented as a standalone command rather than a --reconnect flag for consistency with other mpremote commands like resume

I have never liked that I find myself needing to type 'resume' to 95% of the commands.
I think that that resume and reconnect should be the default state, as that is what most users will expect.

  • I think the main use for soft resetting before each command is automated testing, and there adding a reset is simple. But changing that behavior will need some thought, as it may be a breaking change for tools that depend on it ( although the docs say they should not)

But for this very welcome reconnect behavior, I would propose this to be the default, and to be opted out via--no-reconnect/--nrc or environment variable.

That will make the "how do we educate this" so much simpler,
Note that theresume/no-reset behavior appears to be the default forreconnect.

Physical disconnection of a battery powered board with a running script.

mpremote reconnectConnected to MicroPython at COM10Use Ctrl-] or Ctrl-x to exit this shell>>> import time>>> x=1>>> while 1:...     time.sleep(1)...     print("hoi",x) ...     x+=1...     ...     ... hoi 1hoi 2hoi 3hoi 4hoi 5hoi 6hoi 7hoi 8# Unplug USB Device disconnectedWaiting for reconnection on COM10...# Reconnect USB Connected to MicroPython at COM10Use Ctrl-] or Ctrl-x to exit this shellhoi 12hoi 13hoi 14hoi 15# .... sniphoi 24hoi 25hoi 26# Pressed Ctrl-](micropython) PS D:\mypython\micropython> mpremote reconnectConnected to MicroPython at COM10Use Ctrl-] or Ctrl-x to exit this shellhoi 30hoi 31hoi 32hoi 33hoi 34hoi 35hoi 36hoi 37hoi 38hoi 39

This adds a `reconnect` command that enables automatic reconnectionwhen a device disconnects. The reconnect feature:- For direct port connections: reconnects to the exact same port- For `id:` connections: recoonect to same device on any port- Shows clear status messages during reconnection- Preserves resume state through reconnections- Handles Windows timing issues with retry logicUsage: mpremote connect auto reconnect repl       mpremote connect id:1234567890 reconnect replThe reconnect command behaves differently based on connection type:- For `auto` and direct ports: only reconnects to the exact same port- For `id:` connections: finds the device on any available portThis prevents accidental connections to different devices when using automode, while allowing id-based connections to find the device wherever itreappears.Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@andrewleech
Copy link
ContributorAuthor

Thanks@Josverl for bringing up the defaults!

Yes I too have thought it would be better to haveresume the default, but not entirely sure of what the range of user effects this might have. I do think resume would be less confusing for many people, with an optionalreset to be used to invert that.

I'm certainly interested in this new reconnect feature being on by default, I expect to use it asmy default by adding it to the local config file etc at the very least. I'd certainly be happy to make that change if we get such a concensus from the maintainers!

@andrewleechandrewleech changed the titleMpremote reconnectmpremote: Add automatic reconnect feature.May 19, 2025
@Josverl
Copy link
Contributor

Josverl commentedMay 19, 2025
edited
Loading

adding it to the local config file etc at the very least.

AFAIK mpremote finding its config file is still broken on Windows -#9573 would be needed for that

andrewleech reacted with thumbs up emoji

@dpgeorgedpgeorge added the toolsRelates to tools/ directory in source, or other tooling labelMay 20, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
toolsRelates to tools/ directory in source, or other tooling
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@andrewleech@Josverl@dpgeorge@pi-anl

[8]ページ先頭

©2009-2025 Movatter.jp