Intermittent Arduino Mega2560 problem: "COM5 already in use" - SOLVED

Hi guys,

I run a small company making props for magicians and we've recently shifted our development platform from raw ATMega chips to the Arduino Mega2560. Debugging complex real time software is a dream thanks to the serial output.

Everything was going well up until last night when I kept getting "COM5 already in use" errors when uploading the sketch to the board over USB. Unless there's been an update in the last month, I'm using the very latest software on Windows 7 with all the O/S patches automatically installed and no other USB devices attached - not even a printer. The system management console confirms this.

Most of the time, I can shut down the Arduino environment, pull the USB cable out, plug it back in and re-run the Arduino environment to clear the problem. Sometimes it goes for just one more upload before throwing the error again, sometimes it's several uploads. This is quickly becoming a royal pain in the a**e and points to something in the toolchain not releasing the port after previously uploading a sketch. I'm not using "!!!" anywhere in my code.

After a VERY frustrating hour going around in circles searching online for a solution, all I have found is plenty of others with the same problem. So my question is simple: has anyone solved this problem and if so where is the solution to be found?

Thanks.

I have the same setup and that problem happens to me sometimes. In my case, it seems to have something to do with the console as it is more likely to happen when I just had the console open and then press the upload sketch button without explicitly closing the console first. Sometimes, after I close the console and try to upload the sketch immediately after, I still get the message COMX already in use. Pressing the load sketch one more time then works for me.

Rui

tecmac:
I have the same setup and that problem happens to me sometimes. In my case, it seems to have something to do with the console as it is more likely to happen when I just had the console open and then press the upload sketch button without explicitly closing the console first. Sometimes, after I close the console and try to upload the sketch immediately after, I still get the message COMX already in use. Pressing the load sketch one more time then works for me.

Rui

To test the hypothesis that it may be the sketch already running and using the serial line, I've started removing the USB cable and replacing it before each upload. So far, it's solving the problem.

It seems that I've found a workaround - not a complete solution.

I'm streaming diagnostics data off the Arduino Mega2560 board back to PuTTY the PC (serial monitor won't obey escape sequences). I'm doing rapid application development here, which means writing a small functional unit of code, compiling and testing, writing another piece of code, compiling and testing again, and so on.

If, after checking the data from the board, I kill PuTTY then pull the USB cable out until the "ba dump" sound is heard, and then plug it back in, when I come to subsequently compile and upload all is well. If I don't pull the USB cable out and plug it back in again, I get a "COM5 already in use" error. It seems that the board is keeping a handle on the port by sending data to it. Resetting the USB port seems to release the board's grip and makes COM5 available again for uploading. This workaround was still working yesterday evening after what must have been several dozen uploads.

The question is, however, why did this suddenly start happening after several weeks of perfect operation, during which I've been streaming data off the board? We have several more boards on order. Will this be a recurrent problem, or does it only happen with some hardware?

The question is, however, why did this suddenly start happening after several weeks of perfect operation, during which I've been streaming data off the board? We have several more boards on order. Will this be a recurrent problem, or does it only happen with some hardware?

I really think the root problem is somewhere in either your Putty application or in your PC USB driver, or something else software related in the PC. I use a serial terminal program a lot (Brey terminal) and often leave it opened on the desktop as well as having the arduino IDE open. The IDE is well behaved in that after a upload it releases the comm port automatically. Brey terminal has a connect/disconnect button that also allows releasing of the comm port manualy. So I have no problem going back and forth between uploading sketches and monitoring the serial data on brey terminal, as long as I remember to hit the disconnect button on brey terminal before trying to upload from the arduino IDE. I have never had to unplug the arduino board from the USB.

I use windows XP SP3 and a wide variety of arduino boards.

Lefty

I have much more data on this now now. Running Arduino with no serial output was fine all yesterday afternoon and evening, and when COM5 is refused, pulling the plug frees it without having to exit the IDE. The software I'm porting is a small event-driven operating system; one of the system tasks can be set to just run a few times then force a halt to the virtual machine. Halting closes the serial line. If I allow that to happen, everything is also cool, but to a lesser extent. If I don't close the serial line before uploading, or let the Arduino run normally and send data all the time, I have to pull the USB plug before uploading. An inelegant solution might be to wrap all Serial.print() and Serial.println() blocks with calls to Serial.begin() and Serial.end().

Further to this, it seems that you can avoid the problem altogether. Rather than opening the serial line in setup and leaving it open, open it only when required, write to it, then close it again.