Matching A String - Issue with Newline (i think!)

:blush:

Hi there,

There is probably a simple solution to this - but its got me stumped and searching the web has yielded zero results :~

I am using the Sparkfun Cellular Shield to make a TCP connection to send and receive data using GPRS.

I have it working very well. However, the shield will, at some point return the string '+STCPD:1' to tell the program that there is new data waiting to be read.

The problem i have, is coding the script to match that string. Here is the simplest code I am using:

input = "";
  while(cell.available() >0)
  {    
    incoming_char=cell.read();    //Get the character from the cellular serial port.    
    input+=incoming_char;
    delay(100);      
    if(input=="+STCPD:1")
    {
     Serial.print("DATA MATCH!");
    }
    Serial.print(input);
  }

The serial output clearly shows +STCPD:1 but doesn't output the Data Match!

My only thinking is that output is always on a new line eg>

+S
+ST
+STC
+STCP
+STCPD
+STCPD:
+STCPD:1

But i am using Serial.print() so there should not be a new line? To this end, I can't help but think that the string to match also contains new line characters - but i have tried adding these to the match algorithm but no joy there either.

What could i be missing?

Thank you very much in advance for any help! 8)

    delay(100);

Why? You read some data, and now you want to sit around with your thumb up somewhere doing nothing for 1/10 of a second. Why?

There is a reason that code snippets don't cut it. We have no idea what type input is, or what type incoming_char is, though we could guess.

My only thinking is that output is always on a new line eg

The only way that that can happen is if the first character in input is a carriage return. You probably should not be storing them. Since you do, though, you'll have a hard time comparing the string to a string that does not have a carriage return embedded in it. You could try comparing to "\n+STCPD:1" or "\r+STCPD:1". Be better to not store the carriage return or line feed in the input object.

    delay(100);

Why? You read some data, and now you want to sit around with your thumb up somewhere doing nothing for 1/10 of a second. Why?

There is a reason that code snippets don't cut it. We have no idea what type input is, or what type incoming_char is, though we could guess.

Yup. Its an eternity :stuck_out_tongue: my original code didn't have that line but I read somewhere the buffer needs the slight delay. Don't believe it myself but figured what the hell - can't do any harm during testing!

The input comes from the SM5100B Cellular chipset - and that's my problem -I don't know what hidden characters are lurking in the input.

I was thinking the carriage return would come after the string - didn't think to try the beginning. Will give it a go and see what happens.

But based on the limited code snippet and serial output provided - it should match though shouldn't it? I mean my code isn't flawed or anything is it?

I've seen code that checks each incomming character for a match, and discards the character that matches. You probably could do the same for a line feed or carrage return.

You probably could need to do the same for a line feed or carrage return.

if(incoming_car != '\n' && incoming_char != '\r')
  input += incoming_char;

You probably also need to stop using the String class or you will run into memory fragmentation sooner or later.

I've done a couple of posts about storing serial data, without using String, here:

It includes (now) a description of using a state machine. Or you can just store into a static buffer. Then you can do strcmp to see what is in the buffer. Note that the example code in my post discards carriage-returns.

I've done a couple of posts about storing serial data, without using String, here:

I looked at the "buffering input" and it might be useful to add printing the captured input line back to the serial monitor just as an example of doing something with the captured line.

It does that, here:

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {
  // for now just display it
  Serial.println (data);
  }  // end of process_data

It helps if the NL and CR are turned on in the serial monitor! :slight_smile:

I think i found the problem but is was not as expected.

The string "+STCPD:1" would never match. However, "+STCPD: 1" would.

It would seem that ':1' does something unexpected.

So in the incoming read buffer i didn't allow the ':' colon character to be added to the buffer resulting in "+STCPD1"

It the matched perfectly every time! Thanks for help guys 8)