i try to understand how the serial interface(usb) is actualy making a difference between the things i send and receive during my program runtime, and the data that are sent/received during code upload
what if the data i send correspond to some binary sequence that will trigger the code upload functions in the arduino firmware ?
The bootloader ALWAYS gets control first. It waits a short time to see if any code upload is going to take place. During this time, the bootloader uses the serial interface.
Once the bootloader is done, it hands over control (meaning: jumps to) your program. Your program can now take control of the serial interface. There is no "binary sequence that will trigger the code upload functions in the arduino firmware."
Two caveats:
The sequence "!!!" (three exclamation marks without the double quotes and without anything intervening) in the uploaded code means something special to the bootloader, so do NOT have this sequence in your program.
If your code jumps to the bootloader, or if a reset occurs, the bootloader will of course get control again
It all comes down to timing. When you upload a new sketch from the IDE it opens the serial port. This toggles the DTE handshaking signal of RS232. The Arduino has the DTE signal hardwired into the reset circuit. (This is why your Arduino resets when you open the Serial Monitor. You may not have noticed it before, but it does.) Then a "helper" utility that is distributed as part of the IDE called "avrdude" kicks in and communicates with the Arduino (hopefully the bootloader on the Arduino).
When the Arduino is reset it starts executing the first bit of code in it's memory, the bootloader. If the bootloader then sees the right message come in over the RX line (I actually don't know this message, but I don't need to because I'm not into modifying bootloaders or avrdude) it will respond and (I would imagine) perform a quick handshake with avrdude to load a new sketch. If it doesn't see that message after a set period of time (I'm not quite sure what it is, but I'm sure applying google-fu could answer it) the bootloader times out and then hands execution to the previously loaded sketch which never releases execution control (barring a reset command or intentionally mucking with the execution pointer with assembler code). Because the bootloader doesn't (normally) execute again you can't accidentally force it to load data into sketch space. I suppose one could be extremely unlucky and have an incoming serial stream on digital pin 0 when during a manual reset with the reset button (or after a power glitch) that the bootloader would mistake as a command and normal handshaking between the bootloader and avrdude to overwrite the sketch space, but IMHO that would be extremely rare, bordering on nearly impossible. (But I could be wrong...)
The sequence "!!!" (three exclamation marks without the double quotes and without anything intervening) in the uploaded code means something special to the bootloader, so do NOT have this sequence in your program.
The sequence "!!!" (three exclamation marks without the double quotes and without anything intervening) in the uploaded code means something special to the bootloader, so do NOT have this sequence in your program.
well that's an issue..that i was expecting, hence my question
one is supposed to be able to send any kind of data through serial interface
i guess i'll have to use Serial1 instead
well that's an issue..that i was expecting, hence my question
one is supposed to be able to send any kind of data through serial interface
i guess i'll have to use Serial1 instead
You're not paying attention. The "!!!" sequence is ONLY an issue if you have a Mega bootloader AND it's an issue ONLY when the bootloader is receiving the data. The bootloader will NOT be running when your code is running AND you can't upload code on Serial1
well that's an issue..that i was expecting, hence my question
one is supposed to be able to send any kind of data through serial interface
i guess i'll have to use Serial1 instead
You're not paying attention. The "!!!" sequence is ONLY an issue if you have a Mega bootloader AND it's an issue ONLY when the bootloader is receiving the data. The bootloader will NOT be running when your code is running AND you can't upload code on Serial1
ok, that's right sorry about the mistake, indeed it concerns the bootloader...
i was talking about Serial1 for communication, not for code upload