I have a very long application that deals with a 4G shield and i made a code to parse the incoming data from the shield to the Arduino.
The code is supposed to read what comes from the shield and act upon that.
Reading is done and tested with no problems
The problem is, i have a condition in an if statement that is being met and yet the if statement is being skipped and the software just goes to the else.
Here is the part of the code that parse the data:
SoftwareSerial MYGSM(7, 8); // Define virtual serial port name as MYGSM,Rx is port 7, Tx is port 8
enum parsechecking //PARSING MACHINE WITH DIFFERENT STATES
{
todetectmessge,
toignore,
workwithit,
nextworkwithit
};
byte parsechecking = todetectmessge; //START HERE
char buffer[256]; // buffer of character
byte pos = 0; //the current position
uint8_t state; //machine inside the loop to send the command once
const uint8_t START = 0;
const uint8_t tochecknetwork = 1;
void resetBuffer()
{
memset(buffer, 0, sizeof(buffer));
pos = 0;
}
void setup()
{
MYGSM.begin(19200);
Serial.begin(19200);
}
void loop()
{
if (state == START)
{
Serial.println("we just sent the CSQ");
MYGSM.println("AT+CSQ");
//this command is sent to the module to check the connection
//it return a vule in this form: +CSQ: xx,xx
// so basically if it's 99,xx OR xx,99 i want to switch to offline
//because the connection was not established
state = tochecknetwork;
Serial.println("WE are reading ");
}
else if (state == tochecknetwork)
{
while (MYGSM.available())
{
const char *checked = MYGSM.read();
checkparsename(checked); //take the reading from it and parse it
}
}
}
void checkparsename(byte b)
{
buffer[pos++] = b;
if (pos >= sizeof(buffer))
resetBuffer(); // just to be safe
// Detailed debugging
Serial.println();
Serial.print("state = ");
Serial.println(parsechecking);
Serial.print("pos = ");
Serial.println(pos);
Serial.print("buffer = ");
Serial.println(buffer);
switch (parsechecking)
{
case todetectmessge:
Serial.println("Detecting");
if (b == '\n')
resetBuffer();
if (pos == 6 && strcmp(buffer, "AT+CSQ") == 0) //this is what was sent
{
parsechecking = toignore;
}
if (pos == 5 && strcmp(buffer, "+CSQ:") == 0)//this is the responce from the module
{
resetBuffer();
parsechecking = workwithit;
}
break;
case toignore:
Serial.print("Ignoring echoCSQ: ");
Serial.println(buffer);
resetBuffer();
parsechecking = todetectmessge;
break;
case workwithit:
if (b == ',')
{
Serial.print("The reciecved signal is:");
Serial.println(buffer);
if (pos == 4 && strcmp(buffer, " 99,") == 0)
{
Serial.println("it's 99");
// state = nosignalfound;
resetBuffer();
parsechecking = nextworkwithit;
}
else
{
Serial.println("we can go");
resetBuffer();
parsechecking = nextworkwithit;
// state = signalfound;
}
}
break;
case nextworkwithit:
if (b == '\n')
{
Serial.print("In the nextworkwithit:");
Serial.println(buffer);
if (pos == 2 && strcmp(buffer, "99") == 0)
{
Serial.println("SIGNAL NOT FOUND");
// state = nosignalfound;
}
else
{
Serial.println("signal found;");
// state = signalfound;
}
}
}
}
And i'm sure the conditions are satisfied accourding to the debugging
here is an example output
4:02:06.883 -> WE are reading
14:02:06.883 ->
14:02:06.883 -> state = 0
14:02:06.883 -> pos = 1
14:02:06.883 -> buffer = A
14:02:06.931 -> Detecting
14:02:06.931 ->
14:02:06.931 -> state = 0
14:02:06.931 -> pos = 2
14:02:06.931 -> buffer = AT
14:02:06.931 -> Detecting
14:02:06.931 ->
14:02:06.931 -> state = 0
14:02:06.931 -> pos = 3
14:02:06.931 -> buffer = AT+
14:02:06.976 -> Detecting
14:02:06.976 ->
14:02:06.976 -> state = 0
14:02:06.976 -> pos = 4
14:02:06.976 -> buffer = AT+C
14:02:06.976 -> Detecting
14:02:06.976 ->
14:02:06.976 -> state = 0
14:02:06.976 -> pos = 5
14:02:06.976 -> buffer = AT+CS
14:02:07.023 -> Detecting
14:02:07.023 ->
14:02:07.023 -> state = 0
14:02:07.023 -> pos = 6
14:02:07.023 -> buffer = AT+CSQ
14:02:07.023 -> Detecting
14:02:07.023 ->
14:02:07.023 -> state = 1
14:02:07.023 -> pos = 7
14:02:07.070 -> buffer = AT+CSQ
14:02:07.070 -> Ignoring echoCSQ: AT+CSQ
14:02:07.070 ->
14:02:07.070 -> state = 0
14:02:07.070 -> pos = 1
14:02:07.070 -> buffer =
14:02:07.070 -> Detecting
14:02:07.115 ->
14:02:07.115 -> state = 0
14:02:07.115 -> pos = 2
14:02:07.115 -> buffer =
14:02:07.115 ->
14:02:07.115 -> Detecting
14:02:07.115 ->
14:02:07.115 -> state = 0
14:02:07.115 -> pos = 1
14:02:07.115 -> buffer = +
14:02:07.161 -> Detecting
14:02:07.161 ->
14:02:07.161 -> state = 0
14:02:07.161 -> pos = 2
14:02:07.161 -> buffer = +C
14:02:07.161 -> Detecting
14:02:07.161 ->
14:02:07.161 -> state = 0
14:02:07.161 -> pos = 3
14:02:07.161 -> buffer = +CS
14:02:07.207 -> Detecting
14:02:07.207 ->
14:02:07.207 -> state = 0
14:02:07.207 -> pos = 4
14:02:07.207 -> buffer = +CSQ
14:02:07.207 -> Detecting
14:02:07.207 ->
14:02:07.207 -> state = 0
14:02:07.207 -> pos = 5
14:02:07.207 -> buffer = +CSQ:
14:02:07.253 -> Detecting
14:02:07.253 ->
14:02:07.253 -> state = 2
14:02:07.253 -> pos = 1
14:02:07.253 -> buffer =
14:02:07.253 ->
14:02:07.253 -> state = 2
14:02:07.253 -> pos = 2
14:02:07.253 -> buffer = 1
14:02:07.298 ->
14:02:07.298 -> state = 2
14:02:07.298 -> pos = 3
14:02:07.298 -> buffer = 10
14:02:07.298 ->
14:02:07.298 -> state = 2
14:02:07.298 -> pos = 4
14:02:07.298 -> buffer = 10,
14:02:07.298 -> The reciecved signal is: 10,
14:02:07.344 -> we can go
14:02:07.344 ->
14:02:07.344 -> state = 3
14:02:07.344 -> pos = 1
14:02:07.344 -> buffer = 9
14:02:07.344 ->
14:02:07.344 -> state = 3
14:02:07.344 -> pos = 2
14:02:07.390 -> buffer = 99
14:02:07.390 ->
14:02:07.390 -> state = 3
14:02:07.390 -> pos = 3
14:02:07.390 -> buffer = 99
14:02:07.390 ->
14:02:07.390 -> state = 3
14:02:07.390 -> pos = 4
14:02:07.390 -> buffer = 99
14:02:07.390 ->
14:02:07.437 -> In the nextworkwithit:99
14:02:07.437 ->
14:02:07.437 -> signal found;
14:02:07.437 ->
You see this singal found, it shouldn't be there!!!!
The buffer contained 99 at Pos=2 just like the condition at the if statement
Can anyone tell what's wrong here?
Also, i tried replacing the strcmp(buffer, "99") == 0 with things like saving the buffer into a const char and then compare that char to the 99, For example:
case nextworkwithit:
if (b == '\n')
{
Serial.print("In the nextworkwithit:");
Serial.println(buffer);
const char *toconcider = buffer;
Serial.print("the toconcider is:");
Serial.println(toconcider);
if (toconcider == '99')
{
Serial.println("state = nosignalfound;");
// state = nosignalfound;
}
else
{
Serial.println("state = signalfound;");
// state = signalfound;
}
}
}
i still had the same problem