Arduino Mega Serial port buffer issue

Dear all, I´m conecting an old mobile phone to my Arduino Mega. The problem occurs when I try to receive a SMS. The phone sends a string, let´s say of 80 characters. The problem is that I only receive 64 bytes instead of the full string. I´ve read that the serial buffer in a Mega is 128 bytes. is that correct?
When I count the bytes received in the buffer (Serial.available) I get 64 bytes only… Please can you help me to solve this issue?

void setup ()

{
Serial.begin(9600);
}

char vector[300];
char SMS[50];
char id[20];
int i, k=0, j,flag=0, qdatos=0;
void loop ()
{

if (Serial.available())
  {
   receive_str();                // llamo a la funcion receive_str que levanta del buffer el string
  }
if (flag==1)                    //imprimo el vector
    { Serial.print("PDU recibido ");
      for(j=0; j<qdatos; j++)
     {
       Serial.print(vector[j]);
     }
     flag=0;
     Serial.println();
     Serial.print("Numero ");
     for(j=0; j<k+1; j++)
     {
       Serial.print(id[j]);
     }
     k=0;
     Serial.println();
   }
   
}


void receive_str ()
{
delay(100);                  // le doy tiempo a que se llene el buffer de RX del puerto serie
qdatos=Serial.available();
Serial.println(qdatos);
for (i=0; i<qdatos; i++)
  {
   vector[i]=Serial.read();
   delay(100);
  }
flag=1;
for(i=30; i<41; i++)      // posicion 31 a 42 es el numero de telefono
  {
   id[k]=vector[i];
   k++;
  }
}

The complete string that the phone sends is

“+CMT: 26
06914543991063040C9145110465178600002170305141222907CC3259F6369B01”

Thank you

The problem is that I only receive 64 bytes instead of the full string. I´ve read that the serial buffer in a Mega is 128 bytes. is that correct?

Prior to 1.0, yes. With 1.0, both input and output are buffered, so the buffer is only half as large.

You can edit HardwareSerial.cpp (or .h) and change the size of the buffer.

You are better off not trying to fill up its internal buffer, but to read into your own. Then the exact buffer size doesn't matter.

http://www.gammon.com.au/serial

Either use the first idea there (a buffer) or try a state machine.

Thank you PaulS and Nick!!

Finally I´ve changed HardwareSerial.cpp from 64 to 128 and worked fine!!

Just a question to understand the functionality, what´s the difference between both values(RAMEND<1000 → SERIAL_BUFFER_SIZE 16 and RAMEND >1000 → SERIAL_BUFFER_SIZE 128)?

#if (RAMEND < 1000)
#define SERIAL_BUFFER_SIZE 16
#else
#define SERIAL_BUFFER_SIZE 128
#endif

Also, If I choose to have a buffer size of 128 bytes, this is only for RX? What happens if I choose let´s say 80 bytes? The others 48 bytes are assigned to the TX buffer?

Thank you

Alejandro

They are both that size. So you have now used 256 bytes of RAM.

struct ring_buffer
{
  unsigned char buffer[SERIAL_BUFFER_SIZE];
  volatile unsigned int head;
  volatile unsigned int tail;
};

...

  ring_buffer rx_buffer  =  { { 0 }, 0, 0 };
  ring_buffer tx_buffer  =  { { 0 }, 0, 0 };

Thank you Nick!!