简体   繁体   中英

How to compare an array of char with a string

My program is to receive data from Zigbee and to filter it to get what i wanted.

unsigned char idata buff[100];            //To read data from rawrxd[] and process data
unsigned char count=0;                    //To store counter for rawrxd[]
unsigned char buff_count=0;               //store counter for buff[], read counter for rawrxd[]
if(buff_count!=count)                   //checking Is there any unread data?
    {
        if(buff_count==100)                 //go back to start position of array
        buff_count=0;

        buff[buff_count] = rawrxd[buff_count];  //read the data

        if(strcmp(buff, "UCAST:000D6F0000A9BBD8,06=!221~@") ==0)
        {
        ES0=0;
        Serial_txString("AT+UCAST:000D6F0000A9BBD8=!222~@");
        tx(0x0D);
        tx(0x0A);
        ES0=1;
        }

        if(strcmp(buff, "UCAST:000D6F0000A9BBD8,06=!221#@") ==0)
        {
        ES0=0;
        Serial_txString("AT+UCAST:000D6F0000A9BBD8=!222#@");  
        tx(0x0D);
        tx(0x0A);
        ES0=1;
        }

        buff_count++;                           //increase the read_count
    }

This is how it should be the buffer will receive the UCAST and then compare the it with the string if it is the same, return 0. However, it only compares one time and after that I received the next UCAST it does not compare at all.

Also, the first time it compare must be the same in order to work. If received the wrong char and then received the correct char it will not work. From this, is it the pointer problem? Since my buffer is an array of char and I trying to compare it with a string.

The problem is that buff[] has no NUL character before doing the string comparisons. So strcmp() does not sense the end of received string where you think it should (instead seeing whatever was left in the array before) and so it never matches.

     buff [buff_count] = rawrxd[buff_count];  //read the data
     buff [++buff_count] =  '\000';       /*  you need to add this */

With that addition, the strcmp() s have a chance of working, if all else is well.

Also, I am pretty sure you can neaten and optimize the code sequence

    Serial_txString("AT+UCAST:000D6F0000A9BBD8=!222~@");
    tx(0x0D);
    tx(0x0A);

into

    Serial_txString("AT+UCAST:000D6F0000A9BBD8=!222~@\x0d\x0a");

By assigning the way you have:

buff[buff_count] = rawrxd[buff_count];

you are merely assigning the first element of the rawrxd buffer to buff variable. It is a variable assignment, not a pointer assignment.

Since, both have the same buffer size, just equal the two base pointers like this:

buff = rawrxd;

and then do a strcmp(), or better, use strncmp() since the number of bytes(elements) are fixed.

Hope it helps.

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM