platformio-ide.reopenSerialMonitorDelay doesn’t seem to have any effect, but this solution, together with delay(100) right after setting up the serial port in setup() (to ensure that no output is missed), works reliably enough to not feel too much like a hack:
platformio.ini:
extra_scripts = platformio.py
platformio.py:
Import("env")
# see https://github.com/platformio/platformio-core/issues/3742#issuecomment-1003454439
def wait_for_monitor_port(source, target, env):
port = env.GetProjectOption("monitor_port")
if port is None: return
print(f"Waiting for monitor port {port}...")
import serial
while True:
try:
serial.Serial(port)
break
except:
pass
env.AddPostAction("upload", wait_for_monitor_port)