Search code examples
cmodematmel

Evaluating serial replies with if statement


I am trying to read network statuses from a modem using an atmel MCU, and then debug/restart based on certain status replies. I do so with the following code (and this works):

scanf("%s", state);
printf_P(PSTR("%s\n%d\n"),state,tempstate);

    if (*state=='4'|| *state=='7' || *state == '9' || *state == '11' || *state == '12' || *state == '13' ||*state == '19' || *state == '30' || *state == '31')
    {
        count++;
        if(count == 5)
            {
                send_string("ATZ\r");
                _delay_ms(10000);
                count = 0;
            }
        else{}
    }

However, when trying to do something similar in a pin change interrupt (used for a door switch) I can read the modem reply 'OK', but when trying to confirm that reply with an if statement, the reply is not recognized. See below.

    send_string("AT\r\n");
    scanf("%s", reply);
    printf_P(PSTR("\n%s"),reply);

    if (*reply == 'OK')
    {
    printf_P(PSTR("\nWill text contact now."));
    send_string("AT*SMSM2M=\"15555555TESTING\"\r");
    scanf("%s", reply);
    }

I cannot manage to get my code to enter that if statement after the 'OK' is received. Any help is appreciated.


Solution

  • See http://en.cppreference.com/w/c/string/byte/strcmp

    For comparing the string "OK" (note the different quotes than in your code)
    with what a char* reply points to, use

    strcmp(reply, "OK")
    

    Note that this returns 0 for identity.
    Hence, an if similar to what you seem to try would be

    if(!strcmp(reply, "OK"))
    

    As mentioned by dbush in comment:
    Note that you need to do this for the number comparisons as well, since you have strings containing numbers and not actual numbers.
    Some of them (those comparing to a single character) misleadingly work, because you accidentally compare the first character of your reply string with a single-character char literal.

    (As usual for "string" read "null-terminated sequence of chars.)