1

I'm working in a project that use GPS data. I'm trying to get all the data but I've had a lot of problems. The secuence that I recieve is:

+CGNSINF: <GNSS run status>,<Fix status >,<UTC date &
Time>,<Latitude>,<Longitude>,<MSLAltitude>,<SpeedOverGround>,
<CourseOverGround>,<FixMode>,<Reserved1>,<HDOP>,<PDOP>,<VDOP>,<Reserved2>,
<GNSSSatellites in View>,<GPS Satellites Used>,
<GLONASS SatellitesUsed>,<Reserved3>,<C/N0 max>,<HPA>,<VPA>

I have decided to use strsep because it works with the empty fields of the GPS data. With strsep I can get the data correctly if it's positive. In my case the latitude is negative and with strsep I get a 0 value.

The code that I've done to get the data that I need is:


char *string = aux_str_gps;
char *token;
int  counter = 0;

float  gps_date;
float  latitude;
float  longitude;
float  altitude;
float  speed;
float  COG; //course over ground in degrees
float  HDOP;
int    GNSS_sat_used; //numer of GNSS satellites used

while ((token = strsep(&string, ",")) != NULL){
        switch (counter){
        case 2: 
            sscanf(token, "%f", &gps_date);
            write_gps_date(gps_date);
            counter ++;
            break;
        case 3: 
            sscanf(token, "%f", &latitude);
            write_gps_lat(latitude);
            counter ++;
            break;
        case 4: 
            sscanf(token, "%f", &longitude);
            write_gps_long(longitude);
            counter ++;
            break;
        case 5: 
            sscanf(token, "%f", &altitude);
            write_gps_alt(altitude);
            counter ++;
            break;
        case 6: 
            sscanf(token, "%f", &speed);
            write_gps_speed(speed);
            counter ++;
            break;
        case 7: 
            sscanf(token, "%f", &COG);
            write_gps_COG(COG);
            counter ++;
            break;
        case 10: 
            sscanf(token, "%f", &HDOP);
            write_gps_HDOP(HDOP);
            counter ++;
            break;
        case 15: 
            sscanf(token, "%d", &GNSS_sat_used);
            write_gps_GNSS_used(GNSS_sat_used);
            counter ++;
            break;
        default: 
            counter ++;
        }
    }

If you got any suggest is welcomed.

Javier C
  • 85
  • 1
  • 7
  • 1
    "Strsep() return 0 with negative values" --> Not quite. `strsep()` returns a `char *`. The text it points to might be `"-1.234"`. Code never checks the return value of `scanf()` so the assertion "latitude is negative" is not backed up in code. – chux - Reinstate Monica May 05 '20 at 10:47
  • "In my case the latitude is negative and with strsep I get a 0 value." --> post a sample printing of `aux_str_gps`. – chux - Reinstate Monica May 05 '20 at 10:48
  • 1
    Knowing that `aux_str_gps` is writable is important for `strsep()`. Is it? – chux - Reinstate Monica May 05 '20 at 10:54
  • Welcome to SO. Please provide a complete example. Also tell us what input do you provide and what values for `token` do you get? – Gerhardh May 05 '20 at 11:04
  • You are right, the token is correct and longitude too. The problem is in write_gps_log, but I don't understand why the value changes to 0. In that function I only do this: longitude_b[wr_buff] =(uint32_t) (new_longitude * 1000000); So if I'm not wrong, change to uint32_t shouldn't change the value to 0 – Javier C May 05 '20 at 11:17

0 Answers0