C - 循环,输入未正确读取,仅打印出 "no match"

标签 c

我目前正在编写一个程序,该程序读取 GPS 输入并将输入写入数组。 inChar2 是 UART2 的 getchar。它循环遍历字符串,直到找到字符 $GPPGA 行,然后将经度和纬度写入各自的数组。然后,我想比较经度和 check_longitude 以及纬度和 check_latitude,如果匹配则打印出一件事,如果不匹配则打印另一件事。但是,我的代码不比较字符串,只打印出“不匹配”。我哪里出错了?我还编写了一个名为 Accum 的函数,它获取所有 GPS 输入并将其存储在一个巨大的字符串中。我认为它写得不正确,并且我没有在 ISR 函数中使用它。该程序是在 MPLAB X IDE 中编写的。

    //accumulate variables
void accum(char c) 
{
    static char array[1000];
    static char *end = array;
    *end++ = c;
    if (c == "\n") 
    {
        *end++ = 0; 
        end = array;
    }
}

void _ISR _U2RXInterrupt() 
{
    //setting up the variables used
    unsigned char incomer_data = 0;
    unsigned char longitude[13] = {};
    unsigned char latitude[13] = {};
    unsigned char array_count = 0;

    unsigned char temp;

    int is_in = 0;

    //$GPGGA,224355.00,3326.51776,N,08849.74254,W,2,09,1.02,104.9,M,-29.5,M,,0000*64

    unsigned char check_long[13] =
    {   //"3327.12050,N", //sidewalk 
        "3326.51776",
    };
    unsigned char check_lat[13] = 
    {
        //"08847.25265W",
        "08849.74254",
    };

    accum(temp);
    // printf("%c", temp);
    //char c = inChar2();
    //outChar(c);
    while (1)
    {
        incomer_data = inChar2();      //get character, checking string $GPGGA
        //outChar(incomer_data);

        //step by step find the GPGGA line
        if (incomer_data == '$') 
        { 
            //first statement of GPS data starts with $
            incomer_data = inChar2(); //if the first IF becomes true then next phase
            //    outChar(incomer_data);

            if (incomer_data == 'G') 
            {
                incomer_data = inChar2();
                //outChar(incomer_data);

                if (incomer_data == 'P') 
                {
                    incomer_data = inChar2();
                    //outChar(incomer_data);

                    if (incomer_data == 'G') 
                    {
                        incomer_data = inChar2();    
                        //outChar(incomer_data);

                        if (incomer_data == 'G') 
                        {
                            incomer_data = inChar2();
                            //outChar(incomer_data);

                            if (incomer_data == 'A')
                            {
                                incomer_data = inChar2();
                                //outChar(incomer_data);

                                if (incomer_data == ',')
                                { 
                                    // first ',' received  
                                    incomer_data = inChar2(); //at this stage Final check in done. GPGGA is found
                                    //outChar(incomer_data);
                                    while (incomer_data != ',') 
                                    { //skipping GMT time
                                        incomer_data = inChar2();
                                        //outChar(incomer_data);
                                    }
                                    incomer_data = inChar2();
                                    latitude[0] = incomer_data;
                                    //outChar(incomer_data);
                                    // printf("working\n");

                                    while (incomer_data != ',') 
                                    {
                                        //printf("hi\n");
                                        for(array_count = 1; incomer_data != 'N'; array_count++) 
                                        {
                                            incomer_data = inChar2();
                                            latitude[array_count] = incomer_data; //Store Latitude data
                                            outChar(incomer_data);
                                            //printf("latitude data");
                                        }
                                        //printf("hi");
                                        incomer_data = inChar2();
                                        if (incomer_data == ',') 
                                        {
                                            for(array_count = 0; (incomer_data != 'E'); array_count++) 
                                            {
                                                incomer_data = inChar2();
                                                longitude[array_count] = incomer_data; //store Longitude data
                                                outChar(incomer_data);
                                                //printf("longitude data");
                                            }
                                        }

                                        int i = 0;
                                        for (i = 0; i < 13; ++i) 
                                        {
                                            if ((strcmp(check_long, longitude) == 0) & (strcmp(check_lat, latitude) == 0)) {
                                                printf("hot, sidewalk, bright, sunny, weird\n");
                                                is_in = 1;
                                            }
                                        }

                                        if (is_in == 0) 
                                        { 
                                            printf("no match");
                                        }
                                        array_count = 0;
                                        while (array_count < 12) { //array of latitude data is 11 digit
                                            //print data 
                                            array_count++;
                                        }
                                        array_count = 0;
                                        while (array_count < 13) { //array of longitude data is 12 digit
                                            //print data
                                            array_count++;
                                        }
                                        outChar(incomer_data); //prints out E
                                    }
                                }
                            }     
                        }
                    }
                }
            }
            for(array_count = 0; array_count <= 13; array_count++)
            {
                incomer_data = 0;
                latitude[array_count] = 0;
                longitude[array_count] = 0;
            }
            array_count = 0;
        }
    }  

    //DELAY_MS(3000);
}

最佳答案

查看您发布的代码后,以下是一些注释:

添加您要与“不匹配”printf 进行比较的字符串,以防您得到一些合理但不完全匹配的字符串(即“3326.51758”!=“3326.51756”,即使由于抖动它看起来仍然合理) .

您的无符号字符临时值未经初始化就传递给了accum,因此它的堆栈垃圾和accum函数可能无法按预期工作。

你的 is_in 标志可以做成一个 unsigned char 来保存 1-3 个字节,这并不是什么大问题,但是嘿每个字节都很重要。

check_long/check_lat 应该设置为 const 全局变量,这样您就不必在每次调用时在 ISR 堆栈上重新分配它们,更重要的是因为它们是“只读”的,即 const。

如果您确定“$”符号指示新 GPS 输入的开始,您可以/应该使用“$”进入读取 GPPDA 标记其余部分的状态机,类似于嵌套的 if,但是可能 switch Fallthroughs 更具可读性(或者循环切换,如果你讨厌 Fallthroughs,即使它们被注释了)

我假设 inChar2 只是从寄存器中读取,因为这看起来像它的 PIC,它可能类似于 U2RXREG。我会避免添加如此多的 inChar2 调用,而直接从寄存器中读取。

您不需要在底部的 for 循环将所有这些变量设置为 0,它们应该只是从堆栈中弹出,并在下次通过此 ISR 获得串行/UART 流量时重新初始化为零。

归根结底,您并不真的希望将 double 值存储为字符串(您希望将它们存储为 double 值!)如果您想测试输入,将纬度/经度值存储为 double 值,然后进行 inside_epsilon 比较您的测试值和输入。这可能对你有帮助,GL Converting char* to float or double

关于C - 循环,输入未正确读取,仅打印出 "no match",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51716987/

相关文章:

c++ - 对 `mysql_init' 的 undefined reference

c - 段错误 : 11 on c program

c - 递归如何用于二叉搜索树算法

c - 以相反顺序打印数组指针值

win32 COORD 结构的编译器错误?

c - Apple Mach-O 链接 (Id) 错误

c - Malloc、strlen、strcat

c - 当我尝试创建 10^5 节点图时,Malloc 失败了,较差的数字工作正常

c - 斐波那契数列的段错误

c - 在多线程程序中执行另一个程序