00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #define WIN32_LEAN_AND_MEAN
00028 #include <windows.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include "nmeap.h"
00033
00035 static HANDLE openPort(const char *port,int baud)
00036 {
00037 HANDLE h;
00038 DCB dcb;
00039 COMMTIMEOUTS tmo;
00040 int status;
00041
00042
00043 h = CreateFile( port,
00044 GENERIC_READ | GENERIC_WRITE,
00045 0,
00046 0,
00047 OPEN_EXISTING,
00048 0,
00049 0);
00050 if (h == INVALID_HANDLE_VALUE) {
00051
00052 return h;
00053 }
00054
00055
00056
00057 status = GetCommState(h,&dcb);
00058 if (status == 0) {
00059 CloseHandle(h);
00060 return INVALID_HANDLE_VALUE;
00061 }
00062
00063
00064 dcb.BaudRate = baud;
00065 dcb.ByteSize = 8;
00066 dcb.Parity = NOPARITY;
00067 dcb.StopBits = ONESTOPBIT;
00068
00069
00070 status = SetCommState(h, &dcb);
00071 if (status == 0) {
00072 CloseHandle(h);
00073 return INVALID_HANDLE_VALUE;
00074 }
00075
00076
00077 status = GetCommTimeouts(h,&tmo);
00078 if (status == 0) {
00079 CloseHandle(h);
00080 return INVALID_HANDLE_VALUE;
00081 }
00082
00083
00084 tmo.ReadIntervalTimeout = 0;
00085 tmo.ReadTotalTimeoutConstant = 0;
00086 tmo.ReadTotalTimeoutMultiplier = 0;
00087 status = SetCommTimeouts(h,&tmo);
00088 if (status == 0) {
00089 CloseHandle(h);
00090 return INVALID_HANDLE_VALUE;
00091 }
00092
00093 return h;
00094 }
00095
00097 static int readPort(HANDLE h)
00098 {
00099 BOOL status;
00100 char ch;
00101 DWORD count;
00102 status = ReadFile(h,&ch,1,&count,0);
00103 if (status == 0) {
00104 return -1;
00105 }
00106
00107 return (int)ch;
00108 }
00109
00110
00111 static void closePort(HANDLE h)
00112 {
00113 CloseHandle(h);
00114 }
00115
00116
00118 static void printGps(nmeap_gga_t *gga,nmeap_rmc_t *rmc)
00119 {
00120 printf("%lu %lu %.6f %.6f %.0f %f %f %d %d\n",
00121 gga->time,
00122 rmc->date,
00123 gga->latitude ,
00124 gga->longitude,
00125 gga->altitude ,
00126 rmc->course,
00127 rmc->speed,
00128 gga->satellites,
00129 gga->quality
00130 );
00131 }
00132
00133
00134
00135
00136
00137 static nmeap_context_t nmea;
00138 static nmeap_gga_t gga;
00139 static nmeap_rmc_t rmc;
00140 static int user_data;
00141
00142 int main(int argc,char *argv[])
00143 {
00144 int status;
00145 char ch;
00146 const char *port;
00147 int baud;
00148 HANDLE h;
00149
00150
00151 if (argc != 3) {
00152 printf("%s <comport> <baud>\n",argv[0]);
00153 return 1;
00154 }
00155
00156
00157 port = argv[1];
00158
00159
00160 status = sscanf(argv[2],"%d",&baud);
00161 if (status != 1) {
00162 printf("%s <comport> <baud>\n",argv[0]);
00163 printf("invalid <baud> : %s\n",argv[2]);
00164 return 1;
00165 }
00166
00168 h = openPort(port,baud);
00169 if (h == INVALID_HANDLE_VALUE) {
00170 printf("can't open port : %s\n",port);
00171 return 1;
00172 }
00173
00174
00175
00176
00177 status = nmeap_init(&nmea,(void *)&user_data);
00178 if (status != 0) {
00179 printf("nmeap_init %d\n",status);
00180 exit(1);
00181 }
00182
00183
00184
00185
00186 status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,0,&gga);
00187 if (status != 0) {
00188 printf("nmeap_add %d\n",status);
00189 exit(1);
00190 }
00191
00192
00193
00194
00195 status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,0,&rmc);
00196 if (status != 0) {
00197 printf("nmeap_add %d\n",status);
00198 exit(1);
00199 }
00200
00201
00202
00203
00204 for(;;) {
00205
00206
00207
00208 ch = readPort(h);
00209 if (ch <= 0) {
00210 break;
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 status = nmeap_parse(&nmea,ch);
00222
00223
00224
00225
00226 switch(status) {
00227 case NMEAP_GPGGA:
00228
00229 printGps(&gga,&rmc);
00230 break;
00231 case NMEAP_GPRMC:
00232
00233 printGps(&gga,&rmc);
00234 break;
00235 default:
00236 break;
00237 }
00238 }
00239
00240
00241 closePort(h);
00242
00243 return 0;
00244 }