Go Down

Topic: Problem with reading string from GSM (Read 1 time) previous topic - next topic

stealthtransam

Dec 04, 2011, 09:16 pm Last Edit: Dec 04, 2011, 09:23 pm by stealthtransam Reason: 1
I am having a problem with the arduino reading and building a string from incoming serial data from my GSM module.  It's reading most of the string but not all of it.  Attached is my code.

Code: [Select]
#include <NewSoftSerial.h>

NewSoftSerial GSM(2, 3);

char inData[100];
byte index = 0;

void setup()  
{
 Serial.begin(115200);                                // initialize the serial port on the device
 Serial.println("Retrieving Information...");

 GSM.begin(9600);                                   // set the data rate for the NewSoftSerial port attached to the GSM module

 ReadSerialString();
 Serial.print("inData: ");
 Serial.println(inData);
}

void loop()
{

}

void ReadSerialString()
{
   GSM.println("AT+CMGL");
   delay(1000);
   while(GSM.available() > 0)
  {
char aChar = GSM.read();
if(aChar == '\n')
{
  // End of record detected. Time to parse
  index = 0;
  inData[index] = NULL;
}
else
{
  inData[index] = aChar;
  index++;
  inData[index] = '\0'; // Keep the string NULL terminated
}
  }
}


I'm getting this:
+CMGL: 1,"REC READ","+19998887777",,"11/12/02,23:06:29-

when I want to get this:
+CMGL: 1,"REC READ","+19998887777",,"11/12/02,23:06:29-32"

Ideally I want to get all of this  in 3 strings (line 1 as a string) (line 2 as a string) (line 4 as a string) but I figured I'd work on it one string at a time:
+CMGL: 1,"REC READ","+19998887777",,"11/12/02,23:06:29-32"
Yeah it works!

OK


AWOL

You give up on reading characters as soon as there is nothing more to read.
That doesn't seem a reasonable approach, IMO
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

stealthtransam

#2
Dec 04, 2011, 09:36 pm Last Edit: Dec 04, 2011, 09:39 pm by stealthtransam Reason: 1
AWOL  I thought of that being a potential problem as well and changed the While statement but that didn't help either.    
Code: [Select]
while(GSM.available())

The number of text message and the phone number will very so the string length will not always be the same so I'm telling it to take all incoming characters up to the line feed and store them in a string.

AWOL

Functionally, the conditions are identical
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

stealthtransam

I'm up for suggestions.  It seams to get stuck reading the string right after the dash (-) every time.

AWOL

You could use zoomkat's approach, and put in a short delay for every character read.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

stealthtransam

In case you guys wanted to know what was wrong it wasn't my code =)  the string that was being sent from the GSM was too big for the NewSoftSerial library to handle and it was dropping all the rest of the data.  It was a simple fix.  I went into the NewSoftSerial.h file and changed the buffer size from 64 to 256 and that made everything all better and now working!

AWOL

How can you say it wasn't your code?
Your buffer was big enough for the data, but you didn't accept the data fast enough.
Don't blame the library.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

stealthtransam

wasn't my code it was the library NewSoftSerial.  Nothing in there documentation says that there buffer is small then that of the hardware serial port.  The changes I made were in the library not my code.

48X24X48X

9600 bps is pretty slow compared to what the Arduino is running at. Receiving a byte should take about 1ms at 9600 bps which a lot slower than your loop is running.

stealthtransam

it is an arduino 3.3V pro so it is running at 1/2 the speed of the UNO.  NewSoftSerial goes crazy if i run it any faster than 9600 on the 3.3V Pro =(  All that really matters is I got my problem solved and my code is running.  I was looking on the GPS and GSM forms around the web and it seams like a lot of people run into buffering problems with NSS and really large strings like that of GPS and GSM.

Thanks all for your input.  It's always appreciated.

AWOL

See that "delay (1000);" in your code?
That's your mistake.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

48X24X48X


See that "delay (1000);" in your code?
That's your mistake.

You'll get tonnes of bytes within 1000 ms!

stealthtransam

I actually did lower the delay to 450ms.  the GSM module takes it time before it sends the string back and I've tested it with no delays, varying time delays, etc.. and if I don't put a delay in there the While loop times out before it ever receives the first byte of data.  With my current code a delay of 450m -2s reads all the data every time.  I'm not using the arduino GSM shield, I'm using a ublox LEON G100 module.  I'm quite happy with the way the code is running now.

AWOL

No, really, you're misunderstanding the problem, and this kind of thing will bite you on the ass again if you don't address it.
There is no reason a 16MHz processor shouldn't be able to do this with a serial buffer of a single character at 9600 bps.
Rewriting the serial library is a sticking-plaster solution.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up