COM port remains open sporadically

Either using Arduino IDE or Visual Studio, I have the board connected, edit and upload the code several times and at some point I'll get the error

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

This is the same port I was connected to seconds ago and nothing I do will reset the port except OS restart.

Online resources leads to the same response: "another application is using the port". But there is no other application using the port, it is the same IDE that has just opened the port and didn't close it for some reason.

I would like to understand what is the cause of this and avoid it or have a quick fix without the need to restart the laptop, as this is a huge drawback for me.

Are you using Windows 10 by chance?
It sounds like another program is causing the issue.
You can disable each non-microsoft service one at a time, restart each time, and try to recreate the problem until it goes away, then you've found the problem.

Which board?

@er_name_not_found, I am on Win 10. Unfortunately, the proposed steps aren't feasible since I can't reproduce accurately the problem. The issue can arise at any moment, even after working properly for days.
I am inclined to say that it has more chances of happening after I detach and reattach the USB cable.

@sterretje UNO R3 and Nano. Both are cheap boards from China.

It's the best fix I can offer short of a full system reinstall.
It may take a while but if you keep a written log it is quite possible.
As it takes longer you can do a binary search by disabling half of the services and waiting for the problem. If it appears then the offending program is in the 1/2 not disabled. Continue this elimination and you will find it much faster.
I only have 30 or so non-microsoft services running so it would take maybe 6 times the normal few days it takes for the problem to reappear.

1 Like

Good idea! I'll give it a try

There is a tool called process explorer; it can find which process is locking a file. It might be able to find processes that lock the serial port. Though I would expect the error to be "Ports is busy" and not "Access denied".

You will have to download it, it's a microsoft program.

Is it possible that when reattached, the Arduino is being assigned to a different com port?

@westfw As I recall the port numbers stayed the same on all USB ports.

@sterretje There is a possible solution. In Process Explorer I can see avrdude doesn't close sometimes and this is when i get the error. If I kill the process, I can reuse the port.

So one step closer. At the point where I can't advise further; I did try to find how you can increase the verbosity (avrdud -v -v) in the IDE but could not find it :frowning: Maybe others have ideas where to go from here.

It might be relevant if we know which version of the IDE you are using and which version of avrdude?

Arduino IDE 1.8.16 and Visual Studio 2019 with Visual Micro.

I assume avrdude is 6.3.0 and the same for both IDEs.