2 Copyright (c) 2005, David M Howard (daveh at dmh2000.com)
5 This product is licensed for use and distribution under the BSD Open Source License.
6 see the file COPYING for more details.
8 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
9 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
12 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
13 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
14 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
15 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
16 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
17 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
18 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 ========================================================================================================
24 EXAMPLE : SETUP FOR GGA AND RMC SENTENCES WITH SERIAL IO FOR WIN32
25 =======================================================================================================
27 #define WIN32_LEAN_AND_MEAN
34 /** open a serial port */
35 static HANDLE openPort(const char *port,int baud)
44 GENERIC_READ | GENERIC_WRITE,
50 if (h == INVALID_HANDLE_VALUE) {
56 /* read current configuration */
57 status = GetCommState(h,&dcb);
60 return INVALID_HANDLE_VALUE;
63 /* set the baud rate and other parameters */
66 dcb.Parity = NOPARITY;
67 dcb.StopBits = ONESTOPBIT;
69 /* set configuration */
70 status = SetCommState(h, &dcb);
73 return INVALID_HANDLE_VALUE;
76 /* read timeout configuration */
77 status = GetCommTimeouts(h,&tmo);
80 return INVALID_HANDLE_VALUE;
83 /* set to indefinite blocking */
84 tmo.ReadIntervalTimeout = 0;
85 tmo.ReadTotalTimeoutConstant = 0;
86 tmo.ReadTotalTimeoutMultiplier = 0;
87 status = SetCommTimeouts(h,&tmo);
90 return INVALID_HANDLE_VALUE;
96 /** read a byte (blocking) */
97 static int readPort(HANDLE h)
102 status = ReadFile(h,&ch,1,&count,0);
111 static void closePort(HANDLE h)
117 /** print current data */
118 static void printGps(nmeap_gga_t *gga,nmeap_rmc_t *rmc)
120 printf("%lu %lu %.6f %.6f %.0f %f %f %d %d\n",
133 /* ---------------------------------------------------------------------------------------*/
134 /* STEP 1 : allocate the data structures. be careful if you put them on the stack because */
135 /* they need to be live for the duration of the parser */
136 /* ---------------------------------------------------------------------------------------*/
137 static nmeap_context_t nmea; /* parser context */
138 static nmeap_gga_t gga; /* this is where the data from GGA messages will show up */
139 static nmeap_rmc_t rmc; /* this is where the data from RMC messages will show up */
140 static int user_data; /* user can pass in anything. typically it will be a pointer to some user data */
142 int main(int argc,char *argv[])
150 /* require both arguments */
152 printf("%s <comport> <baud>\n",argv[0]);
156 /* serial port argument */
159 /* baud rate argument */
160 status = sscanf(argv[2],"%d",&baud);
162 printf("%s <comport> <baud>\n",argv[0]);
163 printf("invalid <baud> : %s\n",argv[2]);
167 /** open the serial port */
168 h = openPort(port,baud);
169 if (h == INVALID_HANDLE_VALUE) {
170 printf("can't open port : %s\n",port);
174 /* ---------------------------------------*/
175 /*STEP 2 : initialize the nmea context */
176 /* ---------------------------------------*/
177 status = nmeap_init(&nmea,(void *)&user_data);
179 printf("nmeap_init %d\n",status);
183 /* ---------------------------------------*/
184 /*STEP 3 : add standard GPGGA parser */
185 /* -------------------------------------- */
186 status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,0,&gga);
188 printf("nmeap_add %d\n",status);
192 /* ---------------------------------------*/
193 /*STEP 4 : add standard GPRMC parser */
194 /* -------------------------------------- */
195 status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,0,&rmc);
197 printf("nmeap_add %d\n",status);
201 /* ---------------------------------------*/
202 /*STEP 5 : process input until done */
203 /* -------------------------------------- */
205 /* ---------------------------------------*/
206 /*STEP 6 : get a byte at a time */
207 /* -------------------------------------- */
213 /* --------------------------------------- */
214 /*STEP 7 : pass it to the parser */
215 /* status indicates whether a complete msg */
216 /* arrived for this byte */
217 /* NOTE : in addition to the return status */
218 /* the message callout will be fired when */
219 /* a complete message is processed */
220 /* --------------------------------------- */
221 status = nmeap_parse(&nmea,ch);
223 /* ---------------------------------------*/
224 /*STEP 8 : process the return code */
225 /* -------------------------------------- */
228 /* GOT A GPGGA MESSAGE */
232 /* GOT A GPRMC MESSAGE */