Sometimes there is no problem to launch the serial monitor before or during compilation and loading. Other times it gives a list of avrdude: stk500v2_ReceiveMessage(): timeout errors. The reason I want to launch the serial monitor first is that if I wait until after the uploading, the program behaves slightly strangely at the beginning. For example, when it is supposed to print out "Hello, this is my first program.", the serial monitor shows something like "HelHello, this is my first program." What is the proper way to do it?
bbqq:
Sometimes there is no problem to launch the serial monitor before or during compilation and loading. Other times it gives a list of avrdude: stk500v2_ReceiveMessage(): timeout errors. The reason I want to launch the serial monitor first is that if I wait until after the uploading, the program behaves slightly strangely at the beginning. For example, when it is supposed to print out "Hello, this is my first program.", the serial monitor shows something like "HelHello, this is my first program." What is the proper way to do it?
You can usually open the serial monitor as soon as the board is connected, and before either compiling or uploading.
You shouldn't try to open it during the upload process.
You can also compile, upload and then open the serial monitor. (Which appears to cause your problem).
Opening the serial monitor should cause a board reset. In your case, opening the serial monitor must be doing a double reset.
I have the same problem with my Mega2560 clone, (a 16U2 version, not a CH340G), if I open the serial monitor first, then upload. If I immediately initialise the serial port in 'setup()' and print "Hello", I get "HelloHello".
I can stop that behaviour with a 10ms 'delay()' at the beginning of 'setup()'. (But it it matters I usually just close the serial monitor during uploads, then re-open it.)
If I upload first, then open the serial monitor, everything works as it should without the 'delay()'. In my case it's like the bootloader does a double reset.
It's interesting that it happens to you the other way around, when you open the serial monitor after uploading.
Which board are you using, and is it original Arduino or a clone?
Yes, it is the original Arduino Mega 2560 R3. It seems like opening the serial monitor restarts the execution of the program. If I have it closed while recompiling and uploading, the program executes and prints some intended statement on the screen. When I launch serial monitor after uploading, the program restarts. Thus, things like "HelHello" shows up on the screen.
bbqq:
Yes, it is the original Arduino Mega 2560 R3. It seems like opening the serial monitor restarts the execution of the program. If I have it closed while recompiling and uploading, the program executes and prints some intended statement on the screen. When I launch serial monitor after uploading, the program restarts. Thus, things like "HelHello" shows up on the screen.
With both Mega2560 and UNO boards, of course opening the serial monitor should reset the Arduino, restarting the program, but only the text printed after the reset should be visible on the serial monitor, not whatever was previously printed before the restart.
Does a 'delay(10)' at the beginning of 'setup()' stop the behaviour? (You might need to try a little longer than 10ms, but that works for me.)
but only the text printed after the reset should be visible on the serial monitor, not whatever was previously printed before the restart.
That is not what I find. It might be something to do with the way the hosts computers handles the serial buffers.
The IDE used to not let you have the serial monitor open during upload but the latest one does.
Why does it matter anyway?
Grumpy_Mike:
That is not what I find. It might be something to do with the way the hosts computers handles the serial buffers.
Hmmm. I admit that my main experience is with UNOs, I only have the one Mega2560. My UNOs behave differently to the Mega, and regardless of when the serial monitor is opened, they only print the text once.
Only my Mega has the described behaviour, and only after uploading with the serial monitor already open. That's why I figured it was a bootloader thing.
My Mega never does the print twice if I open the serial monitor after the upload has taken place, when the program is already running.
Maybe it does have something to do with the various computers/operating systems and their settings.
The IDE used to not let you have the serial monitor open during upload but the latest one does.
I don't have the latest IDE, only V1.6.5, and it allows uploading with the serial monitor open. (I wouldn't touch the latest version with a 10 foot pole.
)
Why does it matter anyway?
From a debugging/development point of view, it doesn't. (Not to me, anyway. And if it gets annoying I just add that 10ms delay and all is well.)
IIRC IDE versions handle the Serial Monitor differently. Older versions close the window on upload, newer version(s) only clear the monitor window.
bbqq:
Thus, things like "HelHello" shows up on the screen.
I don't think there is a simple solution to that. The Serial Monitor is really only intended for debugging and in that case it hardly matters if it is a bit scruffy.
You can easily write a PC program to receive the data from the Arduino which ignores and discards unwanted data.
...R
I would not blame the bootloader. I would blame the 16u2 chip that is acting as the USB interface. That is probably different on the Mega board.
I wouldn't blame the firmware at all. What should happen when the Serial Monitor is opened? If it doesn't reset the Arduino, everything sent since the reset after upload may be lost, until the Monitor attaches to the COM port. One cannot rely on the OS to buffer input from a closed COM port, between the end of the upload and the (re-)start of the Monitor.
MorganS:
I would not blame the bootloader. I would blame the 16u2 chip that is acting as the USB interface. That is probably different on the Mega board.
I have found this always happens no matter what board, what processor and what serial interface. Also as far as I know this has always happened for as long back as I can remember. The only constant is that I have always used a Mac.