Strcmp not working

Hi,
I'm trying to interpret code coming in from a 7600 GSM module when a call is received. I am getting the char array 'R', 'I', 'N', 'G' into my 32 byte array, I'm confirming this by mirroring out the content of the array on the console just before I compare it, but when I try and use strcmp to compare it to the string "RING" it never gets a match. Can anyone shed some light on what I'm doing wrong please?

SoftwareSerial myserial(7, 8);  //Define virtual serial port name as myseria,Rx is port 7, Tx is port 8
const byte numChars = 32;
char receivedChars[numChars];  // an array to store the received data
const byte numGChars = 32;
char receivedGChars[numChars];  // an array to store the received data

boolean newData = false;
boolean newGData = false;

const String AlexPhone = ####;
const String LizPhone = ####;
int gesture = -1;
const int powerPin = 12;
const int ledPin = 13;

void setup() {
  Serial.begin(57600);
  myserial.begin(57600);  //Initialize virtual serial port
  while (!Serial)
  turnGsmOn();
  Serial.write("Starting");
}

void loop() {
  recvWithEndMarker();  // Bluetooth input
  showNewData();
  recvGWithEndMarker(); // GSM input
  showGNewData();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  // if (Serial.available() > 0) {
  while (Serial.available() > 0) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    } else {
      receivedChars[ndx] = '\0';  // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void recvGWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char grc;

  // if (Serial.available() > 0) {
  while (myserial.available() > 0 ) {
    grc = myserial.read();

    if (grc != endMarker) {
      receivedGChars[ndx] = grc;
      ndx++;
      if (ndx >= numGChars) {
        ndx = numGChars - 1;
      }
    } else {
      receivedGChars[ndx] = '\0';  // terminate the string
      ndx = 0;
      newGData = true;
    }
  }
}

// Message from bluetooth link
void showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    // Checks input begins with a #
    if (receivedChars[0] == 35) { 
      if (receivedChars[1] == 67) {
        send_SMS("***********", "######");
      }
      if (receivedChars[1] == 66) {
        send_SMS("***********", "######");
      }
    } else {
      myserial.println(receivedChars);  //if myserial received data, output it via Serial.
    }
    newData = false;
  }
}

// Message from modem
void showGNewData() {
  if (newGData == true) {
    Serial.println(receivedGChars);
    // The compare below never works......but if I compare just the 
    // first char in the array to 'R' it does, so I know I'm getting 
    // something through.
    if (strcmp(receivedGChars, "RING") == 0) {
      Serial.println("ATA");
      myserial.println("ATA");
    }
    newGData = false;
  }

}

Do you see "RING" on the Serial Monitor in response to the above command?

RING\0 does not equal RING\n in a comparison.

Edit: Use the trim() function to remove that whitespace.

Yes.

The '\0' is the terminating null, to indicate the end of the string, so that would not be a problem.

The received data may have a carriage return at the end, or some other hidden character. Try comparing to "RING\r", or printing out the hex values for the received data.

Sorry, no I see "RING"

Correct RING\0 != RING\n\0
Remove the whitespace with trim().

I tried the \r trick but that didn't work either. When I print out the array char by char I get nothing in char[4].

It's probably \n, not \r

Hi, sorry, could you elaborate? Where I am trimming the whitespace from as it's an array comparing to a static string. I'm not an embedded expert by any means.

Try the following, to see if limiting the comparison to the first four characters will work.

    if (strncmp(receivedGChars, "RING", 4) == 0) {

If that works, then there is some non-printing character after the text.

I get this when I try......
error: too many arguments to function 'int strcmp(const char*, const char*)'
if (strcmp(receivedGChars, "RING", 4) == 0) {

I can almost guarantee there is a \n at the end of the received string.

Is strlen(receivedGChars) 4?

At the top you have a minor typo, which should not affect things

should be numGChars. Anyway, change it to

char receivedGChars[numGChars] = "RING"; 

and try the strcmp at the bottom of setup

strncmp, not strcmp

My line of code used strncmp, not strcmp, notice the 'n' in the middle.

Yep, that worked.

It was the newline character causing the problem.

Sorry, tried it again and it did work! So if using "Ring\n" isn't working how do I get it to match without having to put a limit on the comparator length?

@alext1772

1. You have only one Software UART Port -- mySerial(7, 8) using which can you communicate with Blutooth and GSM at the same time?

2. Disconnect the GSM from the network and keep the Bluetooth. Upload the following simple sketch in Arduino UNO and check if you can receive the message/string "RING" from your Android Phone via the Bluetooth.

#include<SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);

char myMsg[10];

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

void loop()
{
  byte n = mySerial.available();
  if (n != 0)
  {
    byte m = mySerial.readBytesUntil('n', myMsg, sizeof myMsg - 1);
    myMsg[m] = '\0';
    Serial.println(myMsg);
    if (strcmp(myMsg, "RING") == 0)
    {
      Serial.print("RING is received.");
    }
  }
}