source: pacpussensors/trunk/NMEA0183/src/NMEA0183.cpp@ 103

Last change on this file since 103 was 59, checked in by DHERBOMEZ Gérald, 10 years ago

Integration of new modules:

  • GPS NMEA0183 decoder
  • Span CPT Decoder

Update of:

File size: 18.2 KB
Line 
1#include "nmea0183.h"
2#pragma hdrstop
3
4/*
5** Author: Samuel R. Blackburn
6** Internet: sam_blackburn@pobox.com
7**
8** You can use it any way you like as long as you don't try to sell it.
9**
10** Copyright, 1996, Samuel R. Blackburn
11**
12** $Workfile: nmea0183.cpp $
13** $Revision: 5 $
14** $Modtime: 10/10/98 4:27p $
15*/
16/*
17#ifdef _DEBUG
18#undef THIS_FILE
19static char BASED_CODE THIS_FILE[] = __FILE__;
20#define new DEBUG_NEW
21#endif
22*/
23NMEA0183::NMEA0183()
24{
25 //m_Initialize();
26
27 //m_ResponseTable.resize(m_ResponseTable.size()+1);
28 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Aam );
29 //m_ResponseTable.resize(m_ResponseTable.size()+1);
30 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Alm );
31 //m_ResponseTable.resize(m_ResponseTable.size()+1);
32 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Apb );
33 //m_ResponseTable.resize(m_ResponseTable.size()+1);
34 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Asd );
35 //m_ResponseTable.resize(m_ResponseTable.size()+1);
36 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Bec );
37 //m_ResponseTable.resize(m_ResponseTable.size()+1);
38 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Bod );
39 //m_ResponseTable.resize(m_ResponseTable.size()+1);
40 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Bwc );
41 //m_ResponseTable.resize(m_ResponseTable.size()+1);
42 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Bwr );
43 //m_ResponseTable.resize(m_ResponseTable.size()+1);
44 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Bww );
45 //m_ResponseTable.resize(m_ResponseTable.size()+1);
46 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Dbt );
47 //m_ResponseTable.resize(m_ResponseTable.size()+1);
48 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Dcn );
49 //m_ResponseTable.resize(m_ResponseTable.size()+1);
50 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Dpt );
51 //m_ResponseTable.resize(m_ResponseTable.size()+1);
52 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Fsi );
53 //m_ResponseTable.resize(m_ResponseTable.size()+1);
54 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gda ); // Sentence Not Recommended For New Designs
55 //m_ResponseTable.resize(m_ResponseTable.size()+1);
56 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gdf ); // Sentence Not Recommended For New Designs
57 //m_ResponseTable.resize(m_ResponseTable.size()+1);
58 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gdp ); // Sentence Not Recommended For New Designs
59 //m_ResponseTable.resize(m_ResponseTable.size()+1);
60 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gga );
61 //m_ResponseTable.resize(m_ResponseTable.size()+1);
62 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gla ); // Sentence Not Recommended For New Designs
63 //m_ResponseTable.resize(m_ResponseTable.size()+1);
64 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Glc );
65 //m_ResponseTable.resize(m_ResponseTable.size()+1);
66 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Glf ); // Sentence Not Recommended For New Designs
67
68 //m_ResponseTable.resize(m_ResponseTable.size()+1);
69 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gll );
70 //m_ResponseTable.resize(m_ResponseTable.size()+1);
71 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Glp ); // Sentence Not Recommended For New Designs
72 //m_ResponseTable.resize(m_ResponseTable.size()+1);
73 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gsa );
74 //m_ResponseTable.resize(m_ResponseTable.size()+1);
75 // ajout de GST
76 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gst );
77 //m_ResponseTable.resize(m_ResponseTable.size()+1);
78 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gsv );
79 //m_ResponseTable.resize(m_ResponseTable.size()+1);
80 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gtd ); // Sentence Not Recommended For New Designs
81 //m_ResponseTable.resize(m_ResponseTable.size()+1);
82 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gxa );
83 //m_ResponseTable.resize(m_ResponseTable.size()+1);
84 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gxf ); // Sentence Not Recommended For New Designs
85 //m_ResponseTable.resize(m_ResponseTable.size()+1);
86 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Gxp ); // Sentence Not Recommended For New Designs
87 //m_ResponseTable.resize(m_ResponseTable.size()+1);
88 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Hcc ); // Sentence Not Recommended For New Designs
89 //m_ResponseTable.resize(m_ResponseTable.size()+1);
90 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Hdg );
91
92 //m_ResponseTable.resize(m_ResponseTable.size()+1);
93 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Hdm ); // Sentence Not Recommended For New Designs
94 //m_ResponseTable.resize(m_ResponseTable.size()+1);
95 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Hdt );
96 //m_ResponseTable.resize(m_ResponseTable.size()+1);
97 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Hsc );
98 //m_ResponseTable.resize(m_ResponseTable.size()+1);
99 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Ima ); // Sentence Not Recommended For New Designs
100 //m_ResponseTable.resize(m_ResponseTable.size()+1);
101 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Lcd );
102 //m_ResponseTable.resize(m_ResponseTable.size()+1);
103 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Mhu ); // Sentence Not Recommended For New Designs
104 //m_ResponseTable.resize(m_ResponseTable.size()+1);
105 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Mta ); // Sentence Not Recommended For New Designs
106 //m_ResponseTable.resize(m_ResponseTable.size()+1);
107 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Mtw );
108 //m_ResponseTable.resize(m_ResponseTable.size()+1);
109 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Mwv );
110 //m_ResponseTable.resize(m_ResponseTable.size()+1);
111 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Oln );
112
113 //m_ResponseTable.resize(m_ResponseTable.size()+1);
114 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Osd );
115 //m_ResponseTable.resize(m_ResponseTable.size()+1);
116 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Proprietary );
117 //m_ResponseTable.resize(m_ResponseTable.size()+1);
118 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Rma );
119 //m_ResponseTable.resize(m_ResponseTable.size()+1);
120 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Rmb );
121 //m_ResponseTable.resize(m_ResponseTable.size()+1);
122 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Rmc );
123 //m_ResponseTable.resize(m_ResponseTable.size()+1);
124 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Rot );
125 //m_ResponseTable.resize(m_ResponseTable.size()+1);
126 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Rpm );
127 //m_ResponseTable.resize(m_ResponseTable.size()+1);
128 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Rsa );
129 //m_ResponseTable.resize(m_ResponseTable.size()+1);
130 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Rsd );
131 //m_ResponseTable.resize(m_ResponseTable.size()+1);
132 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Rte );
133
134 //m_ResponseTable.resize(m_ResponseTable.size()+1);
135 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Sfi );
136 //m_ResponseTable.resize(m_ResponseTable.size()+1);
137 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Stn );
138 //m_ResponseTable.resize(m_ResponseTable.size()+1);
139 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Tep ); // Sentence Not Recommended For New Designs
140 //m_ResponseTable.resize(m_ResponseTable.size()+1);
141 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Trf );
142 //m_ResponseTable.resize(m_ResponseTable.size()+1);
143 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Ttm );
144 //m_ResponseTable.resize(m_ResponseTable.size()+1);
145 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Vbw );
146 //m_ResponseTable.resize(m_ResponseTable.size()+1);
147 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Vdr );
148 //m_ResponseTable.resize(m_ResponseTable.size()+1);
149 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Vhw );
150 //m_ResponseTable.resize(m_ResponseTable.size()+1);
151 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Vlw );
152 //m_ResponseTable.resize(m_ResponseTable.size()+1);
153 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Vpw );
154
155 //m_ResponseTable.resize(m_ResponseTable.size()+1);
156 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Vtg );
157 //m_ResponseTable.resize(m_ResponseTable.size()+1);
158 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Vwe );
159 //m_ResponseTable.resize(m_ResponseTable.size()+1);
160 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Wcv ); // Sentence Not Recommended For New Designs
161 //m_ResponseTable.resize(m_ResponseTable.size()+1);
162 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Wdc ); // Sentence Not Recommended For New Designs
163 //m_ResponseTable.resize(m_ResponseTable.size()+1);
164 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Wdr ); // Sentence Not Recommended For New Designs
165 //m_ResponseTable.resize(m_ResponseTable.size()+1);
166 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Wnc );
167 //m_ResponseTable.resize(m_ResponseTable.size()+1);
168 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Wpl );
169 //m_ResponseTable.resize(m_ResponseTable.size()+1);
170 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Xdr );
171 //m_ResponseTable.resize(m_ResponseTable.size()+1);
172 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Xte );
173 //m_ResponseTable.resize(m_ResponseTable.size()+1);
174 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Xtr );
175
176 //m_ResponseTable.resize(m_ResponseTable.size()+1);
177 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Zda );
178 //m_ResponseTable.resize(m_ResponseTable.size()+1);
179 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Zfi ); // Sentence Not Recommended For New Designs
180 //m_ResponseTable.resize(m_ResponseTable.size()+1);
181 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Zfo );
182 //m_ResponseTable.resize(m_ResponseTable.size()+1);
183 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Zlz ); // Sentence Not Recommended For New Designs
184 //m_ResponseTable.resize(m_ResponseTable.size()+1);
185 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Zpi ); // Sentence Not Recommended For New Designs
186 //m_ResponseTable.resize(m_ResponseTable.size()+1);
187 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Zta ); // Sentence Not Recommended For New Designs
188 //m_ResponseTable.resize(m_ResponseTable.size()+1);
189 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Zte ); // Sentence Not Recommended For New Designs
190 //m_ResponseTable.resize(m_ResponseTable.size()+1);
191 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Ztg );
192 //m_ResponseTable.resize(m_ResponseTable.size()+1);
193 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Zti ); // Sentence Not Recommended For New Designs
194 //m_ResponseTable.resize(m_ResponseTable.size()+1);
195 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Zwp ); // Sentence Not Recommended For New Designs
196 //m_ResponseTable.resize(m_ResponseTable.size()+1);
197 m_ResponseTable.insert(m_ResponseTable.size(), (RESPONSE *) &Zzu ); // Sentence Not Recommended For New Designs
198
199 m_SortResponseTable();
200 m_SetContainerPointers();
201}
202
203NMEA0183::~NMEA0183()
204{
205 //m_Initialize();
206}
207
208void NMEA0183::m_Initialize( void )
209{
210 //ErrorMessage.Empty();
211}
212
213void NMEA0183::m_SetContainerPointers( void )
214{
215 int index = 0;
216 int number_of_entries_in_table = m_ResponseTable.size();
217
218 RESPONSE *this_response = (RESPONSE *) NULL;
219
220 index = 0;
221
222 while( index < number_of_entries_in_table )
223 {
224 this_response = (RESPONSE *) m_ResponseTable[ index ];
225
226 this_response->SetContainer( this );
227
228 index++;
229 }
230}
231
232void NMEA0183::m_SortResponseTable( void )
233{
234 int index = 0;
235 int number_of_entries_in_table = m_ResponseTable.size() - 1;
236
237 RESPONSE *this_response = (RESPONSE *) NULL;
238 RESPONSE *that_response = (RESPONSE *) NULL;
239
240 BOOL sorted = FALSE;
241
242 while( sorted == FALSE )
243 {
244 sorted = TRUE;
245
246 index = 0;
247
248 while( index < number_of_entries_in_table )
249 {
250 this_response = (RESPONSE *) m_ResponseTable[ index ];
251 that_response = (RESPONSE *) m_ResponseTable[ index + 1 ];
252 //that_response = (RESPONSE *) m_ResponseTable.at(index + 1);
253
254 if ( this_response->Mnemonic.compare( that_response->Mnemonic ) > 0 )
255 {
256 //m_ResponseTable[ index ] = that_response;
257 //m_ResponseTable[ index + 1 ] = this_response;
258 //m_ResponseTable.insert ( index ,that_response );
259 m_ResponseTable.replace ( index ,that_response );
260 //m_ResponseTable.insert ( index+1 ,this_response );
261 m_ResponseTable.replace ( index+1 ,this_response );
262
263 sorted = FALSE;
264 }
265
266 index++;
267 }
268 }
269}
270
271/*
272** Public Interface
273*/
274
275BOOL NMEA0183::IsGood( void ) const
276{
277 /*
278 ** NMEA 0183 sentences begin with $ and and with CR LF
279 */
280
281 if ( m_Sentence.Sentence[ 0 ] != '$' )
282 {
283 return( FALSE );
284 }
285
286 /*
287 ** Next to last character must be a CR
288 */
289// char character[1];
290// QString str = (QString) m_Sentence.Sentence.mid( m_Sentence.Sentence.length() - 2, 1 );
291// character[0] = str.toLatin1();
292
293 if ( m_Sentence.Sentence[m_Sentence.Sentence.length() - 2].toLatin1() != CARRIAGE_RETURN )
294 {
295 return( FALSE );
296 }
297
298 if ( m_Sentence.Sentence[m_Sentence.Sentence.length() - 1].toLatin1() != LINE_FEED )
299 {
300 return( FALSE );
301 }
302
303 return( TRUE );
304}
305
306BOOL NMEA0183::Parse( void )
307{
308 BOOL return_value = FALSE;
309
310 if ( IsGood() )
311 {
312 int index = 0;
313 int comparison = 0;
314 int drop_dead = 0;
315 int exit_loop = 0;
316 int lower_limit = 0;
317 int upper_limit = 0;
318
319 QString mnemonic;
320
321 RESPONSE *response_p = (RESPONSE *) NULL;
322
323 mnemonic = m_Sentence.Field( 0 );
324
325 /*
326 ** See if this is a proprietary field
327 */
328
329 if ( mnemonic[0].toLatin1() == 'P' )
330 {
331 mnemonic = "P";
332 }
333 else
334 {
335 mnemonic = mnemonic.right( 3 );
336 }
337
338 /*
339 ** Set up our default error message
340 */
341
342 ErrorMessage = mnemonic;
343 ErrorMessage += " is an unknown type of sentence";
344
345 LastSentenceIDReceived = mnemonic;
346
347 /*
348 ** Do a Binary Search to call the appropriate function
349 */
350
351 lower_limit = 0;
352 upper_limit = m_ResponseTable.size();
353 index = upper_limit / 2;
354 drop_dead = ( index < 10 ) ? 10 : index + 2;
355
356 /*
357 ** The drop_dead is here as an insurance policy that we will never get stuck in an endless loop.
358 ** I have encountered situations where the inaccuracy of the division leaves the loop stuck when
359 ** it can't find something.
360 */
361
362 while( exit_loop == 0 )
363 {
364 response_p = (RESPONSE *) m_ResponseTable[ index ];
365
366 comparison = mnemonic.compare( response_p->Mnemonic );
367
368 if ( comparison == 0 )
369 {
370 return_value = response_p->Parse( m_Sentence );
371
372 /*
373 ** Set your ErrorMessage
374 */
375
376 if ( return_value == TRUE )
377 {
378 /*
379 ** Now that we sucessfully parsed a sentence, record stuff *about* the transaction
380 */
381
382 ErrorMessage = "No Error";
383 LastSentenceIDParsed = response_p->Mnemonic;
384 TalkerID = talker_id( m_Sentence );
385 ExpandedTalkerID = expand_talker_id( TalkerID );
386 PlainText = response_p->PlainEnglish();
387 }
388 else
389 {
390 ErrorMessage = response_p->ErrorMessage;
391 }
392
393 exit_loop = 1;
394 }
395 else
396 {
397 if ( comparison < 0 )
398 {
399 upper_limit = index;
400 }
401 else
402 {
403 lower_limit = index;
404 }
405
406 if ( lower_limit == upper_limit )
407 {
408 exit_loop = 1;
409 return_value = FALSE;
410 }
411 else
412 {
413 index = ( lower_limit + upper_limit ) / 2;
414 }
415 }
416
417 drop_dead--;
418
419 if ( exit_loop != 1 && drop_dead < 0 )
420 {
421 exit_loop = 1;
422 return_value = FALSE;
423 }
424 }
425 }
426 else
427 {
428 return_value = FALSE;
429 }
430
431 return( return_value );
432}
433/*
434#if 0
435
436void NMEA0183::Serialize( CArchive& archive )
437{
438 CObject::Serialize( archive );
439
440 if ( archive.IsStoring() )
441 {
442 archive << m_Sentence;
443 archive << ErrorMessage;
444 archive << LastSentenceIDParsed;
445 archive << LastSentenceIDReceived;
446 archive << PlainText;
447 archive << TalkerID;
448 archive << ExpandedTalkerID;
449 }
450 else
451 {
452 //archive >> m_Sentence;
453 archive >> ErrorMessage;
454 archive >> LastSentenceIDParsed;
455 archive >> LastSentenceIDReceived;
456 archive >> PlainText;
457 archive >> TalkerID;
458 archive >> ExpandedTalkerID;
459 }
460}
461
462#endif // 0
463*/
464NMEA0183& NMEA0183::operator << ( const char *source )
465{
466 m_Sentence = source;
467
468 return( *this );
469}
470
471NMEA0183& NMEA0183::operator >> ( QString& destination )
472{
473 destination = m_Sentence;
474
475 return( *this );
476}
Note: See TracBrowser for help on using the repository browser.