arduino / arduino-ide

Arduino IDE 2.x
https://www.arduino.cc/en/software
GNU Affero General Public License v3.0
2.08k stars 353 forks source link

"Access is denied" error when uploading to board w/ FT232 USB chip with Serial Monitor/Plotter open #1783

Open maciejmiklas opened 1 year ago

maciejmiklas commented 1 year ago

Describe the problem

In order to upload to an Arduino board via a serial port, the upload tool (e.g., AVRDUDE) must be able to open the port. This is not possible if another process already has control of the port.

Arduino IDE's "Serial Monitor" and and "Serial Plotter" components also use the serial port of the board. Arduino IDE handles this potential conflict of port usage automagically by closing the port in Serial Monitor/Plotter when an upload process is triggered, then opening it again after the upload is finished. This means no special action is needed from the user to upload while Serial Monitor/Plotter is open.

🐛 The upload fails with an "Access is denied" error under the following conditions:

I've been using Arduino IDE 2.0.3 with Nano without issues when uploading on Ubuntu. Now I've tried it on Windows 11, and most of the time, it gives me this error:

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\macie\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"

         Using Port                    : COM5
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
avrdude: ser_open(): can't open device "\\.\COM5": Access is denied.

But I can see logs from the serial monitor in Arduino IDE -> serial monitor.

To reproduce

Equipment

A board or other device that has an FTDI FT232R USB chip

The classic Arduino Nano is a common board that uses this chip.

FT232R modules (example) or cables (example) are also often used to upload to boards such as the Arduino Pro Mini that don't have an integrated USB chip.

The fault can be reproduced even if the FT232R-based device is not an Arduino board. In this case, the upload is expected to fail, but it would fail with a "not in sync" error instead of "Access is denied".

Steps

  1. Select File > New Sketch from the Arduino IDE menus.
  2. Connect the Arduino board to your computer.
  3. Select the board and port from the Arduino IDE menus.
  4. If the Serial Monitor view is not already open, select Tools > Serial Monitor from the Arduino IDE menus to open it.
  5. Select Sketch > Upload from the Arduino IDE menus.
  6. Wait for the upload process to finish. 🐛 The upload fails:

    avrdude: Version 6.3-20190619
            Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
            Copyright (c) 2007-2014 Joerg Wunsch
    
            System wide configuration file is "C:\Users\macie\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"
    
            Using Port                    : COM5
            Using Programmer              : arduino
            Overriding Baud Rate          : 115200
    avrdude: ser_open(): can't open device "\\.\COM5": Access is denied.
  7. Click the X icon on the "Serial Monitor" view tab in the bottom panel.
  8. Select Sketch > Upload from the Arduino IDE menus.
  9. Wait for the upload process to finish. 🙂 The upload is successful.
  10. Select Tools > Serial Plotter from the Arduino IDE menus.
  11. Select Sketch > Upload from the Arduino IDE menus.
  12. Wait for the upload process to finish. 🐛 The upload fails:

    avrdude: Version 6.3-20190619
            Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
            Copyright (c) 2007-2014 Joerg Wunsch
    
            System wide configuration file is "C:\Users\macie\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"
    
            Using Port                    : COM5
            Using Programmer              : arduino
            Overriding Baud Rate          : 115200
    avrdude: ser_open(): can't open device "\\.\COM5": Access is denied.
  13. Close the "Serial Plotter" window.
  14. Select Sketch > Upload from the Arduino IDE menus.
  15. Wait for the upload process to finish. 🐛 The upload fails:

    avrdude: Version 6.3-20190619
            Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
            Copyright (c) 2007-2014 Joerg Wunsch
    
            System wide configuration file is "C:\Users\macie\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"
    
            Using Port                    : COM5
            Using Programmer              : arduino
            Overriding Baud Rate          : 115200
    avrdude: ser_open(): can't open device "\\.\COM5": Access is denied.

    This unexpected result is caused by a separate bug: https://github.com/arduino/arduino-ide/issues/1423

Expected behavior

Serial Monitor and Serial Plotter do not interfere with upload process.

Arduino IDE version

Original report

2.0.3

Last verified with

aa9b10d

Operating system

Windows

Operating system version

Edition Windows 11 Pro Version 22H2 Installed on 07/11/2022 OS build 22621.963 Experience Windows Feature Experience Pack 1000.22638.1000.0

Additional context

I bisected the introduction of the fault to df8658e


Logs generated from an upload that fails with this fault using the build for 053574b with the arduino.cli.daemon.debug advanced setting set to true:

2023-08-16_log.log


I have no problems uploading sketches on Arduino IDE 1.8.19 from Microsoft Store on the same system. I can quit 2.0.3, start 1.8.19, and it's working immediately.


