Serial.begin hangs when not connected to computer

It appears that Serial.begin() does not return if the Arduino is not connected to another serial device such as the USB port on a computer. Is this expected behavior?

For my application the Arduino will collect data on battery power then be connected to a computer USB port to dump the collected data. Are there any functions or examples to test if the Arduino is connected to a computer?

Thanks for taking the time to read this.

Which Arduino? Can you post your code please?

How to use this forum

Read this before posting a programming question

Upon further observation it appears Serial.begin does not hang when not connected to a computer USB port.

I'm still interested if the Arduino can detect if the serial monitor is running or not. I suppose I could use a serial command to initiate a data dump but I would prefer for the dump to begin automatically when the Serial Monitor is started on the computer.

Hi Nick. Thanks for getting back to me. I have already read the links you provided. I felt this question was not specific to my code so didn't include it. If there is another area where these types of questions are more appropriate please let me know.

In case it helps with my follow on question I'm using an Arduino Uno v1 with 1.0.5 ide.

I'm still interested if the Arduino can detect if the serial monitor is running or not.

No it can't, not with just software.

You could wire a link between the DTR pin of the serial / USB converter to an input pin and look at that. That is the pin that goes through a cap to reset the Arduino when the com port is opened.

Hi GM, thanks for the comments. I'm still fiddling with this and discovered the Serial Monitor appears to reset the Arduino when it starts. Not really the behavior I was looking for. I'm starting to think my original premise is not possible, data collection on battery and serial monitor to dump data since starting the Serial Monitor resets the Arduino. Back to the drawing board...

You can stop the serial monitor from resetting the arduino by disabling the auto reset circuit. People do this in two ways.

  1. By placing a large capacitor across it.
  2. By disconnecting the reset capacitor from the pin. There is a track break point on the PCB you can cur and remake with a blob of solder.

I use method 2 and I have fitted a sub miniature slide switch over the break point on the PCB on my Uno. The reset circuit is needed for uploading code unless you get fancy with the use of the reset button and drive that by hand during the upload process.

Thanks GM! That is a great solution! My soldering skills have atrophied over the years but this should be doable with the big magnifying glass. I've worked with the duemilanove and am familiar with using the reset during sketch uploads.

  1. put a 100 ohm resistor between the arduino 5v pin and the reset pin.

Note that the arduino IDE code upload won't work with the reset defeated. I suggest no permanent defeat of the reset function.

If you don't mind using a different serial monitor than the simple-minded arduino serial monitor, I recommend this terminal program:

You can use this program to connect and disconnect and then reconnect to arduino without triggering arduino reset, unless you push the green DTR button to reset arduino any time you want. There is really no need to defeat the reset mechanism unless you are using arduino serial monitor again.

These terminal programs also understand ANSI escape code for cursor control so you can display numbers in place instead of letting 20 lines scroll up your monitor in each second.

LinhartR:
Hi Nick. Thanks for getting back to me. I have already read the links you provided. I felt this question was not specific to my code so didn't include it. If there is another area where these types of questions are more appropriate please let me know.

If you had a Leonardo it may well have been relevant to see your code.

liudr:
If you don't mind using a different serial monitor than the simple-minded arduino serial monitor, I recommend this terminal program:

Terminal

You can use this program to connect and disconnect and then reconnect to arduino without triggering arduino reset, unless you push the green DTR button to reset arduino any time you want. There is really no need to defeat the reset mechanism unless you are using arduino serial monitor again.

These terminal programs also understand ANSI escape code for cursor control so you can display numbers in place instead of letting 20 lines scroll up your monitor in each second.

Heck no I don't mind. I will likely continue to use Serial Monitor for debugging but I will use this terminal program to communicate with the Arduino for data dumps. Thanks!

zoomkat:
3) put a 100 ohm resistor between the arduino 5v pin and the reset pin.

Note that the arduino IDE code upload won't work with the reset defeated. I suggest no permanent defeat of the reset function.

Thanks ZK, while trying to find the trace to cut as GM suggested I found this thread please add a jumper to disable auto reset - Bugs & Suggestions - Arduino Forum where there is quite a bit of discussion about disabling the auto-reset.

Just remember to hit disconnect on the terminal program before uploading sketch. Otherwise arduino ide can't access the port while it's opened by the terminal program. A nice feature of the program is you can save a bunch of texts and send them to serial port by pushing buttons. Say if you want to send some commands to arduino in the form of pin=3,state=H\n, you just save it in one of the buttons and push it to send the command quickly. For values you can't type with a keyboard such as say ASCII=7, you can't send it via arduino ide but you can send via this terminal program and save the message with a button :slight_smile:

Sorry if I've missed something obvious, also sorry I'm reviving an ancient thread, but I think I've read this whole thread and I can't seem to find any actual resolution. I have the problem of using a DHT-11 and an LCD/touchscreen shield, wanting the board to display temperature and humidity in standalone powered mode and store it to an SD card (all of that code works fine) and then if I unplug it from the 9V power and plug it into a USB socket, I want it to just write the data to the serial monitor as well as storing it. And THAT part of the code works just fine, too, with the following issue:

If I comment the

Serial.begin(9600); // (or whatever)

out in the setup section, the project runs fine no matter what it's plugged into, any Serial.print() seem to fail silently without halting my program, but now it can't communicate over the USB connection, which is sort of what you expect of a port that hasn't been initialised.

If I leave the serial initialisation in the setup section, the program will run fine while the project is plugged into a laptop, but it will hang around doing nothing when I power it from a USB power only source (like a wall wart) or a 9V battery, and this is unexpected.

Is there actually a solution that will allow a project to do both? A way to detect if there's a valid USB port and skip the Serial.begin if there isn't one?

What board do you have? What is your code? It is perfectly normal to do a Serial.begin in a project that is not connected to the USB. It might make a difference on the Leonardo though.