Main Page | Data Structures | Directories | File List | Data Fields | Globals

nmea.c

Go to the documentation of this file.
00001 #include <config.h>
00002 
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <math.h>
00007 
00008 #include <cartcomm/nmea.h>
00009 #include "nan.h"
00010 
00012 
00016 double nmea_to_deg(char *nmea) {
00017     int b;
00018     char min[8] = {0};
00019     double dmin;
00020     char deg[5] = {0};
00021     int ideg;
00022     double ddeg;
00023 
00024     // Position of the first minute digit (zero-based)
00025     // in the nmea string
00026     b = strlen(nmea) - 8;
00027 
00028     if(b < 0)
00029         return NAN; // return NAN value on invalid input
00030 
00031     // Extract the minutes as a string, convert to decimal
00032     strncpy(min, nmea + b, sizeof(min));
00033     min[sizeof(min)-1] = '\0';
00034     dmin = strtod(min, NULL);
00035 
00036     // Extract the degrees as a string, convert to integer
00037     if(b > sizeof(deg))
00038         return (0.0/0.0);
00039     strncpy(deg, nmea, b);
00040     deg[sizeof(deg)-1] = '\0';
00041     ideg = atoi(deg);
00042 
00043     // Combine degrees and minutes into decimal degrees
00044     ddeg = abs(ideg) + dmin / 60.0; // add absolute values
00045     if(ideg < 0)
00046         ddeg = -ddeg;   // assign the sign of the degrees
00047 
00048     return ddeg;
00049 }
00050 
00052 
00058 void deg_to_nmea(double ddeg, char *nmea, int nmeasize) {
00059     int ideg;
00060     double dmin;
00061 
00062     // Get the whole degrees as integer
00063     ideg = (int) abs(ddeg);
00064     if(ddeg < 0.0)
00065         ideg = -ideg;
00066 
00067     // Convert the remaining fractional degrees into minutes
00068     dmin = 60.0 * fabs(ddeg - ideg);
00069 
00070     // Write out the NMEA-formatted string
00071     snprintf(nmea, nmeasize, "%d%08.5f", ideg, dmin);
00072 }

Generated on Thu Sep 6 13:13:10 2007 for driver by  doxygen 1.3.9.1