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

windows: Add machine.UART support for serial COM ports.#18533

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
kimstik wants to merge4 commits intomicropython:master
base:master
Choose a base branch
Loading
fromkimstik:master

Conversation

@kimstik
Copy link

Summary

Implement machine.UART class for Windows port, providing access to serial COM ports with an API compatible with other MicroPython ports.

Features:

  • Support for COM1-COM255 via integer or string identifier
  • Standard UART parameters: baudrate, bits, parity, stop, flow control
  • Configurable timeouts (timeout, timeout_char)
  • Configurable buffer sizes (rxbuf, txbuf)
  • Stream protocol support (read, write, poll, flush)
  • UART.list() class method to enumerate available COM ports
  • sendbreak() and readchar()/writechar() support

Implementation uses Win32 API (CreateFile, DCB, COMMTIMEOUTS) with the MICROPY_PY_MACHINE_UART_INCLUDEFILE pattern for integration.

Testing

CI Testing:

  • GitHub Actions: All builds pass (MSVC 2017/2019/2022 x86/x64, MinGW32/64)
  • Test suite: 823/824 tests pass (see skip note below)

Hardware Testing:

  • Tested with physical USB-to-serial adapter

Skipped Tests:

  • extmod/machine_uart_tx.py - requires physical UART with loopback wiring
  • extmod/machine_uart_irq_txidle.py - requires IRQ support (not implemented)

Note:mpremote build may fail in CI due to upstream issue with
hatch-vcs version detection in forked repository (not related
to this PR).

from machine import UARTimport sysprint("Ports:", ports := UART.list())if ports:    uart = UART(ports[0], 115200, timeout=100)    print(uart)    uart.write(b"Hello from MicroPython\r\n")    while True:        d = uart.read(256)        if d:            sys.stdout.write(d)# TODO:# * select.poll(stdin)# * sys.stdin.any()

dpgeorge reacted with rocket emoji
@kimstikkimstik changed the titlewindows: Add machine.UART support for serial COM portswindows: Add machine.UART support for serial COM ports.Dec 8, 2025
Implement machine.UART class for Windows port, providing access toserial COM ports with an API compatible with other MicroPython ports.Features:- Support for COM1-COM255 via integer or string identifier- Standard UART parameters: baudrate, bits, parity, stop, flow control- Configurable timeouts (timeout, timeout_char)- Configurable buffer sizes (rxbuf, txbuf)- Stream protocol support (read, write, poll, flush)- UART.list() class method to enumerate available COM ports- sendbreak() and readchar()/writechar() supportImplementation uses Win32 API (CreateFile, DCB, COMMTIMEOUTS) withthe MICROPY_PY_MACHINE_UART_INCLUDEFILE pattern for integration.Signed-off-by: Konstantin Kim <kimstik@gmail.com>
@codecov
Copy link

codecovbot commentedDec 8, 2025
edited
Loading

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.38%. Comparing base (2bd337e) to head (0575173).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@##           master   #18533   +/-   ##=======================================  Coverage   98.38%   98.38%           =======================================  Files         171      171             Lines       22300    22300           =======================================  Hits        21939    21939             Misses        361      361

☔ 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.

- Fix UART.list() to check ERROR_NO_MORE_ITEMS specifically and skip  entries that fail due to buffer too small instead of breaking early.- Make sendbreak duration baudrate-dependent (13 bit periods) like rp2.- Disable DTR by default to avoid resetting Arduino/ESP devices.- Add UART_PORT_NAME_MAX constant for consistent buffer sizes.- Remove DEFAULT_UART_* macros, use inline values in one place.- Add comments for flow control flags and buffer sizes.Signed-off-by: Konstantin Kim <kimstik@gmail.com>
@kimstikkimstikforce-pushed themaster branch 2 times, most recently fromc65fec6 toeda90cdCompareDecember 10, 2025 16:23
no loopback requirementSigned-off-by: Konstantin Kim <kimstik@gmail.com>
@Josverl
Copy link
Contributor

