Data flow of control - Sim 900 module with Arduino mega 2560

I own a Sim 900 based module (link) and arduino mega 2560. I am connecting it to serial port 1. serial port 0 being used with PC. I have no problem recieving/dialling phone numbers or sending a SMS. The problem I am having is while reading the contents of SMS. Since the buffer size of ArduinoMega is 64 bytes, the message I read using AT command - AT+CMGR=n (n being the index) and then reading it using serial1.read(), I only get partial details (not even the body of the sms)

I understand that the problem is occuring due buffer overflow. IS IT?

How to use data flow of control? (SIM 900 module documentation states that both hardware or software flow of control are supported). RTS/CTS pins needed for hardware flow of control? using XON/XOFF for software flow of control?

Does arduino mega has RTS/CTS pins? How is software flow of control achieved?

Should I post in some other section?

Posting your code would be a better idea.

I understand that the problem is occuring due buffer overflow. IS IT?

Only you can tell us that. But, most likely not. Serial data transmission is slow. So a 64 byte buffer allows you to be doing a lot of other stuff, and only occasionally read the serial data. If you stop reading too soon, you will not get a complete message.

Should I post in some other section?

If you want help based solely on the information shown here, www.psychics.com/Arduino/NowhereNearEnoughInfo would be a more appropriate forum.

Terribly sorry for not posting the code. At the moment, I have resolved my problem by changing the buffer size to 256 bytes in hardwareserial.cpp. Do tell if you find any mistakes in code or any poor programming practice.

