Go Down

Topic: reading chars from Software serial and comparing (Read 123 times) previous topic - next topic

psabade

Hi,
I am newbee in Arduino development.
I am trying to read charecters from software serial and comparing it with predefined string.
But, I am not able to compare the string - I mean, the comparison is not success.
I am not sure why.
Hence, I need some help from experts here.

my code is as below:

void loop() {
  if(SIM900.available() >0)
  {
    inchar = SIM900.read();
    if(inchar != '\n') {
      str += inchar;
    }else {
      if(str == "RING") {
        Serial.println("phone is ringing");
        str.remove(0);
      }
    }
  }
}

Thanks in advance for the help.
Praveen.

PaulS

Why not print out str, and what it REALLY contains?

psabade

Thanks Paul.

I did that. I added print in the else block. I got the console o/p as below:
///////////////////////////////////
RING

+CLIP: "+XXXXXXXXXXXX",145,"",,"XXX",0

NO CARRIER
///////////////////////////////////

But, still the compare check is failing.
I am not getting "phone is ringing" printed on console.

sterretje

inchar might also contain e.g. '\r'. In which case str contains that as well and the compare will fail.

Code: [Select]

    if (inchar != '\n' && inchar != '\r') {
      ...
      ...


When posting code, please use code tags; [code]your code here[/code] will result in

Code: [Select]

void loop()
{
  if (SIM900.available() > 0)
  {
    inchar = SIM900.read();
    if (inchar != '\n') {
      str += inchar;
    } else {
      if (str == "RING") {
        Serial.println("phone is ringing");
        str.remove(0);
      }
    }
  }
}
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

psabade


Robin2

You need to post a complete program so we can see how variables are defined.

I'm guessing that your variable str is an instance of the String class. The String class can cause memory corruption in the small memory of an Arduino. Use C strings (a char array terminate with 0.

Have a look at the examples in Serial Input Basics and use the strcmp() function to compare strings.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Quote
I did that. I added print in the else block. I got the console o/p as below:
You got a bunch of anonymous crap that we can't make heads or tails of.

Print the String with identification AND with start and end markers:
Code: [Select]
Serial.print("The phone sent: [");
Serial.print(str);
Serial.print("]");


If str really does contain "///////////////////////////////////
RING

+CLIP: "+XXXXXXXXXXXX",145,"",,"XXX",0

NO CARRIER
///////////////////////////////////", I can not, for the life oe me figure out how you expect that match "RING".

I would be far more likely to believe that str was printed more than once to show all that output.

psabade

Paul,

okay.. please find the code snippet below:
Code: [Select]

#include <arduino.h>
#include <SoftwareSerial.h>
char inchar;
SoftwareSerial SIM900(7, 8);
 
int numring=0;
int comring=3;
int onoff=0; // 0 = off, 1 = on
String str;
String str2 = "RING";
 
void setup()
{
  Serial.begin(9600);
  SIM900.begin(9600);
  SIM900.print("AT+CLIP=1\r"); // turn on caller ID notification
  delay(100); 
}
 
void loop() {
  if(SIM900.available() >0)
  {
    inchar = SIM900.read();
    if( (inchar != '\n')  ) {
      str += inchar;
    }else {
      Serial.println(str);
      if(str == "RING") {
        Serial.println("phone is ringing");
        str.remove(0);
      }
    }
  }
}



and the output on the serial console is :



RING


RING



RING

+CLIP: "+XXXXXXXXXXXX",145,"",,"XXX",0


RING

+CLIP: "+XXXXXXXXXXXX",145,"",,"XXX",0



RING

+CLIP: "+XXXXXXXXXXXX",145,"",,"XXX",0

NO CARRIER


PaulS

You CLEARLY missed the part about printing the str value WITH identifier AND start and end markers. No one can help you until you pay attention!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy