PIO Upload error Cannot open port it does not exist

I keep getting an error message when I try to upload saying the com port does not exist. So no upload. Devices recognizes the port. The auto port find run as part of the upload finds the correct port. I have tried many things including multiple reboots.

I finally found that if I close all PIO tasks on the terminal window so it only shows powershell and then kill the power shell terminal (trash bin icon on the terminal) then use upload it opens a new terminal and will upload.

I post this mainly so others who have this issue can find it. Seems a bug in PIO it should be able to reset terminal so as to close anything using the port before it tries to upload. This is particularly true for PIO (serial monitor) windows in the terminal.

An open serial monitor may block the serial port for upload purposes. (https://github.com/platformio/platformio-vscode-ide/issues/3809)

Thanks again for your help. I think you are correct. The question is what and where. My approach above seems to work around the problem but I think PIO should kill any leftovers. It does not seem to do so.

I opened the githup link and added a comment with my suggestion.

Thanks again for your help

Makes sense. If monitor_port and upload_port are the same, uploading should have higher priority.

It’s possible also that you don’t have permission to read/write that tty.

In my case, the port was /dev/ttyACM0. Doing ls -l gets:
crw-rw---- 1 root dialout 166, 0 Aug 20 20:42 ttyACM0

I edited /etc/group to add my userid to the group dialout. I had to reboot the machine (logging out wasn’t enough).

I’m pleased to report that it works just fine. I can build and upload (the → icon) while also monitoring the serial output (Serial.println() calls). I don’t have to do anything special to the Terminal controls on the right of the terminal. No garbage can or anything like that.

Visual Studio Code version 1.81.1 on Ubuntu Cinnamon 22.04 LTS

My platformio.ini for target M5Stack Core2:
[env]
platform = espressif32
framework = arduino
monitor_speed = 115200
lib_compat_mode = off

Note, this is officially documented in the PlatformIO docs (udev rules)

I had the same issue, but when scrolling up I saw the following warning which revealed the underlying problem:

Warning! Please install `99-platformio-udev.rules`. 
More details: https://docs.platformio.org/en/latest/core/installation/udev-rules.html

It comes down to needing permission to read the serial USB ports. This can be done by installing a udev rules file to your /etc/udev/rules.d directory (recommended), or by simply adding your user to the dialout group (strongly discouraged for security reasons).