简体   繁体   English

如何将char数组与字符串进行比较

[英]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. 我的程序是从Zigbee接收数据并对其进行过滤以获得我想要的。

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. 这就是缓冲区应该接收UCAST,然后将其与字符串进行比较的方式,如果相同,则返回0。但是,它只比较一次,而在我收到下一个UCAST之后,它根本不比较。

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. 由于我的缓冲区是char数组,因此我尝试将其与字符串进行比较。

The problem is that buff[] has no NUL character before doing the string comparisons. 问题是buff[]在进行字符串比较之前没有NUL字符。 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. 因此, strcmp()不会在您认为应该接收的字符串的末尾感知(而是查看之前数组中剩余的内容),因此它永远不会匹配。

     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. 如果所有其他条件都很好,那么添加了strcmp()就有机会工作。

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. 您只是将rawrxd缓冲区的第一个元素分配给buff变量。 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. 然后执行一个strcmp()或更好的方法,因为字节(元素)的数量是固定的,所以请使用strncmp()。

Hope it helps. 希望能帮助到你。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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