"com port is denied" after 10-12 hours of working.

Hi everyone.

I made a windows app that I wrote on C#, it sends chars to arduino.
everything works fine for about 10-12 hours but than I get message "COM8 is denied".

every time app sends char, it opens the port sends it and than closes the port.

I cut the USB cable of arduino and made it longer (5-6m) so at first I thought that the long cable was problem but why does it works for 10 and more hours.
someone said that the reason is refrigerator in the building but why it always stuck after some hours.

after restarting windows is starts working fine again.
maybe the reason is bad driver for ports?

one time when I got this bug, I tried to disable the port from Device manager but windows get the message that it has to restart to disable it.

when I disconnect USB and connect it again, it starts working.

I'm working on this bug for 2 weeks and can't make it disappear.

if anyone had similar problem, please comment.

I'm using arduino to control IR Led_s to turn on/off TVs and Blink the LEDs before turning TV off.
arduino code works very good, the only problem is COM communication.

thanks you a lot.

PlayTime.ino (4.64 KB)

Why does your C# program close the com port?

If it is the only program using the com port then you can leave it open all the time. Closing it allows other process, including Windows update, to open it thus creating problems for you.

I tried using always opened port but bug appears again.

I'm using my program for 20 hours now and have no bug at my home, so I think that the problem is in place where I installed it.

A refrigerator starting or stopping its compressor can cause a spike on the circuit powering it - I recall another project here where this was rebooting the Arduino.

It might be worth seeing if you can move your computer to a different place or use a UPS. Having said that, I wouldn't expect the refrigerator to go so long without needing to cool.

Can you catch the error on the C# side and retry? If that fails, restart the program.

The other thing to consider is that sometimes the port that windows uses for the Arduino changes. After a failure, use the IDE to see which one you need to use to talk to the Arduino. Another reason to keep the port open.

Just guessing but it sounds like a windows problem to me. Try connecting it to another device and see if it stays open. I think when Windows enumerates the USB ports they will disconnect, but a lot depends on what the hardware is and the software version. Try turning off updates and the disconnecting the internet. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil