[SOLVED] YASOQ (Yet Another Serial Overflow Question....)

Hi everyone !
First of all, i’d like to thank you all for all the explanations that I found in this forum that helped me a lot with my arduino project !
But even if I read lots and lots of topics about serial communication, I still don’t get something. So I post here to summerize what I understood to see if I’m totally wrong or if I just miss a little something.

I’m working with a GSM module (Teltonika TM2) and I’m trying to read the sms that is stored inside. It respects the following form :

+CMGR: “REC READ”,"+XXXXXXXX",“dd/mm/yyyy,hh:mm:ss+”,N,N,N,N,"+XXXXXXXXXXXXXXX",N,N
<Here is the message which will be between <> in order to help the sms parsing >

I’m using NewSoftSerial with a baud rate of 9600, when I send the AT command to read the message (“AT+CMGL=1”), I received on my arduino only the beginning of the msg, something like :

AT+CMGL=1
+CMGR: “REC READ”,"+XXXXXXXX",“dd/mm/yyyy,hh:mm:ss+”,N,N,N,N,"+XXX

(The “AT+CMGL=1” appears because of the echo done by the TM2 module).
My code is something like :

NewSoftSerial serialGSM(2,3);
serialGSM.begin(9600);
Serial.begin(9600);  //used for debugging
serialGSM.println(“AT+CMGL=1”);
delay(500);
while(serialGSM.available()>0)
{
char c = serialGSM.read()
Serial.print(c); 
}

I’m using a delay in order to give some time to TM2 to give a response to the Arduino.
But of course, with that delay, the serial buffer raises an overflow and I think that’s why I can read only the first characters.
The first thing I’ve done was to decrease the baud rate (and put a delay in the while) in order to be able to escape from the overflow!
(I’ve read the following topic http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1258390822/10)

NewSoftSerial serialGSM(2,3);
serialGSM.begin(2400);
Serial.begin(2400);  //used for debugging
serialGSM.println(“AT+CMGL=1”);
delay(1000);
while(serialGSM.available()>0)
{
char c = serialGSM.read()
	Serial.print(c); 
delay(100);
}

But the results are not really better than what I had before. My problem is probably “How am I supposed to compute the delay which will correspond to my design”. So that’s the first question.
After that I also tried to think about a way of programming that will allow me to avoid the use of delay. I was not the only guy to ask that question so I found some answers. But everyone looks agree with one of them (by PaulS) :
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1281439758/24
The use of a “start” and a “end” marker so I did something like what is written :

bool start = false;
bool end = false;
char inData[10]; // Leave plenty of room
byte index;

void loop()
{
  while(Serial.available() > 0)
  {
    char aChar = Serial.read();
    if(aChar == '<')
    {
	// Start of packet marker read
	index = 0;
	inData[index] = '\0'; // Throw away any incomplete packet
	started = true;
	ended = false;
    }
    else if(aChar == '>')
    {
	// End of packet marker read
	ended = true;
	break; // Done reading serial data for now
    }
    else
    {
	if(index < 10) // Make sure there is room
	{
	  inData[index] = aChar; // Add char to array
	  index++;
	  inData[index] = '\0'; // Add NULL to end
	}
    }
  }

  // When we get here, there is no more serial data to read,
  // or we have read an end-of-packet marker
  if(started && ended)
  {
    // We've seen both markers - do something with the data here

    index = 0;
    inData[index] = '\0';
    started = false;
    ended = false;
  }
}

But of course it does not change anything, because my serialGSM does not reach the first ‘<’. So my second question is : did I really not understood the started && ended stuff or is it supposed to help the acquisition of text in a serial communication and not just doing a better work when I will have to parse my result ?

Thanks again for your help

Tybo

The first thing I’ve done was to decrease the baud rate (and put a delay in the while) in order to be able to escape from the overflow!

The first thing I did when I saw that the boat was sinking was to switch to a smaller bucket and bail water slower.

That’s doesn’t seem like the correct approach to prevent the buffer from filling up. Switching to a slower baud rate helps, but delaying at all is not helping. The Arduino is perfectly capable of reading serial data far faster than it arrives. The problem with your code is that you are running out of data to read, not overflowing a buffer.

You need, instead, to read until there is no more serial data or until the > has arrived.

My problem is probably “How am I supposed to compute the delay which will correspond to my design”. So that’s the first question.

You aren’t supposed to delay at all. See above comments, too.

If the GSM module is not sending all the data, there is no way that you can read and parse it, so that is the first thing to work out.

serialGSM.begin(9600);
Make this value as high as the GSM module will support.

serialGSM.println(“AT+CMGL=1”);
After sending this command, try this:

char inByte = "";
unsigned long start = millis();

while((inByte = serialGSM.read() != '>' && millis() - start < 30000)
{
   if(inByte > 0)
   {
      Serial.print(inByte);
   }
}

This will print every character that can be read from the GSM module in 30 seconds.

If you are able to read the whole message this way, then you can add code in the block with the Serial.print() statement to collect (using the started && ended code you posted) the message portion of the data.

Thank you very much ! :relaxed:
Don’t understand why but it does not worked perfectly when I did the read() in the while expression but I just have to put it into the while and it’s working well !

 serialGSM.println(cmd);
 char inByte;
 unsigned long start = millis();
 
 while((inByte !='>') && ((millis()-start)<10000))
 {

	inByte=serialGSM.read();
 	
 	if(inByte>0)
 	{
 	
		if(inByte=='<')
		{
			started = true;
			ended = false;
		}
		
		else if(inByte=='>')
		{
			ended = true;
		}
		
	 
		if(started==true)
		{
			trame[idx]=inByte;
			idx++;
		}
		
		if(ended==true)
		{
			trame[idx]='\0';
		}
 	
 	}
 	
 }
Serial.flush();
serialGSM.flush();
}

Thanks again !