I cannot reproduce the fault on Linux (Ubuntu 22.04) or macOS (Ventura).


I cannot reproduce the fault when using native USB boards (e.g., Leonardo, MKR) or boards that use other models of dedicated USB chip (e.g., ATmega16U2 on Uno and Mega, WCH CH340 on derivative boards).

Workaround

Serial Monitor
  1. Click the X icon on the "Serial Monitor" tab in the bottom panel of the Arduino IDE window to close the tab: image
  2. Upload the sketch.
  3. Open Serial Monitor again if needed (by clicking the icon on the IDE toolbar or the Tools > Serial Monitor menu item) after the upload is finished.

Serial Plotter
  1. Close the "Serial Plotter" window.
  2. If the "Serial Monitor" view is not already open, select Tools > Serial Monitor from the Arduino IDE menus to open it. We must do an open/close cycle on Serial Monitor as a workaround for a bug in Serial Plotter
  3. Click the X icon on the "Serial Monitor" tab in the bottom panel of the Arduino IDE window to close the tab: image
  4. Upload the sketch.
  5. Open Serial Monitor and/or Serial Plotter again if needed after the upload is finished.

Additional reports

Related

Issue checklist

Wezz19 commented 1 year ago

Not sure if it's actuality related to your problem, but I got this after I had changed the COM port in device manager. After rebooting the computer I could use the COM port normally again.

Is this still happening for you?

maciejmiklas commented 1 year ago

Yes, I can work for several hours on 1.8 without a single issue, and this problem occurs immediately after switching to 2.0

per1234 commented 1 year ago

Hi @maciejmiklas. Thanks for your report.

Does the error still occur if you close the Serial Monitor tab in the bottom panel of the Arduino IDE window before uploading?

You can do that by clicking the X icon on the tab:

image

Arduino IDE automatically closes the port in Serial Monitor and Serial Plotter when uploading, so this should not be necessary. I'm only trying to determine the exact conditions the fault occurs under.

maciejmiklas commented 1 year ago

I did that and have had no problems so far.

per1234 commented 1 year ago

Thanks for checking @maciejmiklas.

I am able to reproduce this fault, but only under specific conditions (FT232 chip, Windows, using Serial Monitor/Plotter) and I wanted to make sure the fault you are experiencing also depends on those conditions.

Knowing the required conditions and being able to reproduce it should make the preparation and validation of a fix for the bug much easier.

maciejmiklas commented 1 year ago

I do not have this problem with the original UNO, only when using a custom board with USB->Serial adapter:

IMG_3680

ananzjo commented 1 year ago

Still Have the Same Problem Although Serial Monitor & Serial Plotter are both closed , this is very frustrating

any advice please

FQBN: arduino:avr:nano
Using board 'nano' from platform in folder: C:\Users\ananz.CAD\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
Using core 'arduino' from platform in folder: C:\Users\ananz.CAD\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6

Detecting libraries used...
"C:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\eightanaloginputs" "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D\\sketch\\frank-Zaid.ino.cpp" -o nul
Generating function prototypes...
"C:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\eightanaloginputs" "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D\\sketch\\frank-Zaid.ino.cpp" -o "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D\\preproc\\ctags_target_for_gcc_minus_e.cpp"
"C:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\builtin\\tools\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D\\preproc\\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\eightanaloginputs" "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D\\sketch\\frank-Zaid.ino.cpp" -o "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D\\sketch\\frank-Zaid.ino.cpp.o"
Compiling libraries...
Compiling core...
Using precompiled core: C:\Users\ananz.CAD\AppData\Local\Temp\arduino\cores\arduino_avr_nano_cpu_atmega328_ff382133097821e9b3511dee9c511050\core.a
Linking everything together...
"C:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-gcc" -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D/frank-Zaid.ino.elf" "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D\\sketch\\frank-Zaid.ino.cpp.o" "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D/..\\..\\cores\\arduino_avr_nano_cpu_atmega328_ff382133097821e9b3511dee9c511050\\core.a" "-LC:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D" -lm
"C:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D/frank-Zaid.ino.elf" "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D/frank-Zaid.ino.eep"
"C:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy" -O ihex -R .eeprom "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D/frank-Zaid.ino.elf" "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D/frank-Zaid.ino.hex"

