Problem with comparing Serial.readString to a String

Hi guys I'm having a problem with comparing Serial.readString and a String with it always returns false
I am using the Software serial library if that makes any difference

My Code example

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); // RX, TX

void setup() {
    Serial.begin(9600);
    mySerial.begin(9600);
}

void loop() {
    if(Serial.available() > 0){
        if (mySerial.readString().equals("SOURCE 11"))
        {
          Serial.println("testing123");
        }
    }
}

It's probably me forgetting something silly and it's not the .equals to == tried that didn't work ever.

So you check Serial for a character available then use Serial.readString to fill your String named data with characters and then do nothing with it.

Then without checking on mySerial.available you proceed to readString on that in a compare.

I don't use C++ Strings on small memory computers like AVR's. They waste cycles and bytes, they shotgun the heap (RAM) with actions designed to 'make things easier' for, well, I won't say it.

These microcontrollers are small hardware with small RAM. C char array strings are the way to go with these chips. You define your string with a fixed size from the start and you can manipulate the contents directly (char myString[ 12 ] is an array) through indexes and/or pointers or you can use functions in the <string.h> library.

Ooooops wrong piece of code.

That was me testing something even without the variable data I have the same problem

ryaneastland:
Ooooops wrong piece of code.

That was me testing something even without the variable data I have the same problem
[/quote

Ok, you will get rid of string and then come back and tell how it works?

Well it didn't work I've tried like 10 different ways of doing it including this I wrote which doesn't use the readString

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); // RX, TX
String inData;
String string[] = {"SOURCE 11", "SOURCE 21", "SOURCE 41"};
int size = 3;
int i;
void setup() {
    Serial.begin(9600);
    mySerial.begin(9600);
}

void loop() {
    if (mySerial.available())
    {
        char recieved = mySerial.read();
        inData += recieved; 
        // Process message when new line character is recieved
        for (i = 0; i < size; i++) {
          if (string[i].equals(inData))
          {
              Serial.print("Arduino Received: ");
              Serial.println(inData);
  
              inData = ""; // Clear recieved buffer
          }
        }
    }
}

It is not a good idea to use the String (capital S) class as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Everyone who has replied has nothing to do with my problem the if statement if (mySerial.readString().equals("SOURCE 11")) never returns true and in my piece of code that doesn't use the readString method if (string*.equals(inData)) returns true the first time it should return true but it doesn't after that no matter which value in the array is used.*

Try this. Tested, working.

And for everyone, the compiler gave me a headache, see below.

#include <string.h>

char inData[ 20 ];
char compareString[3][10] = { "SOURCE 11", "SOURCE 21", "SOURCE 41" };
byte cmpStrings = 3;
byte cmpIndex;
byte entryDone;
char received;

byte i; // if it's not going to hold values over 255, use 8 bit type byte instead of 16 bit type int.

void setup()
{
  Serial.begin(9600);
//  for ( i = 0; i < cmpStrings; i++ )
//  {
//    Serial.println( compareString[ i ] );
//  }
//  Serial.println( );
}

void loop()
{
  if (Serial.available())  // get the entry
  {
    received = Serial.read();
    if ( received != '\n' )  inData[ cmpIndex++ ] = received;
    if (( received == '\n' ) || ( cmpIndex == 20 ))
    {
      entryDone = 1;
      cmpIndex = 0;
//      Serial.print("Arduino Received: ");
//      Serial.println(inData);
    }
  }

  if ( entryDone > 0 )
  {
    entryDone = 0;
    // Process message when new entry is recieved
    for ( i = 0; i < cmpStrings; i++ )
    {
      if ( strcmp( inData, compareString[ i ] ) == 0 )
      {
        Serial.print("Arduino Matched: ");
        Serial.println(inData);

        inData[ 0 ] = 0; // Clear recieved buffer
      }
    }
  }
}

It wouldn't do the compare until I changed

if (entryDone )

to

if ( entryDone > 0 )

I'm running 1.6.9 on XP so it may be a byte position or something.

I'm having a problem where checking for a new line it never thinks there was a new line

Did you set serial monitor to send a newline at the end of every input line?

Nope just worked that out

Then maybe set the baudrate up as fast as it runs reliably... perhaps 57600 for soft serial.
That will get your data in faster and clear the output buffer faster too.

Since I got 1.6.9 I've been running Serial at 250000. I have a word-match that keeps up too.