Skip to content

Retry opening the display COM port instead of exiting on first failure - #1057

Open
axectly wants to merge 1 commit into
mathoudebine:mainfrom
axectly:pr/retry-open-com-port
Open

Retry opening the display COM port instead of exiting on first failure#1057
axectly wants to merge 1 commit into
mathoudebine:mainfrom
axectly:pr/retry-open-com-port

Conversation

@axectly

@axectly axectly commented Aug 18, 2026

Copy link
Copy Markdown

Problem

openSerial() stops the whole program as soon as the COM port cannot be opened:

except Exception as e:
    logger.error(f"Cannot open COM port {self.com_port}: {e}")
    sys.exit(0)

That single attempt is easy to lose at boot. The program resets the screen at startup (RESET_ON_STARTUP), which disconnects and reconnects the device, so its COM port disappears for a few seconds and can come back under a different name. When the program is started automatically at logon, it can hit exactly that window and exit, leaving the screen showing nothing until the user starts it again by hand.

Real log of an autostart at boot, on Windows 11 with a Turing 5" (rev C):

[DEBUG] Waiting for device COM3 - USB Serial Device (COM3) to be turned ON...
[DEBUG] Detected screen turned ON
[DEBUG] Auto detected COM port: COM4
[ERROR] Cannot open COM port COM4: could not open port 'COM4': FileNotFoundError(2, ...)

The port was there again seconds later, and starting the program manually worked.

The same single-attempt logic is used for reconnection at runtime: WriteLine() closes and reopens the port on SerialException, through this same function.

Changes

  • openSerial() retries (10 attempts, 1 second apart) before giving up. Failed attempts are logged as warnings; giving up still logs an error and exits as before.
  • self.com_port keeps the configured value ("AUTO" or a port name) instead of being overwritten by the detected port. With AUTO, the port is therefore detected again at every attempt — which matters here, because the screen may come back on a different port than the one detected a moment earlier. Nothing else in the codebase reads self.com_port.

Testing

  • Simulated failures with a stubbed serial.Serial: opening succeeds after 3 failed attempts, and when every attempt fails the program still exits cleanly with the error message instead of hanging.
  • Real hardware (Turing 5" rev C, Windows 11, Python 3.13): normal startup is unaffected.

🤖 Generated with Claude Code

openSerial() gave up and stopped the whole program as soon as the port could
not be opened. The program resets the screen at startup, which disconnects and
reconnects the device: its COM port is gone for a few seconds and can come back
under a different name, so an autostart at boot can lose that race and leave the
screen frozen until the user starts the program again:

    [DEBUG] Detected screen turned ON
    [DEBUG] Auto detected COM port: COM4
    [ERROR] Cannot open COM port COM4: could not open port 'COM4': FileNotFoundError(2, ...)

Retry for a few seconds before giving up. Also keep self.com_port as configured
instead of overwriting it with the detected port, so that with COM_PORT: AUTO
the port is detected again at every attempt: the screen may come back on another
port than the one found before. This applies to the reconnection in WriteLine()
too, which reopens the port through the same function.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant