PlatformIO not entering programming mode reliably

Status
Not open for further replies.
Sorry if this is more a question for PlatformIO but I've gotta start somewhere.

I've been successfully programing for a while with no touch of my custom Teensy 3.2 based hardware.

Recently I increased the amount of data that is sent from Teensy over SerialUSB to ~150bytes, 4 times a second. This seems to have caused entering the program mode to reliably fail:

platformio.exe run --target upload said:
Code:
Processing teensy31 (platform: teensy; board: teensy31; framework: arduino)
------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/teensy/teensy31.html
PLATFORM: Teensy (4.12.0) > Teensy 3.1 / 3.2
HARDWARE: MK20DX256 72MHz, 64KB RAM, 256KB Flash
DEBUG: Current (jlink) External (jlink)
PACKAGES:
 - framework-arduinoteensy 1.153.0 (1.53)
 - tool-teensy 1.152.200516 (1.52)
 - toolchain-gccarmnoneeabi 1.50401.190816 (5.4.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 93 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <AceRoutine> 1.3.1
|   |-- <AceCommon> 1.4.5
|-- <AceCommon> 1.4.5
|-- <i2c_t3>
Building in release mode
Linking .pio\build\teensy31\firmware.elf
Building .pio\build\teensy31\firmware.hex
Checking size .pio\build\teensy31\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]  10.9% (used 7176 bytes from 65536 bytes)
Flash: [==        ]  20.3% (used 53172 bytes from 262144 bytes)
Configuring upload protocol...
AVAILABLE: jlink, teensy-cli, teensy-gui
CURRENT: upload_protocol = teensy-gui
Uploading .pio\build\teensy31\firmware.hex
Unable to open COM3 for reboot request
  Windows Error Info: Access is denied.
  more ideas... https://forum.pjrc.com/threads/40632?p=126667&viewfull=1#post126667
Teensy did not respond to a USB-based request to enter program mode.
Please press the PROGRAM MODE BUTTON on your Teensy to upload your sketch.

After the first line "Uploading ..." is displayed, the Teensy loader app sits on this screen for ~6 seconds:

Teensy Loader.png

Log:

Teensy Loader Verbose Info said:
Code:
18:08:47.623 (post_compile 163): Begin, version=1.52, high-res time
18:08:47.624 (loader): remote connection 1452 opened
18:08:47.625 (loader): remote cmd from 1452: "comment: Teensyduino 1.52 - WINDOWS (teensy_post_compile)"
18:08:47.626 (post_compile 163): Sending command: comment: Teensyduino 1.52 - WINDOWS (teensy_post_compile)
18:08:47.626 (loader): remote cmd from 1452: "status"
18:08:47.627 (loader): file changed
18:08:47.642 (loader): File "firmware.hex". 53184 bytes, 20% used
18:08:47.649 (loader): remote cmd from 1452: "dir:C:\Users\camer\git\Firmware\.pio\build\teensy31\"
18:08:47.650 (post_compile 163): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:47.650 (post_compile 163): Sending command: dir:C:\Users\camer\git\Firmware\.pio\build\teensy31\
18:08:47.651 (post_compile 163): Sending command: file:firmware.hex
18:08:47.653 (loader): remote cmd from 1452: "file:firmware.hex"
18:08:47.668 (loader): File "firmware.hex". 53184 bytes, 20% used
18:08:47.674 (loader): remote cmd from 1452: "status"
18:08:47.678 (post_compile 163): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:47.678 (post_compile 163): Disconnect
18:08:47.688 (loader): remote connection 1452 closed
18:08:47.689 (post_compile 164): Running teensy_reboot: "C:\Users\camer\.platformio\packages\tool-teensy\teensy_reboot.exe" teensy_reboot.exe "-board=TEENSY31"
18:08:47.690 (loader): remote connection 1452 opened
18:08:47.714 (reboot 165): Begin, version=1.52, high-res time
18:08:47.714 (reboot 165): LoadLibrary cfgmgr32 ok
18:08:47.714 (reboot 165): LoadLibrary ntdll ok
18:08:47.716 (reboot 165): found_usb_device, id=\\?\usb#vid_16c0&pid_0483#4294967295#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
18:08:47.716 (reboot 165): found_usb_device, loc=usb:0/140000/0/1/3/3    Port_#0003.Hub_#0009
18:08:47.716 (reboot 165): found_usb_device, hwid=USB\VID_16C0&PID_0483&REV_0275
18:08:47.716 (reboot 165): found_usb_device, devinst=0000000a
18:08:47.716 (reboot 165): add: loc=usb:0/140000/0/1/3/3, class=Ports, vid=16C0, pid=0483, ver=0275, serial=4294967295, dev=\\?\usb#vid_16c0&pid_0483#4294967295#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
18:08:47.716 (reboot 165):   comport_from_devinst_list attempt
18:08:47.716 (reboot 165):   found Ports in classguid_list at index=0
18:08:47.716 (reboot 165):   port COM3 found from devnode
18:08:47.716 (reboot 165): found_usb_device complete
18:08:47.720 (loader): remote connection 1424 opened
18:08:47.722 (loader): remote cmd from 1424: "show:arduino_attempt_reboot"
18:08:47.723 (reboot 165): found Teensy Loader, version 1.52
18:08:47.723 (reboot 165): Sending command: show:arduino_attempt_reboot
18:08:47.724 (loader): got request to show arduino rebooting message
18:08:47.728 (loader): remote cmd from 1424: "comment: Teensyduino 1.52 - WINDOWS (teensy_reboot)"
18:08:47.729 (reboot 165): Sending command: comment: Teensyduino 1.52 - WINDOWS (teensy_reboot)
18:08:47.732 (loader): remote cmd from 1424: "status"
18:08:47.736 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:47.736 (reboot 165): do_reset (serial) COM3
18:08:47.737 (loader): remote cmd from 1424: "status"
18:08:47.741 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:47.741 (reboot 165): status read, retry 0
18:08:47.841 (loader): remote cmd from 1424: "status"
18:08:47.850 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:47.850 (reboot 165): status read, retry 1
18:08:47.951 (loader): remote cmd from 1424: "status"
18:08:47.957 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:47.957 (reboot 165): status read, retry 2
18:08:48.057 (loader): remote cmd from 1424: "status"
18:08:48.062 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:48.062 (reboot 165): status read, retry 3
18:08:48.163 (loader): remote cmd from 1424: "status"
18:08:48.167 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:48.167 (reboot 165): status read, retry 4
18:08:48.268 (loader): remote cmd from 1424: "status"
18:08:48.272 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:48.272 (reboot 165): status read, retry 5
18:08:48.373 (loader): remote cmd from 1424: "status"
18:08:48.376 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:48.376 (reboot 165): status read, retry 6
18:08:48.476 (loader): remote cmd from 1424: "status"
18:08:48.480 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:48.480 (reboot 165): status read, retry 7
18:08:48.580 (loader): remote cmd from 1424: "status"
18:08:48.584 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:48.584 (reboot 165): status read, retry 8
18:08:48.684 (loader): remote cmd from 1424: "status"
18:08:48.688 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:48.688 (reboot 165): status read, retry 9
18:08:48.789 (loader): remote cmd from 1424: "status"
18:08:48.793 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:48.793 (reboot 165): status read, retry 10
18:08:48.894 (loader): remote cmd from 1424: "status"
18:08:48.900 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:48.900 (reboot 165): status read, retry 11
18:08:49.001 (loader): remote cmd from 1424: "status"
18:08:49.005 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:49.005 (reboot 165): status read, retry 12
18:08:49.105 (loader): remote cmd from 1424: "status"
18:08:49.111 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:49.111 (reboot 165): status read, retry 13
18:08:49.212 (loader): remote cmd from 1424: "status"
18:08:49.220 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:49.220 (reboot 165): status read, retry 14
18:08:49.320 (loader): remote cmd from 1424: "status"
18:08:49.329 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:49.329 (reboot 165): status read, retry 15
18:08:49.430 (loader): remote cmd from 1424: "status"
18:08:49.439 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:49.439 (reboot 165): status read, retry 16
18:08:49.540 (loader): remote cmd from 1424: "status"
18:08:49.548 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:49.548 (reboot 165): status read, retry 17
18:08:49.649 (loader): remote cmd from 1424: "status"
18:08:49.658 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:49.658 (reboot 165): status read, retry 18
18:08:49.759 (loader): remote cmd from 1424: "status"
18:08:49.764 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:49.764 (reboot 165): status read, retry 19
18:08:49.864 (loader): remote cmd from 1424: "status"
18:08:49.870 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:49.870 (reboot 165): status read, retry 20
18:08:49.971 (loader): remote cmd from 1424: "status"
18:08:49.975 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:49.975 (reboot 165): status read, retry 21
18:08:50.076 (loader): remote cmd from 1424: "status"
18:08:50.084 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:50.084 (reboot 165): status read, retry 22
18:08:50.185 (loader): remote cmd from 1424: "status"
18:08:50.192 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:50.192 (reboot 165): status read, retry 23
18:08:50.293 (loader): remote cmd from 1424: "status"
18:08:50.300 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:50.300 (reboot 165): status read, retry 24
18:08:50.401 (loader): remote cmd from 1424: "status"
18:08:50.411 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:50.411 (reboot 165): status read, retry 25
18:08:50.511 (loader): remote cmd from 1424: "status"
18:08:50.524 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:50.524 (reboot 165): status read, retry 26
18:08:50.625 (loader): remote cmd from 1424: "status"
18:08:50.634 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:50.634 (reboot 165): status read, retry 27
18:08:50.735 (loader): remote cmd from 1424: "status"
18:08:50.741 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:50.741 (reboot 165): status read, retry 28
18:08:50.841 (loader): remote cmd from 1424: "status"
18:08:50.854 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:50.854 (reboot 165): status read, retry 29
18:08:50.955 (loader): remote cmd from 1424: "status"
18:08:50.961 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:50.961 (reboot 165): status read, retry 30
18:08:51.062 (loader): remote cmd from 1424: "status"
18:08:51.070 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:51.070 (reboot 165): status read, retry 31
18:08:51.171 (loader): remote cmd from 1424: "status"
18:08:51.177 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:51.177 (reboot 165): status read, retry 32
18:08:51.277 (loader): remote cmd from 1424: "status"
18:08:51.285 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:51.285 (reboot 165): status read, retry 33
18:08:51.385 (loader): remote cmd from 1424: "status"
18:08:51.392 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:51.392 (reboot 165): status read, retry 34
18:08:51.493 (loader): remote cmd from 1424: "status"
18:08:51.499 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:51.499 (reboot 165): status read, retry 35
18:08:51.600 (loader): remote cmd from 1424: "status"
18:08:51.609 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:51.609 (reboot 165): status read, retry 36
18:08:51.710 (loader): remote cmd from 1424: "status"
18:08:51.718 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:51.718 (reboot 165): status read, retry 37
18:08:51.819 (loader): remote cmd from 1424: "status"
18:08:51.827 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:51.827 (reboot 165): status read, retry 38
18:08:51.928 (loader): remote cmd from 1424: "status"
18:08:51.935 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:51.935 (reboot 165): status read, retry 39
18:08:52.036 (loader): remote cmd from 1424: "status"
18:08:52.045 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:52.045 (reboot 165): status read, retry 40
18:08:52.145 (loader): remote cmd from 1424: "status"
18:08:52.153 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:52.153 (reboot 165): status read, retry 41
18:08:52.254 (loader): remote cmd from 1424: "status"
18:08:52.260 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:52.260 (reboot 165): status read, retry 42
18:08:52.360 (loader): remote cmd from 1424: "status"
18:08:52.368 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:52.368 (reboot 165): status read, retry 43
18:08:52.469 (loader): remote cmd from 1424: "status"
18:08:52.475 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:52.475 (reboot 165): status read, retry 44
18:08:52.575 (loader): remote cmd from 1424: "status"
18:08:52.583 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:52.583 (reboot 165): status read, retry 45
18:08:52.684 (loader): remote cmd from 1424: "status"
18:08:52.689 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:52.689 (reboot 165): status read, retry 46
18:08:52.789 (loader): remote cmd from 1424: "status"
18:08:52.795 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:52.795 (reboot 165): status read, retry 47
18:08:52.895 (loader): remote cmd from 1424: "status"
18:08:52.903 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:52.903 (reboot 165): status read, retry 48
18:08:53.004 (loader): remote cmd from 1424: "status"
18:08:53.010 (reboot 165): Status: 1, 1, 0, 38, 0, 0, C:\Users\camer\git\Firmware\.pio\build\teensy31\, firmware.hex
18:08:53.010 (reboot 165): status read, retry 49
18:08:53.111 (reboot 165): Teensy did not respond to a USB-based request to automatically reboot.
18:08:53.125 (loader): remote connection 1424 closed
18:08:53.155 (loader): remote connection 1452 closed

Curiously, the log does not fill "linearly". The "(loader)" outputs appear on my screen one at a time, as the arrive, as confirmed by timestamps. Once it gives up retrying, the other lines appear and get interleaved.

If I slow down my status line prints, it uploads more reliably.

I start the upload while the serial monitor is open. I can see the serial monitor close. Curiously, after the programming "fails", PlatformIO reports "Success" and then restarts the serial monitor. However I can see clearly that my "sketch" has not restarted since the initialization outputs don't happen (and it waits for Serial, with 10 sec timeout, at start).
 
I see "version=1.52" in the verbose info log. Maybe give 1.54 a try?

Teensyduino 1.54 did fix a long-standing issue with use of volatile on the USB buffer descriptor table, possibly affecting all Teensy 3.x boards. Hard to say if that will make any difference, but I can at least confirm 1.54 has a bug fix which might matter. It's worth a try.
 
If you are using the platformio plugin in vscode and you have configured it to show PIO Home at startup, then you can check for updates by selecting Platforms / Updates which will update it on demand. PIO Home also checks periodically (weekly?) for platform and library updates and notifies you if anything is not up to date...
 
It was Windows the whole time

Thank you all for the replies. I tried updating to 1.54 but, unfortunately, that did not fix the problem.

Looking into it more, I noticed that if I ran the upload twice, it would pretty reliably work. That got me thinking, it could just be something weird with the serial port and Windows. We are getting an "Access Denied" error after all...

