How does the mega2560 distinguish between a sketch upload and serial monitor

My first post here so sorry if this is a newbie question.
I'm developing a sketch that will allow me to communicate with my device using the USB serial port. It does occur to me, however, that the arduino software uses that same port to upload new sketches.

So what worries me is that I may, send some serial data to my board and inadvertently put the board in to "programming mode"
OR potentially more worryingly, have my sketch intercept data that is sent when the arduino software is attempting to upload a new sketch.

Is there something I should avoid to ensure that there is no conflict caused by my user of the serial port?

The Arduin Mega 2560 has a 'bootloader'. It is software that starts when the power it switched on. It checks the serial port for a new sketch that needs to be uploaded, and if there is no new sketch, it starts the sketch in its memory.

When you click 'upload', the Arduino board is reset, which causes the bootloader to run and connect with the Arduino IDE to upload a sketch.

I think it is impossible to mix those two, since there are a few checks when uploading a new sketch.

Yep, if you don't reset the board it will never enter the bootloader with anything you do.


Rob

Peter
Thank you for your response it helps me understand the process a little better but even that reset signal has to be sent through the com port somehow but at least it sounds like the bootloader code would be fairly safe from my kludgery.

Not just fairly safe, it is completely safe.

There are a few chips used by Arduino boards for a (simulated/virtual) serial port on the USB bus.
The simulated/virtual serial port does not only have the RX and TX signals, but also the other control signals like DTR, CTS and so on.

The DTR is connected to the Reset.
The DTR is also activated when you open the serial monitor (on the computer), so that also resets the Arduino board.

So far it is simple. Don't ask why the Arduino Leonardo is different.....

Peter, thank you so much.
I've actually got another program that I'm intending to use for serial communication. I've found that this also resets the board but at least now I know why. This was another issue I was attempting to debug. I found an article suggesting a workaround by putting a 120 ohm resister between reset and 5V (which I have tried) but that hasn't worked for me.

You've been a great help. Thanks again.

If you are writing a PC program to communicate with an Arduino it is a good idea to have the Arduino send a short message from setup() such as Serial.println("Arduino is Ready") and have the PC code wait until it receives that before starting to send any data to the Arduino. That way the PC knows the Arduino reset process has completed.

This demo illustrates the idea.

Also, make sure that your PC program maintains the connection with the Arduino until the Arduino is no longer needed. Some PC programs open and close connections for a few moments at a time - that doesn't work with an Arduino.

You may need to disable the auto-reset if you want the Arduino to keep working (perhaps for several days) while un-connected to a PC and then connect to it without losing data.

...R