-5

I am currently writing a program that reads in GPS input and writes the input to an array. inChar2 is getchar for UART2. It loops through the string until it finds the characters $GPPGA line and then writes the longitude and latitude to their respective arrays. I then want to compare longitude & check_longitude and latitude & check_latitude and print out one thing if they match and another thing if they don't. However, my code is not comparing the strings and only prints out "no match". Where am I going wrong? I also have written a function called accum that takes all the GPS input and stores it in a giant string. I don't think it's written properly, and I am not utilizing it in the ISR function.This program is being written in 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);
}
WedaPashi
  • 3,561
  • 26
  • 42

1 Answers1

0

Here are some notes after reviewing the code you posted:

Add the strings that you are comparing to your "no match" printf in case you get something reasonable that is not an exact match (ie. "3326.51758" != "3326.51756" even though it still seems reasonable due to jitter).

your unsigned char temp is passed to accum without being initialized, so its stack garbage and that accum function will probably not work as expected.

your is_in flag could be made a unsigned char to save 1-3 bytes, not really a big deal but hey every byte counts.

check_long/check_lat should be made const globals so you dont re-allocate them on the ISR stack each call and more importantly because they are "read only" ie const.

If you are sure that the "$" symbol indicates the start of a new GPS input you can/should use the "$" to enter a state machine that reads the rest of the GPPDA tag, similar to your nested ifs but probably switch fallthroughs are more readable (or loop over switch if you hate fallthroughs even when they're commented)

I assume inChar2 just reads from a register, since this looks like its a PIC its probably something like U2RXREG. I would avoid adding so many of these inChar2 calls and just read from the register directly.

You dont need to for loop at the bottom to set all those variables to 0, they should just get popped off the stack and be re-initalized to zero the next time you get serial/UART traffic through this ISR.

At the end of the day, you dont really want to store double values as a string (you want them as doubles!) If you want to test your input store the lat/long values as doubles then do a within_epsilon compare of your test values and the input. This may prove helpful for you, GL Converting char* to float or double

Bwebb
  • 675
  • 4
  • 14