/** * parse a buffer of nmea data */ int nmeap_parseBuffer(nmeap_context_t *context,const char *buffer,int *length) { int i; int status; int rem; int tlen; tlen = *length; rem = *length; status = 0; /* for each byte in the buffer */ for(i=0;i<tlen;i++) { /* decrement remaining byte count */ rem--; /* parse the byte */ status = nmeap_parse(context,buffer[i]); if (status != 0) { /* message found or error */ break; } } /* return remaining byte count */ *length = rem; return status; }
void accept_char(char ch) { if (parser_initialized==0) { /* Looks like we forgot to initialize the parser. Do it now. */ if (init_parser()!=0) return; //Failed! } #ifdef ECHO_GPS putchar(ch); // For debugging... #endif if (ch == IGNORE_CHAR) { // fprintf(stderr, "Skipping\n"); return; // Ignore it! } if (ch == ACCEPT_CHAR) { // Insert a '\r' first nmeap_parse(&nmea, '\r'); } nmeap_parse(&nmea, ch); }
int main(int argc,char *argv[]) { int status; char ch; /* ---------------------------------------*/ /*STEP 5 : initialize the nmea context */ /* ---------------------------------------*/ status = nmeap_init(&nmea,(void *)&user_data); if (status != 0) { printf("nmeap_init %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 6 : add standard GPGGA parser */ /* (no callout this time) */ /* -------------------------------------- */ status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,0,&gga); if (status != 0) { printf("nmeap_add %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 7 : add standard GPRMC parser */ /* (no callout this time) */ /* -------------------------------------- */ status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,0,&rmc); if (status != 0) { printf("nmeap_add %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 8 : ADD THE CUSTOM PARSER */ /* with callout ) */ /* -------------------------------------- */ status = nmeap_addParser(&nmea,"PGRMF",custom_pgrmf,pgrmf_callout,&rmf); if (status != 0) { printf("nmeap_add %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 9 : process input until done */ /* -------------------------------------- */ for(;;) { /* ---------------------------------------*/ /*STEP 10: get a byte at a time */ /* -------------------------------------- */ ch = readchar(); if (ch <= 0) { break; } /* --------------------------------------- */ /*STEP 11: pass it to the parser */ /* status indicates whether a complete msg */ /* arrived for this byte */ /* NOTE : in addition to the return status */ /* the message callout will be fired when */ /* a complete message is processed */ /* --------------------------------------- */ status = nmeap_parse(&nmea,ch); /* ---------------------------------------*/ /*STEP 12 : process the return code */ /* -------------------------------------- */ switch(status) { case NMEAP_GPGGA: /* GOT A GPGGA MESSAGE */ printf("-------------switch\n"); printf("GPGGA\n"); printf("-------------\n"); break; case NMEAP_GPRMC: /* GOT A GPRMC MESSAGE */ printf("-------------switch\n"); printf("GPRMC\n"); printf("-------------\n"); break; case GARMIN_PGRMF: /* GOT A PGRMF MESSAGE */ printf("-------------switch\n"); print_pgrmf(&rmf); printf("-------------\n"); break; default: break; } } return 0; }
int main(int argc,char *argv[]) { int status; char ch; const char *port; int baud; HANDLE h; /* require both arguments */ if (argc != 3) { printf("%s <comport> <baud>\n",argv[0]); return 1; } /* serial port argument */ port = argv[1]; /* baud rate argument */ status = sscanf(argv[2],"%d",&baud); if (status != 1) { printf("%s <comport> <baud>\n",argv[0]); printf("invalid <baud> : %s\n",argv[2]); return 1; } /** open the serial port */ h = openPort(port,baud); if (h == INVALID_HANDLE_VALUE) { printf("can't open port : %s\n",port); return 1; } /* ---------------------------------------*/ /*STEP 2 : initialize the nmea context */ /* ---------------------------------------*/ status = nmeap_init(&nmea,(void *)&user_data); if (status != 0) { printf("nmeap_init %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 3 : add standard GPGGA parser */ /* -------------------------------------- */ status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,0,&gga); if (status != 0) { printf("nmeap_add %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 4 : add standard GPRMC parser */ /* -------------------------------------- */ status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,0,&rmc); if (status != 0) { printf("nmeap_add %d\n",status); exit(1); } /* ---------------------------------------*/ /*STEP 5 : process input until done */ /* -------------------------------------- */ for(;;) { /* ---------------------------------------*/ /*STEP 6 : get a byte at a time */ /* -------------------------------------- */ ch = readPort(h); if (ch <= 0) { break; } /* --------------------------------------- */ /*STEP 7 : pass it to the parser */ /* status indicates whether a complete msg */ /* arrived for this byte */ /* NOTE : in addition to the return status */ /* the message callout will be fired when */ /* a complete message is processed */ /* --------------------------------------- */ status = nmeap_parse(&nmea,ch); /* ---------------------------------------*/ /*STEP 8 : process the return code */ /* -------------------------------------- */ switch(status) { case NMEAP_GPGGA: /* GOT A GPGGA MESSAGE */ printGps(&gga,&rmc); break; case NMEAP_GPRMC: /* GOT A GPRMC MESSAGE */ printGps(&gga,&rmc); break; default: break; } } /* close and quit */ closePort(h); return 0; }