Should there be an option to filter out the Bluetooth serial.?

Bluetooth serial: when Windows creates outgoing/incoming RFCOMM ports, they appear as COMx and are listed in the registry.

AFAIK the registry does not provide sufficient detail for that.

kimstik reacted with thumbs up emoji

@kimstik
Copy link
Author

kimstik commentedDec 11, 2025
edited
Loading

Should there be an option to filter out the Bluetooth serial.?

Bluetooth serial: when Windows creates outgoing/incoming RFCOMM ports, they appear as COMx and are listed in the registry.

AFAIK the registry does not provide sufficient detail for that.

Good idea — could be added to a TODO/wishlist for post-merge enhancements (e.g. UART.list(bluetooth=False) or similar).
For now, Bluetooth SPP ports work identically to physical serial.
Users may legitimately want to communicate with BT devices via UART API.
Let's keep it simple and let users decide what they need.

RegEnumValueA already returns the string length in portNameSize,so strlen() call is redundant.Signed-off-by: Konstantin Kim <kimstik@gmail.com>
@kimstik
Copy link
Author

@dpgeorge,@stinos
Feel free to let me know if I missed anything.
Are there any suggestions to improve the chances of a merge?

@Josverl
Copy link
Contributor

could be added to a TODO/wishlist for post-merge enhancements
Let's keep it simple and let users decide what they need.

My point is that by choosing the registry API - this will not be possible other than by replacing it entirely with another API that does provide access to richer information.
I think this is a relevant design decision at this point, and should not just be punted forward

@kimstik
Copy link
Author

kimstik commentedDec 15, 2025
edited
Loading

could be added to a TODO/wishlist for post-merge enhancements
Let's keep it simple and let users decide what they need.

My point is that by choosing the registry API - this will not be possible other than by replacing it entirely with another API that does provide access to richer information. I think this is a relevant design decision at this point, and should not just be punted forward

Valid concern. Registry API (advapi32) indeed cannot distinguish Bluetooth ports.

However:

  1. Other MicroPython ports (esp32, stm32, rp2) don't filter hardware either
  2. pyserial's comports() also returns all ports - filtering is user's responsibility
  3. SetupAPI would add ~50% more code and setupapi.dll dependency
  4. Future enhancement possible without breaking changes (add optional filter param)

Even pyserial has open PR#70 (since 2016) to add Registry support - SetupAPI misses virtual ports (com0com).
For v1, keeping it simple and consistent with other ports seems reasonable. We can revisit if users request filtering.

PS, my short research
- Registry        : advapi32 WinNT+   - Sees busy ports. No metadata (can't filter BT).                  [libserialport, jSSC/Arduino, .NET Runtime, Sysinternals Portmon]- QueryDosDevice  : kernel32 WinXP+   - Same issues + buffer guesswork.                                  [-]- SetupAPI        : setupapi Win2000+ - +50% code, slower - for metadata we don't need.                  [pyserial, node-serialport]- CreateFile loop : kernel32 Any      - Slow, misses busy ports, side effects (DTR toggle). antipattern. [Tera Term legacy]- WMI             : ole32+   Win2000+ - Massive complexity for no benefit. overkill

@Josverl
Copy link
Contributor

Josverl commentedDec 16, 2025
edited
Loading

My experience and feedback from mpflash users was that they did not want enumeration and connections to Bluetooth serial ports by default (Windows, Linux, nor Mac) , and want an opt-in for the edge cases.

While enumeration and connection to all potential ports is different from single connection I think this view from the community is a relevant input.

Thanks for sharing your research
To me only Registry or SetupAPI make sense. but as Pyserial apparently uses SetupAPI under the hood, there is no need to rip-and replace in the future.

Filtering BT could be just using Pyserial, similar to themflash BT filter

So I think this PR is a good starting point.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@dpgeorgedpgeorgedpgeorge left review comments

+1 more reviewer

@stinosstinosstinos left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@kimstik@Josverl@stinos@dpgeorge

[8]ページ先頭

©2009-2025 Movatter.jp