int gsmready=0;
char mobileno1[]="91XXXXXXXXXX";       // primary no
char mobileno2[12];                   // temporary(to save caller no in case of ring)
char inchar;
volatile int state = LOW;
const int ledPin =  13;
void setup()
{
 attachInterrupt(0, blink, RISING);
  pinMode(ledPin, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  
  digitalWrite(ledPin, LOW);
  Serial.begin(115200);
  Serial1.begin(115200);    // Gsm Modem baud rate
  Serial.println();
  chkgsm();
  while (gsmready!=1)
  {
    chkgsm();
    Serial.print("Check");
  }
}
void chkgsm()
{
    delay(5000);                              //according to SIM900 documentation
    Serial.println("CHECK GSM--->");
    Serial1.println("AT");
    delay(1000);
    while(Serial1.available())
    {
      inchar=Serial1.read();
      Serial.print(inchar);
      if(inchar=='O')
      {
        delay(10);
        inchar=Serial1.read();
        Serial.print(inchar);
        if(inchar=='K')
        {
          delay(800);
          Serial.println("GSM Is Ready");
          Serial1.println("AT+CNMI=3,3,0,0");
          delay(800);
          Serial1.println("AT+CLIP=1");
          delay(800);
          Serial1.println("AT+CMGF=1");
          delay(800);
          Serial1.println("AT+CMGD=1"); // delete all SMS
          digitalWrite(ledPin, HIGH); 
          gsmready=1;
          break;
        }
      }
    }
}
void loop()
{
  //Serial.println("IN loop");
  if (state==HIGH)
  {
    Serial1.println("ATD+919897523184;");
    state=LOW;
  }
  
  decodegsmdata();
}
void decodegsmdata()
{
   if (Serial1.available() >0)
   {
    // Serial.println("Decoding Gsm Data");
      inchar=Serial1.read();
      Serial.print(inchar);
      if (inchar=='R')
        {
          delay(1000);
          inchar=Serial1.read();
          if (inchar=='I')
            {
               delay(10);
               inchar=Serial1.read();
               if (inchar=='N')
                 {
                    delay(10);
                    inchar=Serial1.read();
                    if (inchar=='G')
                      {
                         delay(10);     // So the phone has 'rung' 
                         ring();        // call ring function to get mobileno and send message
                         Serial.println("Someone Calling");
                         delay(5000);
                         Serial1.println("ATD+919897523184;");
                         Serial.println("Call has been placed");
                      }
                  }
              }
         }  
        else if (inchar=='C')
        {
          //Serial.println("C found");
          delay(10);
          inchar=Serial1.read();
          if (inchar=='M')
          {
            //Serial.println("M found");
            delay(10);
            inchar=Serial1.read();
            if (inchar=='T')
            {
              //Serial.println("T found");
              delay(10);
              inchar=Serial1.read();
              if (inchar=='I')
              {
                //Serial.println("I found");          
                readmsg();
              }
            }
          }
        }
   }
}

void ring()
{
  Serial.print("Test String After Ring:");
  while(Serial1.available())
  {
    inchar=Serial1.read();
    Serial.print(inchar);
        if (inchar=='C')
        {
          Serial.println("C found");
          delay(10);
          inchar=Serial1.read();
          if (inchar=='L')
            {
               delay(10);
               inchar=Serial1.read();
               if (inchar=='I')
                 {
                    delay(10);
                    inchar=Serial1.read();
                    if (inchar=='P')
                      {
                        Serial.print(Serial1.read());
                        Serial.print(Serial1.read());
                        Serial.print(Serial1.read());
                        Serial.println(Serial1.read());
                        Serial.print("Caller No :");
                        for(int i=0;i<12;i++)
                        {
                          mobileno2[i]=Serial1.read();    // Save caller mobile no     
                          Serial.print(mobileno2[i]);
                        }
                         Serial1.print("ATH");          // To cut call
                         Serial1.println();
                         //readmsg();
                         //sendmsg(mobileno2);
                      }
                  }
              }
      
      }
    }
}

void sendmsg(char mobileno[])
{
  Serial1.print("AT+CMGS=");
  Serial1.write(34);
  for(int i=0;i<12;i++)
  { 
    Serial1.print(mobileno[i]);
  }
  Serial1.write(34);
  Serial1.println();    
  Serial1.write(26);
}
void readmsg()
{
  int tempPin;
  Serial1.println("AT+CMGR=1");      //Read msg at index 1
  Serial.println("AT+CMGR=1");
  delay(1800);
  while(Serial1.available())
  {
     inchar=Serial1.read();
     Serial.print(inchar);
     if(inchar=='S')
     {
       inchar=Serial1.read();
       if(inchar=='W')
       {
         inchar=Serial1.read();
         if(inchar==' ')
         {
           inchar=Serial1.read();
           if(inchar=='L')
           {
             inchar=Serial1.read();
             switch(inchar)
             {
               case '1': tempPin=13;        //PIN 13 is led 1
                         break;
               case '2': tempPin=8;          //PIN 8 is led 2
                         break;          
               case '3': tempPin=9;        //PIN 9 is led 3
                         break;
             }
             inchar=Serial1.read();
             if(inchar==' ')
             {
               inchar=Serial1.read();
               if(inchar=='1')
                {
                  digitalWrite(tempPin, HIGH);
                  break;
                }
                else if (inchar=='0')
                {
                  digitalWrite(tempPin, LOW);
                  break;
                }
             }
           }
         }
       }
     }
  }
  delay(800);
  Serial1.println("AT+CMGD=1"); // delete SMS at index 1
}

void blink()
{
  state = HIGH;
}
   if (Serial1.available() >0)
   {
    // Serial.println("Decoding Gsm Data");
      inchar=Serial1.read();
      Serial.print(inchar);
      if (inchar=='R')
        {
          delay(1000);
          inchar=Serial1.read();
          if (inchar=='I')
            {
               delay(10);
               inchar=Serial1.read();
               if (inchar=='N')
                 {
                    delay(10);
                    inchar=Serial1.read();
                    if (inchar=='G')
                      {

If there is at least one byte of data to read, read all 5, real slowly. What happens is some serial data is lost, and RNG arrives, instead of RING?

It's no wonder you had to increase the serial buffer size, with all that diddling around you do. You should be reading serial data as fast as possible, not napping between each character. If you expect to get "RING" followed by some other data, you should read the data as fast as it arrives. After each character, see if the string is "RING". If so, do one thing. If not, do something else, which may be just keep reading.

I suspected the same after reading an example at tronixstuff (a website). So should I do away with the delay altogether or should I use some small delay?

So should I do away with the delay altogether or should I use some small delay?

Should I waste no time reading serial data, or only a little bit of time? Me, I'm a busy person. I have no time to waste.

PaulS:

So should I do away with the delay altogether or should I use some small delay?

Should I waste no time reading serial data, or only a little bit of time? Me, I'm a busy person. I have no time to waste.

Got it. :)

Though I am having another problem now. I am unable to send SMS with the above mentioned code. Please help if you can. What is even more frustrating that the same code (with baud rate 9600 in that case) is working perfectly with SIM300 module.

Thanks. But the problem is resolved. I found that I had to use some delay before sending message body. And the some more after sending CTRL Z

A word though, Does changing buffer size has any -ve effect?

A word though, Does changing buffer size has any -ve effect?

The SRAM that the buffer occupies is not available for other purposes.