what happened if the loop is faster than baud rate?

if baud rate is 9600bps ,8n1 , takes 1.04ms to Transmit a byte,and i write a code which loop is faster than 1.04ms,maybe 0.104ms.(serial.print() is in the loop.)

would the serial port monitor only shows one data but the loop probably already runs ten times?

sorry for my bad english ,really want to know these answer

It won't be a problem if you code correctly : you need to start your serial reception routine with serial.available(), that will tell you if there are characters to read. If more than one, you should either treat them all in the same instance of loop(), or accept to lose some if your loop() is too slow.

But too fast it can't be :wink:

Even if there is a baud rate (max speed of transitions, hence bits transmitted), the beauty of asynchronous transmission is that it never transmits garbage (like SPI could). So 9600bps means a maximum of about 1000 chars/second. The line stays IDLE a variable amount of time between characters.

Beware as well of the Serial.print(), you may have understood incorrectly how it works with Arduinos. Since a long time, the print function is asynchronous as well, which means that it doesn't block the loop for the needed time to print everything. Instead, the chars to print are buffered, and some background interrupt routine will send the chars one by one. So you have to be careful that a loop() with only Serial.println("Something"); in it for example would quickly behave erratically, after the buffer is full.

See the documentation Serial.flush() - Arduino Reference etc. for more "formal" explanation.

IIRC, both read and write buffers are 64 bytes big.

Full marks for asking the question. There have been many problems here where people wrote code that ignores the issue.

Have a look at the examples in Serial Input Basics - they are simple reliable ways to receive data without blocking and without any problem over the baud rate being fast or slow. Also the size of the Serial input buffer does not matter because it empties the buffer faster than the data arrives so it can't overflow.

...R

If you generate more traffic than can be transferred, the tx-buffer of 64 byte begins to fill, eventually slowing loop down to the transmission rate by blocking in Serial.print().

Oops, it looks as if I missed the fact that you want to transmit data. My Reply #2 was about receiving data.

...R

Beware as well of the Serial.print(), you may have understood incorrectly how it works with Arduinos. Since a long time, the print function is asynchronous as well, which means that it doesn't block the loop for the needed time to print everything. Instead, the chars to print are buffered, and some background interrupt routine will send the chars one by one. So you have to be careful that a loop() with only Serial.println("Something"); in it for example would quickly behave erratically, after the buffer is full.

It is you that does not understand Serial.print(). Serial.print() does indeed buffer data to be sent later. But, it does this only if there is room in the buffer. If there is not enough room, Serial.print() blocks until there is room.

This is why Serial.print() can not be used in an interrupt service routine. If the buffer is full, the only way that room is made in the buffer is by shifting bits out. But, shifting bits out relies on interrupts which are disabled while an ISR is running, so room can not be made in the buffer. So, Serial.print() sits and waits for something that can never happen, locking up the Arduino.

Thanks PaulS for your explanation ´╝îit helps me a lot especially the ISR part because my code only use poll and I still don't how to use ISR in arduino for now . i have few interrupt concepts but not enough to use it .

Also have gratitude to Robin2 that's not what i need right now though, but it probably would become useful to me and I am reading it.