Pages: [1] 2   Go Down
Author Topic: Problem with reading string from GSM  (Read 1440 times)
0 Members and 1 Guest are viewing this topic.
Orlando, FL
Offline Offline
Full Member
***
Karma: 0
Posts: 147
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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

« Last Edit: December 04, 2011, 03:23:19 pm by stealthtransam » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25761
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You give up on reading characters as soon as there is nothing more to read.
That doesn't seem a reasonable approach, IMO
Logged

"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.

Orlando, FL
Offline Offline
Full Member
***
Karma: 0
Posts: 147
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

AWOL  I thought of that being a potential problem as well and changed the While statement but that didn't help either.    
Code:
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.
« Last Edit: December 04, 2011, 03:39:09 pm by stealthtransam » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25761
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Functionally, the conditions are identical
Logged

"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.

Orlando, FL
Offline Offline
Full Member
***
Karma: 0
Posts: 147
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25761
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could use zoomkat's approach, and put in a short delay for every character read.
Logged

"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.

Orlando, FL
Offline Offline
Full Member
***
Karma: 0
Posts: 147
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25761
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Orlando, FL
Offline Offline
Full Member
***
Karma: 0
Posts: 147
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 185
www.rocketscream.com
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Orlando, FL
Offline Offline
Full Member
***
Karma: 0
Posts: 147
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-cry  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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25761
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

0
Offline Offline
Full Member
***
Karma: 1
Posts: 185
www.rocketscream.com
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See that "delay (1000);" in your code?
That's your mistake.
You'll get tonnes of bytes within 1000 ms!
Logged

Orlando, FL
Offline Offline
Full Member
***
Karma: 0
Posts: 147
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25761
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Pages: [1] 2   Go Up
Jump to: