incoming data buffered?

Hi!

I'm having troubles with serial communication and I'm not sure if I understood the concept of receiving bytes well so I would need some clarification.

Does the Serial receive have some kind of buffer where it stores incoming bytes until my software reads them, or do I risk losing bytes if they happen to arrive when my program is busy doing something else? I always assumed the received bytes sit there waiting until my program reads them out. Is this correct?

If this is the case, when I will read the bytes out later, which byte comes out first? The most recently received byte or the oldests?
I mean, say I would send 'ABC' to the Arduino's serial port and say a moment later I would do IncomingByte = Serial.read(), would it return the 'A' or the 'C'?

Thanks a lot!

Yes, the UART has a buffer. Can't remember how long though.

would it return the 'A' or the 'C'?

A. The buffer is "first in, first out" (FIFO)

Hope that helps..

The HardwareSerial object has a buffer and how long it is depends on the version of software and the amount of RAM in use, but is usually 64 or 128 bytes.

hello
I also have one question
I'm trying to receive a message from a gsm modem but I think I have a buffer problem.
If I connect the modem using a hiperterminal client I received all the message ok, connecting it to arduino mega I just can retrieve half of the message.Coult it be because buffer overflow??
were is my code

//Read new SMS from Motorola C168i to Arduino and print to LED panels
//Program developed by Matthew Mosher ©2010


#include <NewSoftSerial.h>

NewSoftSerial modem_gsm(10, 11);
char sms[500];
int i;


void setup()  
{
  Serial.begin(9600);
  Serial.println("Goodnight moon!");

  // set the data rate for the NewSoftSerial port
  modem_gsm.begin(9600);
  delay(500);
  modem_gsm.println("AT+CMGF=0\r\n");  // Set modem_gsm mode to text (vs pdu)
  delay(500);
  modem_gsm.flush();
  delay(300);


}



void loop()                     // run over and over again
{


  modem_gsm.print("AT+CMGR=1"); 

  modem_gsm.println("\r\n"); 
  delay(500);

  // Debug Loop
  for (i = 0; i < 500; i++) {
  if (modem_gsm.available() > 0) {
      char c = modem_gsm.read();
     Serial.print(c, BYTE);
      }
    delay(10);   
  }
  

  delay(300);
  modem_gsm.flush();
  delay(300);

}


}

this is what I should expect like in hiperterminal:
+CMGR: 1,,22
0791539126010000040C915391862668620000212080013203000331D90C

OK

And this is what arduino reply when connect to the modem:

AT+CMGR=1

+CMGR: 1,,22
0791539126010000040C91539186266862000

The rest of the message is missing.Another thing that is bugging me is why it apears "AT+CMGR=1" on the output since I'm just seending it to the modem, it should not expected to see it when receiving!!!
any help

What is the point of the for() loop? You only check serial available 500 times. Why would you expect the message to be completed by then?

yes you are right.can you sugest a simple way to just received what is in the buffer? thanks

Problem solved:
using the softserial library aparently the buffer is lower.Since arduino board have 3 UART ports i remove the library and use the serial1 and it works now.