""""avrdude: ser_open(): can't open device "\\.\COM5": Access is denied.""""

Trying to flash a new Marlin build like I’ve done before (on a different PC though), but its showing the error in the title. Brand new VS Code download (with Platform IO and Auto Marlin Build auto installed), but I can’t upload the new edited, firmware to my Arduino Mega 2560 with a Ramps 1.4 Board.

This is the full terminal output:

 *  Executing task: C:\Users\Caleb\.platformio\penv\Scripts\platformio.exe run --target upload 

Warning! Ignore unknown configuration option `monitor_flags` in section [env]
Warning! `src_filter` configuration option in section [env:include_tree] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Warning! `src_filter` configuration option in section [env:DUE] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Warning! `src_filter` configuration option in section [env:esp32] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Warning! `src_build_flags` configuration option in section [env:linux_native] is deprecated and will be removed in the next release! Please use `build_src_flags` instead
Warning! `src_filter` configuration option in section [env:linux_native] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Warning! `src_build_flags` configuration option in section [env:simulator_windows] is deprecated and will be removed in the next release! Please use `build_src_flags` instead
Warning! `src_filter` configuration option in section [env:SAMD51_grandcentral_m4] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Warning! `src_filter` configuration option in section [env:malyan_M300] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Warning! `src_filter` configuration option in section [env:STM32F103CB_malyan] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Warning! `src_filter` configuration option in section [env:teensy31] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Warning! `src_filter` configuration option in section [env:teensy35] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Warning! `src_filter` configuration option in section [env:teensy36] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Warning! `src_filter` configuration option in section [env:teensy41] is deprecated and will be removed in the next release! Please use `build_src_filter` instead
Processing mega2560 (board: megaatmega2560; platform: atmelavr@~3.4; framework: arduino)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/megaatmega2560.html
PLATFORM: Atmel AVR (3.4.0) > Arduino Mega or Mega 2560 ATmega2560 (Mega 2560)
HARDWARE: ATMEGA2560 16MHz, 8KB RAM, 248KB Flash
DEBUG: Current (avr-stub) On-board (avr-stub, simavr)
PACKAGES:
 - framework-arduino-avr @ 5.1.0
 - tool-avrdude @ 1.60300.200527 (6.3.0)
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
Converting Marlin.ino
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 6 compatible libraries
Scanning dependencies...
Dependency Graph
|-- U8glib-HAL @ 0.5.1
|   |-- Wire @ 1.0
|   |-- SPI @ 1.0
|-- Wire @ 1.0
|-- SPI @ 1.0
Building in release mode
Compiling .pio\build\mega2560\src\src\inc\Warnings.cpp.o
Linking .pio\build\mega2560\firmware.elf
Checking size .pio\build\mega2560\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=====     ]  54.0% (used 4425 bytes from 8192 bytes)
Flash: [=====     ]  45.0% (used 114294 bytes from 253952 bytes)
Configuring upload protocol...
AVAILABLE: wiring
CURRENT: upload_protocol = wiring
Looking for upload port...
Auto-detected: COM5
Uploading .pio\build\mega2560\firmware.hex
avrdude: ser_open(): can't open device "\\.\COM5": Access is denied.


avrdude: ser_drain(): read error: The handle is invalid.


avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.


avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.


avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.


avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.


avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.


avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.


avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

avrdude done.  Thank you.

*** [upload] Error 1
================================================================================================================================ [FAILED] Took 45.10 seconds ================================================================================================================================
Environment    Status    Duration
-------------  --------  ------------
mega2560       FAILED    00:00:45.097
=========================================================================================================================== 1 failed, 0 succeeded in 00:00:45.097 ===========================================================================================================================
 *  The terminal process "C:\Users\Caleb\.platformio\penv\Scripts\platformio.exe 'run', '--target', 'upload'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it.

When the printer is unplugged, the PlatformIO Home Tab just shows COM1 under devices. When I plug in the printer, my PC makes a sound…but refreshing the device list in the PlatformIO Home Tab shows no new com port…like it doesn’t even see my Arduino Mega 2560. Why? I can’t see a driver I’m missing online.

Edit: Nevermind, the refresh button doesn’t actually do anything. COM5 does in fact come up. BUT WHY IS IT ACCESS DENIED for VS Code? I even tried launching VS Code with admin priveledges.

Edit 2: I don’t think a missing driver is the issue as Device Manager shows that COM5 is in fact an Arduin Mega 2560.

AAAAND I fixed the problem myself. If anyone has this issue in the future, here was my issue: some unknown serial monitor was for some reason using that COM port (5 in my case) immediately. No idea why. To fix it, I went to Device Manager, found my Arduino under Port > COM5 then Right-Click > Properties > Advanced Settings. From here, I manually changed the COM Port from 5 to 100 (you can pick any number lol).

I don’t know how to figure out what was using my COM5 port as soon as it was plugged it.

well i tried exactly what you did but it still wont work, any other ideas

Adding to this solution.

In my case it was a multi step process.
I was trying to debug an ELEGOO ARDUINO UNO R3 following the avr-stub on my windows machine.

I encountered this issue first and then this other issue right away.

My solution was to change the Arduino COM port from 5 to 100 like explained on this post.
And then, in the platformio.ini file, I had to configure the debug port like this debug_port = \\.\COM100.

Then closed all open PlatformIO windows (and serial monitors) ; un-plugged + re-plugged Arduino into USB and tried to debug.
Worked like a charm.