In searching for why PlatformIO wasn't using the GitHub version... I noticed a way to run some python before uploading.

I put in a 1 second delay. No joy. 2 seconds. No luck. Let's go for the moon, 10 seconds. WHAT? Works!

Turns out even though the serial monitor was closed automatically by the PlatformIO VS Code extension, it was still "using" the port for like 3 seconds!

I expanded on the script to explore some more. Turns out larger builds would take long enough to give the COM port enough time to close. Some short builds, basically with the intent of sending a software reset, would reliably be too short. I've gotten it to take over 3 seconds in certain situations to have the old COM port be "let go".

Anyway, the next question is, is this worth dealing with in Teensy Loader? Maybe an "Access Denied" error is only fatal after a ~10 second timeout? Or should this be filed as a PlatformIO bug?

Oh, and before I forget:

extra_script.py
Code:
Import("env")
from time import sleep, monotonic
import os
from serial import Serial
import configparser

config = configparser.ConfigParser()
config.read(env["PROJECT_CONFIG"])

port = config["env:" + env["PIOENV"]]["monitor_port"]

def before_upload(source, target, env):
    # Maybe this is just a Windows thing?
    if os.name != "nt":
        return

    print(f"Trying to open serial port, {port}, repeatedly, until we can.")

    tries = 0
    start = monotonic()

    while True:
        try:
            with Serial(port, 9600):
                break
        except:
            tries += 1
            # Be nice
            sleep(0.01)

    duration = monotonic() - start

    print(f"It took {tries} tries, in {duration:.2f}s, for {port} to become available.")


env.AddPreAction("upload", before_upload)

And in platformio.ini:

Code:
extra_scripts =
  extra_script.py
 
Last edited:
Updated version of the wait script for PlatformIO. Handles certain errors more cleanly.

Code:
from typing import Any, TYPE_CHECKING

if TYPE_CHECKING:
    Import: Any = None
    env: Any = {}

Import("env")
from time import sleep, monotonic
import os
import configparser

config = configparser.ConfigParser()
config.read(env["PROJECT_CONFIG"])

port = config["env:" + env["PIOENV"]]["monitor_port"]


def before_upload(source, target, env):
    # Only tested on Windows
    if os.name != "nt":
        return

    print(f"Trying to open serial port, {port}, repeatedly, until we can.")

    tries = 0
    start = monotonic()

    while True:
        try:
            with open(port):
                break
        except IOError as e:
            # File not found
            if e.errno == 2:
                # print("Port does not exist.")
                break

            # Permission denied
            if e.errno == 13:
                # This is the error we're waiting for
                tries += 1
                # Be nice
                sleep(0.01)
                # Repeat until we can open it
                continue

            # Other exceptions we don't know about
            raise e

    if tries != 0:
        duration = monotonic() - start
        status = f"It took {tries} tries, in {duration:.2f}s, for {port} to become available."
        print(status)

    print("Good to go!")


env.AddPreAction("upload", before_upload)
 
Status
Not open for further replies.
Back
Top