Correct. The loop goes through all devices and returns the last one. Unless there’s a direct match by hardware ID (VID, PID). See core logic at
You can set the upload_port to a specific COM port or use a wildcard expression. If PIO grabs the wrong upload (or monitor_port), you can thus correct it.
There’s also the possibility of programmatically setting the upload port / $UPLOAD_PORT via a script. See docs. For that you of course also have to have a way to always tell to which board you’re uploading. You may e.g. want to match the USB serial number of the chip to that one USB serial converter chip you’re working with, or analyze the USB hubs tree structure / location path (e.g., target upload port is always connected to first port of USB hub?) to find the correct one and then env.Replace(UPLOAD_PORT = <correct port>) that result.