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