how to arduino serial communication open without restart

Explain briefly, in the next code,

void setup() {
Serial.begin(9600);
Serial.println("setup start");
}
void loop(){
Serial.println("loop start");
delay(10000);
}

connect an arduino and PC through using USB. Then turn on the arduino sketch program and I can see the serial monitor printing the message "setup start" and "loop start".
I guess the “setup()” method is working whenever I open the serial communication.
What I want is to run only the “setup()” method when arduino’s first power is on and after that, whenever opening the serial communication I want only the “loop()” method to be run.
I searched everywhere through Googling but there is only way through hardware.(not for software) what I want is the software way, but If there is only way through hardware please tell me how. Thank you very much!

while(!Serial){}

will pause your program until you open the serial monitor

arduino_new:

while(!Serial){}

will pause your program until you open the serial monitor

Thank you for your answer.
However, it still starts with the setup function.

SRshow:
Thank you for your answer.
However, it still starts with the setup function.

Isn't that what you wanted?

Ah so. What you don't want is for the Arduino to reset every time you open the console program. The Arduino is configured to reset on startup to enable a quick check to see if there is new code waiting to be uploaded. This is accomplished by the USB hardware monitoring DTR which is supplied by the monitor application. By default, the serial monitor within the Arduino IDE is configured with DTR on so every time you open this it will cause the unit to reset. You can get around this with a hardware fix that will disable the reset function. This also disables your ability to upload new sketches without having a hardware bypass circuit controlled say by a push button switch. The other solution is to use a terminal program (almost any will do) that allows you to switch DTR off, then you can connect/disconnect without triggering the unit to reset.

2 Likes

arduino_new:

while(!Serial){}

will pause your program until you open the serial monitor

That only has effect on Arduinos with native USB support (e.g. Leonardo and Micro). It's useless on e.g. an Uno.

Yes. Try it. I use Terminal by Br@y.

The simplest way to achieve what you want is to connect to the Arduino using a USB-TTL cable that is connected to Rx Tx and GND.

Just use the regular USB cable when you want to upload code.

...R

1 Like

Witsie74:
I'm very interested to know how that works, since DTR in RS-232 is (was?) a hardware pin, and doesn't exist in the 2x data wire USB world. Is it mimicked in software?

Just like rx and tx are mimicked :wink:

DTR ... doesn't exist in the 2x data wire USB world.

Just like rx and tx are mimicked ;)

Which is to point out that the two data wires of USB are NOT REMOTELY like rx/tx in the rs232 world, and a USB/Serial chip does a LOT of processing to make it seem like there is a serial port.

DKWatson:
Yes. Try it. I use Terminal by Br@y.

Failed to resolve yet.
Is there a way to edit and fix the '. cpp ' or '. h ' document in the [arduino-core] directory?

SRshow:
Failed to resolve yet.

Did you read Reply #8?

...R

Well, I just did (again) exactly as I suggested you try and it works perfectly. If your's is not, then there's something else wrong.

The arduino resets when you open a serial connection to it because of the "auto-reset" feature.
There are several ways to disable auto-reset:

  • Connect a relatively large (10uF) capacitor between the RESET and GROUND on the power header of the board.
  • Cut the RESET-EN solder jumper intended for this purpose (not all clones will have this jumper)
  • remove the auto-reset capacitor in between DTR (from the USB/Serial chip) and the RESET signal.

All of these will interfere with the automatic upload of new sketches; you'll need to either undo them, or manage to use the manual reset button at the right time during upload (you have about a 2s window)

connect to the Arduino using a USB-TTL cable that is connected to Rx Tx and GND

Now THAT'S a pretty neat idea, now that USB/TTL cables are less expensive than they used to be...

1 Like