"C:\\Users\\ananz.CAD\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-size" -A "C:\\Users\\ananz.CAD\\AppData\\Local\\Temp\\arduino\\sketches\\51312D1A002CB6593C184EABDDDB0A2D/frank-Zaid.ino.elf"
Sketch uses 3418 bytes (11%) of program storage space. Maximum is 30720 bytes.
Global variables use 202 bytes (9%) of dynamic memory, leaving 1846 bytes for local variables. Maximum is 2048 bytes.
"C:\Users\ananz.CAD\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\ananz.CAD\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v -V -patmega328p -carduino "-PCOM5" -b115200 -D "-Uflash:w:C:\Users\ananz.CAD\AppData\Local\Temp\arduino\sketches\51312D1A002CB6593C184EABDDDB0A2D/frank-Zaid.ino.hex:i"

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\ananz.CAD\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"

         Using Port                    : COM5
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
avrdude: ser_open(): can't open device "\\.\COM5": Access is denied.

avrdude done.  Thank you.

Failed uploading: uploading error: exit status 1
per1234 commented 1 year ago

Hi @ananzjo

any advice please

This is not an appropriate place to request advice. This issue is only used by the developers to track the bug. If you would like assistance with fixing the upload error (which has multiple potential causes other than the one tracked here), please post on Arduino Forum:

https://forum.arduino.cc/

I'm sure we'll be able to help you out over there.

jenschr commented 11 months ago

I just spent an evening helping a non-technical person (cosplayer) finding a solution to this problem. Closing the Serial Monitor or installing an older version of the IDE does the trick. This person was on Win 11 using Arduino 2.1.0 IDE with an original Arduino Nano. This is a very common setup, so this will affect many users.

It puzzles me a bit that this bug still has not been assigned to a milestone given that it was reported 6+ months ago. Are there problems reproducing it? Anything I can do to help resolve it?

kittaakos commented 9 months ago

I investigated, and the problem cannot be fixed without CLI changes/support.

IDE2 creates a gRPC bidirectional stream via the Monitor method.

BeforeIDE2 does an upload, all related monitor connections are closed. IDE2 closes the monitor connection by ending the bidirectional stream and waiting for the OK signal:

https://github.com/arduino/arduino-ide/blob/69ae38effaf950ae6fba83617b08b1cc0bc0a40d/arduino-ide-extension/src/node/monitor-service.ts#L435

I see a race condition; if I put a breakpoint into the code after closing the bidirectional stream (and let the OS clean up the resources), I do not have the access is denied error.

2023-08-18T14:19:15.694Z daemon INFO time="2023-08-18T16:19:15+02:00" level=info msg="sending command" command=CLOSE monitor=serial-monitor
2023-08-18T14:19:15.705Z daemon INFO time="2023-08-18T16:19:15+02:00" level=info msg="received message" error=false event_type=close message=OK monitor=serial-monitor
2023-08-18T14:19:15.707Z daemon INFO time="2023-08-18T16:19:15+02:00" level=info msg="sending command" command=QUIT monitor=serial-monitor
time="2023-08-18T16:19:15+02:00" level=info msg="received message" error=false event_type=quit message=OK monitor=serial-monitor
2023-08-18T14:19:15.710Z daemon INFO time="2023-08-18T16:19:15+02:00" level=info msg="Killing monitor process" monitor=serial-monitor
2023-08-18T14:19:15.711Z daemon INFO time="2023-08-18T16:19:15+02:00" level=error msg="Sent kill signal" error="TerminateProcess: Access is denied." monitor=serial-monitor
2023-08-18T14:19:15.719Z daemon INFO time="2023-08-18T16:19:15+02:00" level=error msg="stopped decode loop: EOF" monitor=serial-monitor
2023-08-18T14:19:15.736Z daemon INFO time="2023-08-18T16:19:15+02:00" level=info msg="Monitor process killed" monitor=serial-monitor

https://github.com/arduino/arduino-ide/assets/1405703/ef510025-ffd7-4368-af39-64c93e2d9305

CLI should provide a way to terminate the monitor connection gracefully. Calling this API and receiving an OK ack means the port is free.

This is what is happening:

This is what should happen:

This is just one of the possible solutions, but the main idea is that IDE2 or any other CLI client might need this way of terminating the monitor.

per1234 commented 9 months ago

Thanks for taking the time to make an investigation @kittaakos! I am sorry it wasn't so simple as I had hoped.

Since you are now most familiar with the problem and the work that will be required to resolve it, would you mind reporting this to the Arduino CLI developers by submitting an issue to their repo when you get the time?

If work is needed in both the Arduino IDE and Arduino CLI codebases, then I would suggest we keep this one here to track the Arduino IDE work and create a separate one in the Arduino CLI repo to track the work needed on that codebase.

ubidefeo commented 9 months ago

@kittaakos , I agree with @per1234

Please, report to the CLI team and let's do things right even if it shifts development a little. This is something that should be addressed in the next release :)

per1234 commented 3 months ago

Unfortunately it was not fixed by #2334. I am still able to reproduce the fault using aa9b10d.