-
Notifications
You must be signed in to change notification settings - Fork 2
/
NMEAProtocol.h
1171 lines (1059 loc) · 43.3 KB
/
NMEAProtocol.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#ifndef NMEAPROTOCOL_H
#define NMEAPROTOCOL_H
#include "OSMisc.h"
// Temporary...
#if defined(__cplusplus) && !defined(DISABLE_AIS_SUPPORT)
#include "AIS.h"
#endif // defined(__cplusplus) && !defined(DISABLE_AIS_SUPPORT)
// Need to be undefined at the end of the file...
// min and max might cause incompatibilities...
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif // !max
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif // !min
#pragma region NMEA-SPECIFIC DEFINITIONS
// A NMEA sentence begins with a '$' and ends with a carriage return/line feed sequence and can
// be no longer than 80 characters of visible text (plus the line terminators). The data is contained
// within this single line with data items separated by commas. The data itself is just ascii text.
// There is a provision for a checksum at the end of each sentence which may or may not be checked by
// the unit that reads the data. The checksum field consists of a '*' and two hex digits representing
// an 8 bit exclusive OR of all characters between, but not including, the '$' and '*'.
#define MAX_NB_BYTES_TALKER_ID_NMEA 2
#define MIN_NB_BYTES_TALKER_ID_NMEA 1
#define DEFAULT_NB_BYTES_TALKER_ID_NMEA 2
#define MAX_NB_BYTES_MNEMONIC_NMEA 5
#define MIN_NB_BYTES_MNEMONIC_NMEA 0
#define DEFAULT_NB_BYTES_MNEMONIC_NMEA 3
#define MAX_NB_BYTES_ADDRESS_NMEA (MAX_NB_BYTES_TALKER_ID_NMEA+MAX_NB_BYTES_MNEMONIC_NMEA)
#define MIN_NB_BYTES_ADDRESS_NMEA (MIN_NB_BYTES_TALKER_ID_NMEA+MIN_NB_BYTES_MNEMONIC_NMEA)
#define DEFAULT_NB_BYTES_ADDRESS_NMEA (DEFAULT_NB_BYTES_TALKER_ID_NMEA+DEFAULT_NB_BYTES_MNEMONIC_NMEA)
#define MAX_NB_BYTES_CHECKSUM_NMEA 3
#define MAX_NB_BYTES_END_NMEA 2
#define MIN_NB_BYTES_END_NMEA 1
// Maximum number of characters of a NMEA sentence (including the line terminators CR and LF).
// Should be increased to at least 92 and probably more
//#define MAX_NB_BYTES_SENTENCE_NMEA 82
#define MIN_NB_BYTES_SENTENCE_NMEA (1+MIN_NB_BYTES_ADDRESS_NMEA+MIN_NB_BYTES_END_NMEA)
#pragma endregion
struct NMEADATA
{
// Weather station.
double utc, date;
double pressure, temperature;
char cpressure, ctemperature;
double winddir, windspeed;
char cwinddir, cwindspeed;
double awinddir, awindspeed;
char cawinddir, cawindspeed;
int latdeg, longdeg;
double latmin, longmin;
char szlatdeg[3];
char szlongdeg[4];
char north, east;
int GPS_quality_indicator; // Possible values for quality: 0 = No fix, 1 = Autonomous GNSS fix, 2 = Differential GNSS fix, 4 = RTK fixed, 5 = RTK float, 6 = Estimated/Dead reckoning fix.
int nbsat;
double hdop;
double height_geoid;
char status; // Possible values for status: V = Data invalid, A = Data valid.
char posMode; // Possible values for posMode: N = No fix, E = Estimated/Dead reckoning fix, A = Autonomous GNSS fix, D = Differential GNSS fix, F = RTK float, R = RTK fixed.
double sog, kph, cog, mag_cog; // Respectively in knots, km/h, deg in NED coordinate system.
double heading, deviation, variation; // Respectively in deg in NED coordinate system.
char dev_east, var_east;
double rateofturn; // In deg/min in NED coordinate system.
int wplatdeg, wplongdeg;
double wplatmin, wplongmin;
char szwplatdeg[3];
char szwplongdeg[4];
char wpnorth, wpeast;
char szwpname[64];
int totalrtemsg, rtemsgnb;
char rtemsgmode;
char szrtewp1name[64];
char szrtewp2name[64];
char szrtewp3name[64];
char szrtewp4name[64];
// AIS.
int nbsentences;
int sentence_number;
int seqmsgid;
char AIS_channel;
int nbfillbits;
// DVL.
double roll, pitch; // In deg in NED coordinate system.
double salinity;
double depth; // In m.
double speedofsound; // In m/s.
double vx_dvl, vy_dvl, vz_dvl, verr_dvl, vt_ship, vl_ship, vn_ship, v_east, v_north, v_up; // Already converted from mm/s to m/s...
char vstatus_dvl, vstatus_ship, vstatus_earth; // 'A' = good, 'V' = bad.
double d_east, d_north, d_up, rangetobottom; // In m.
double timesincelastgood; // In s.
// Converted values.
double Latitude; // In decimal degrees.
double Longitude; // In decimal degrees.
double Altitude; // In m.
double Altitude_AGL; // In m.
double SOG; // In m/s.
double COG; // In rad in NED coordinate system.
int year, month, day, hour, minute;
double second;
double Roll; // In rad in NED coordinate system.
double Pitch; // In rad in NED coordinate system.
double Heading; // In rad in NED coordinate system.
double RateOfTurn; // In rad/s in NED coordinate system.
double WindDir; // In rad in NED coordinate system.
double WindSpeed; // In m/s.
double ApparentWindDir; // In rad.
double ApparentWindSpeed; // In m/s.
double wpLatitude; // In decimal degrees.
double wpLongitude; // In decimal degrees.
double AIS_Latitude; // In decimal degrees.
double AIS_Longitude; // In decimal degrees.
double AIS_SOG; // In m/s.
double AIS_COG; // In rad.
};
typedef struct NMEADATA NMEADATA;
// sentence must contain a valid sentence, checksum will contain a null-terminated string starting with '*'.
inline void ComputeChecksumNMEA(char* sentence, int sentencelen, char* checksum)
{
int i = 0;
char res = 0;
// +1 for the null terminator character for strings.
memset(checksum, 0, MAX_NB_BYTES_CHECKSUM_NMEA+1);
i++; // Exclude start character.
while (i < sentencelen)
{
if (sentence[i] == '*') break;
res ^= sentence[i];
i++;
}
sprintf(checksum, "*%02X", (int)(unsigned char)res);
if (checksum[MAX_NB_BYTES_CHECKSUM_NMEA] != 0)
{
PRINT_DEBUG_WARNING(("Warning : NMEA checksum computation failed."));
}
}
/*
char* talkerid, char* mnemonic : (IN) Null-terminated strings.
*/
inline void EncodeSentenceNMEA(char* sentence, int* psentencelen, char* talkerid, char* mnemonic, char* payload, int payloadlen)
{
char checksum[MAX_NB_BYTES_CHECKSUM_NMEA+1]; // +1 for the null terminator character for strings.
sentence[0] = '$';
strcpy(&sentence[1], talkerid);
strcpy(&sentence[1+strlen(talkerid)], mnemonic);
memcpy(&sentence[1+strlen(talkerid)+strlen(mnemonic)], payload, payloadlen);
*psentencelen = 1+(int)strlen(talkerid)+(int)strlen(mnemonic)+payloadlen+MAX_NB_BYTES_CHECKSUM_NMEA+MAX_NB_BYTES_END_NMEA;
sentence[*psentencelen-MAX_NB_BYTES_END_NMEA-MAX_NB_BYTES_CHECKSUM_NMEA] = '*'; // Needed for ComputeChecksumNMEA().
ComputeChecksumNMEA(sentence, *psentencelen, checksum);
strcpy(&sentence[*psentencelen-MAX_NB_BYTES_END_NMEA-MAX_NB_BYTES_CHECKSUM_NMEA+1], checksum+1);
sentence[*psentencelen-2] = '\r';
sentence[*psentencelen-1] = '\n';
}
/*
char* talkerid, char* mnemonic : (INOUT) Will contain null-terminated strings (need to be at least
MAX_NB_BYTES_TALKER_ID_NMEA+1 and MAX_NB_BYTES_MNEMONIC_NMEA+1 bytes).
Return : EXIT_SUCCESS if the beginning of buf contains a valid sentence (there might be other data at the end),
EXIT_OUT_OF_MEMORY if the sentence is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if there is an error (check *pnbBytesToDiscard to know how
many bytes can be safely discarded).
*/
inline int AnalyzeSentenceNMEA(char* buf, int buflen, char* talkerid, char* mnemonic, int* psentencelen,
int* pnbBytesToRequest, int* pnbBytesToDiscard)
{
int offset = 0, i = 0, nb_bytes_talkerid = MIN_NB_BYTES_TALKER_ID_NMEA, nb_bytes_mnemonic = MIN_NB_BYTES_MNEMONIC_NMEA, nb_bytes_end = MIN_NB_BYTES_END_NMEA;
char checksum[MAX_NB_BYTES_CHECKSUM_NMEA+1]; // +1 for the null terminator character for strings.
*psentencelen = 0;
*pnbBytesToRequest = -1;
*pnbBytesToDiscard = 0;
if (buflen < MIN_NB_BYTES_SENTENCE_NMEA)
{
*pnbBytesToRequest = MIN_NB_BYTES_SENTENCE_NMEA-buflen;
return EXIT_OUT_OF_MEMORY;
}
if (((buf[0] != '$')&&(buf[0] != '!')&&(buf[0] != ':')))
{
*pnbBytesToDiscard = 1; // We are only sure that the start character can be discarded...
return EXIT_FAILURE;
}
memset(talkerid, 0, MAX_NB_BYTES_TALKER_ID_NMEA+1); // +1 for the null terminator character for strings.
memset(mnemonic, 0, MAX_NB_BYTES_MNEMONIC_NMEA+1); // +1 for the null terminator character for strings.
// Start at i = 1 because of the start character...
for (i = 1; i < min(buflen, 1+MAX_NB_BYTES_ADDRESS_NMEA+1); i++)
{
if ((buf[i] == ',')||(buf[i] == '*')||(buf[i] == '\r')||(buf[i] == '\n')) break;
}
if (i == buflen)
{
*pnbBytesToRequest = nb_bytes_end;
return EXIT_OUT_OF_MEMORY;
}
else if (i >= DEFAULT_NB_BYTES_TALKER_ID_NMEA+DEFAULT_NB_BYTES_MNEMONIC_NMEA+1)
{
nb_bytes_talkerid = DEFAULT_NB_BYTES_TALKER_ID_NMEA;
}
else if (i == MIN_NB_BYTES_TALKER_ID_NMEA+DEFAULT_NB_BYTES_MNEMONIC_NMEA+1)
{
nb_bytes_talkerid = MIN_NB_BYTES_TALKER_ID_NMEA;
}
else if (i == DEFAULT_NB_BYTES_TALKER_ID_NMEA+MIN_NB_BYTES_MNEMONIC_NMEA+1)
{
nb_bytes_talkerid = DEFAULT_NB_BYTES_TALKER_ID_NMEA;
}
else
{
*pnbBytesToDiscard = 1; // We are only sure that the start character can be discarded...
return EXIT_FAILURE;
}
nb_bytes_mnemonic = i-nb_bytes_talkerid-1;
offset = 1;
for (i = 0; i < nb_bytes_talkerid; i++)
{
talkerid[i] = buf[offset];
offset++;
}
for (i = 0; i < nb_bytes_mnemonic; i++)
{
mnemonic[i] = buf[offset];
offset++;
}
// Line endings problems...
while (offset < buflen)
{
if ((buf[offset] == '\r')||(buf[offset] == '\n')) break;
offset++;
}
if (offset >= buflen)
{
*pnbBytesToRequest = nb_bytes_end;
return EXIT_OUT_OF_MEMORY;
}
offset++;
if (offset < buflen)
{
if ((buf[offset] != '\n')&&(buf[offset] != '\r'))
{
*psentencelen = offset;
}
else
{
*psentencelen = offset+1;
nb_bytes_end = MAX_NB_BYTES_END_NMEA;
}
}
else
{
*psentencelen = offset;
}
// If there is a checksum, check it.
if (buf[*psentencelen-MAX_NB_BYTES_CHECKSUM_NMEA-nb_bytes_end] == '*')
{
ComputeChecksumNMEA(buf, *psentencelen, checksum);
if ((toupper(buf[*psentencelen-2-nb_bytes_end]) != checksum[1])||(toupper(buf[*psentencelen-1-nb_bytes_end]) != checksum[2]))
{
PRINT_DEBUG_MESSAGE_OSUTILS(("Warning : NMEA checksum error (computed \"%.3s\", found \"*%c%c\"). \n", checksum, buf[*psentencelen-2-nb_bytes_end], buf[*psentencelen-1-nb_bytes_end]));
//for (i = 0; i < buflen; i++) printf("%c", buf[i]);
//printf("\n");
//for (i = 0; i < buflen; i++) printf("0x%02x ", (unsigned)buf[i]);
//printf("\n");
//*pnbBytesToDiscard = *psentencelen;
*pnbBytesToDiscard = 1; // Not sure more than the start character can be discarded...
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
/*
char* talkerid, char* mnemonic : (INOUT) Will contain null-terminated strings.
Return : EXIT_SUCCESS if the beginning of *pFoundSentence contains a valid sentence (there might be other data
at the end),
EXIT_OUT_OF_MEMORY if the sentence is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if no compatible sentence could be found.
Data in the beginning of buf might have been discarded (check *pnbBytesDiscarded to know how many bytes were discarded).
*/
inline int FindSentenceNMEA(char* buf, int buflen, char* talkerid, char* mnemonic, int* psentencelen,
int* pnbBytesToRequest, char** pFoundSentence, int* pnbBytesDiscarded)
{
int res = EXIT_FAILURE, nbBytesToRequest = -1, nbBytesToDiscard = 0;
*pnbBytesToRequest = -1;
*pFoundSentence = buf;
*pnbBytesDiscarded = 0;
for (;;)
{
res = AnalyzeSentenceNMEA(*pFoundSentence, buflen-(*pnbBytesDiscarded), talkerid, mnemonic, psentencelen, &nbBytesToRequest, &nbBytesToDiscard);
switch (res)
{
case EXIT_SUCCESS:
return EXIT_SUCCESS;
case EXIT_OUT_OF_MEMORY:
(*pnbBytesToRequest) = nbBytesToRequest;
return EXIT_OUT_OF_MEMORY;
default:
(*pFoundSentence) += nbBytesToDiscard;
(*pnbBytesDiscarded) += nbBytesToDiscard;
if (buflen-(*pnbBytesDiscarded) <= 0)
{
*pFoundSentence = NULL;
return EXIT_FAILURE;
}
break;
}
}
}
/*
This function is probably not really useful in practice...
char* talkerid, char* mnemonic : (INOUT) Will contain null-terminated strings.
Return : EXIT_SUCCESS if the beginning of *pFoundSentence contains the latest valid sentence (there might be other data
at the end), EXIT_OUT_OF_MEMORY if the sentence is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if no compatible sentence could be found.
Data in the beginning of buf might have been discarded, including valid sentences (check *pnbBytesDiscarded
to know how many bytes were discarded).
*/
inline int FindLatestSentenceNMEA(char* buf, int buflen, char* talkerid, char* mnemonic, int* psentencelen,
int* pnbBytesToRequest, char** pFoundSentence, int* pnbBytesDiscarded)
{
char* ptr = NULL;
int res = EXIT_FAILURE, nbBytesDiscarded = 0, sentencelen = 0, nbBytesToRequest = 0;
*pnbBytesToRequest = -1;
*pnbBytesDiscarded = 0;
res = FindSentenceNMEA(buf, buflen, talkerid, mnemonic, &sentencelen, &nbBytesToRequest, &ptr, &nbBytesDiscarded);
// Save the position of the beginning of the packet and its length.
*pFoundSentence = ptr;
*psentencelen = sentencelen;
switch (res)
{
case EXIT_SUCCESS:
(*pnbBytesDiscarded) += nbBytesDiscarded;
break;
case EXIT_OUT_OF_MEMORY:
(*pnbBytesToRequest) = nbBytesToRequest;
(*pnbBytesDiscarded) += nbBytesDiscarded;
return EXIT_OUT_OF_MEMORY;
default:
*pFoundSentence = NULL;
(*pnbBytesDiscarded) += nbBytesDiscarded;
return EXIT_FAILURE;
}
for (;;)
{
// Save the position of the beginning of the packet and its length.
*pFoundSentence = ptr;
*psentencelen = sentencelen;
// Search just after the packet.
res = FindSentenceNMEA(*pFoundSentence+*psentencelen, buflen-*psentencelen-(*pnbBytesDiscarded),
talkerid, mnemonic, &sentencelen, &nbBytesToRequest, &ptr, &nbBytesDiscarded);
switch (res)
{
case EXIT_SUCCESS:
(*pnbBytesDiscarded) += (*psentencelen+nbBytesDiscarded);
break;
case EXIT_OUT_OF_MEMORY:
default:
return EXIT_SUCCESS;
}
}
}
/*
char* talkerid, char* mnemonic : (IN) Null-terminated strings.
Return : EXIT_SUCCESS if the beginning of buf contains a valid sentence (there might be other data at the end),
EXIT_OUT_OF_MEMORY if the sentence is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if there is an error (check *pnbBytesToDiscard to know how
many bytes can be safely discarded).
*/
inline int AnalyzeSentenceWithAddressNMEA(char* buf, int buflen, char* talkerid, char* mnemonic, int* psentencelen,
int* pnbBytesToRequest, int* pnbBytesToDiscard)
{
int offset = 0, i = 0, nb_bytes_end = MIN_NB_BYTES_END_NMEA;
char checksum[MAX_NB_BYTES_CHECKSUM_NMEA+1]; // +1 for the null terminator character for strings.
*psentencelen = 0;
*pnbBytesToRequest = -1;
*pnbBytesToDiscard = 0;
if (buflen < MIN_NB_BYTES_SENTENCE_NMEA)
{
*pnbBytesToRequest = MIN_NB_BYTES_SENTENCE_NMEA-buflen;
return EXIT_OUT_OF_MEMORY;
}
if (((buf[0] != '$')&&(buf[0] != '!')&&(buf[0] != ':'))||(buf[1] != talkerid[0]))
{
*pnbBytesToDiscard = 1; // We are only sure that the start character can be discarded...
return EXIT_FAILURE;
}
offset = 2;
if (strlen(talkerid) >= MAX_NB_BYTES_TALKER_ID_NMEA)
{
if (offset >= buflen)
{
*pnbBytesToRequest = nb_bytes_end;
return EXIT_OUT_OF_MEMORY;
}
if (buf[offset] != talkerid[1])
{
*pnbBytesToDiscard = 2; // We are only sure that the 2 first bytes can be discarded...
return EXIT_FAILURE;
}
offset++;
}
for (i = 0; i < (int)strlen(mnemonic); i++)
{
if (offset >= buflen)
{
*pnbBytesToRequest = nb_bytes_end;
return EXIT_OUT_OF_MEMORY;
}
if (buf[offset] != mnemonic[i])
{
*pnbBytesToDiscard = offset; // We are only sure that the offset first bytes can be discarded...
return EXIT_FAILURE;
}
offset++;
}
// Line endings problems...
while (offset < buflen)
{
if ((buf[offset] == '\r')||(buf[offset] == '\n')) break;
offset++;
}
if (offset >= buflen)
{
*pnbBytesToRequest = nb_bytes_end;
return EXIT_OUT_OF_MEMORY;
}
offset++;
if (offset < buflen)
{
if ((buf[offset] != '\n')&&(buf[offset] != '\r'))
{
*psentencelen = offset;
}
else
{
*psentencelen = offset+1;
nb_bytes_end = MAX_NB_BYTES_END_NMEA;
}
}
else
{
*psentencelen = offset;
}
// If there is a checksum, check it.
if (buf[*psentencelen-MAX_NB_BYTES_CHECKSUM_NMEA-nb_bytes_end] == '*')
{
ComputeChecksumNMEA(buf, *psentencelen, checksum);
if ((buf[*psentencelen-2-nb_bytes_end] != checksum[1])||(buf[*psentencelen-1-nb_bytes_end] != checksum[2]))
{
PRINT_DEBUG_MESSAGE_OSUTILS(("Warning : NMEA checksum error (computed \"%.3s\", found \"*%c%c\"). \n", checksum, buf[*psentencelen-2-nb_bytes_end], buf[*psentencelen-1-nb_bytes_end]));
//for (i = 0; i < buflen; i++) printf("%c", buf[i]);
//printf("\n");
//for (i = 0; i < buflen; i++) printf("0x%02x ", (unsigned)buf[i]);
//printf("\n");
//*pnbBytesToDiscard = *psentencelen;
*pnbBytesToDiscard = 1; // Not sure more than the start character can be discarded...
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
/*
char* talkerid, char* mnemonic : (IN) Null-terminated strings.
Return : EXIT_SUCCESS if the beginning of *pFoundSentence contains a valid sentence (there might be other data
at the end),
EXIT_OUT_OF_MEMORY if the sentence is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if no compatible sentence could be found.
Data in the beginning of buf might have been discarded (check *pnbBytesDiscarded to know how many bytes were discarded).
*/
inline int FindSentenceWithAddressNMEA(char* buf, int buflen, char* talkerid, char* mnemonic, int* psentencelen,
int* pnbBytesToRequest, char** pFoundSentence, int* pnbBytesDiscarded)
{
int res = EXIT_FAILURE, nbBytesToRequest = -1, nbBytesToDiscard = 0;
*pnbBytesToRequest = -1;
*pFoundSentence = buf;
*pnbBytesDiscarded = 0;
for (;;)
{
res = AnalyzeSentenceWithAddressNMEA(*pFoundSentence, buflen-(*pnbBytesDiscarded), talkerid, mnemonic, psentencelen, &nbBytesToRequest, &nbBytesToDiscard);
switch (res)
{
case EXIT_SUCCESS:
return EXIT_SUCCESS;
case EXIT_OUT_OF_MEMORY:
(*pnbBytesToRequest) = nbBytesToRequest;
return EXIT_OUT_OF_MEMORY;
default:
(*pFoundSentence) += nbBytesToDiscard;
(*pnbBytesDiscarded) += nbBytesToDiscard;
if (buflen-(*pnbBytesDiscarded) <= 0)
{
*pFoundSentence = NULL;
return EXIT_FAILURE;
}
break;
}
}
}
/*
char* talkerid, char* mnemonic : (IN) Null-terminated strings.
Return : EXIT_SUCCESS if the beginning of *pFoundSentence contains the latest valid sentence (there might be other data
at the end), EXIT_OUT_OF_MEMORY if the sentence is incomplete (check *pnbBytesToRequest to know how many additional bytes
should be requested, -1 if unknown) or EXIT_FAILURE if no compatible sentence could be found.
Data in the beginning of buf might have been discarded, including valid sentences (check *pnbBytesDiscarded
to know how many bytes were discarded).
*/
inline int FindLatestSentenceWithAddressNMEA(char* buf, int buflen, char* talkerid, char* mnemonic, int* psentencelen,
int* pnbBytesToRequest, char** pFoundSentence, int* pnbBytesDiscarded)
{
char* ptr = NULL;
int res = EXIT_FAILURE, nbBytesDiscarded = 0, sentencelen = 0, nbBytesToRequest = 0;
*pnbBytesToRequest = -1;
*pnbBytesDiscarded = 0;
res = FindSentenceWithAddressNMEA(buf, buflen, talkerid, mnemonic, &sentencelen, &nbBytesToRequest, &ptr, &nbBytesDiscarded);
// Save the position of the beginning of the packet and its length.
*pFoundSentence = ptr;
*psentencelen = sentencelen;
switch (res)
{
case EXIT_SUCCESS:
(*pnbBytesDiscarded) += nbBytesDiscarded;
break;
case EXIT_OUT_OF_MEMORY:
(*pnbBytesToRequest) = nbBytesToRequest;
(*pnbBytesDiscarded) += nbBytesDiscarded;
return EXIT_OUT_OF_MEMORY;
default:
*pFoundSentence = NULL;
(*pnbBytesDiscarded) += nbBytesDiscarded;
return EXIT_FAILURE;
}
for (;;)
{
// Save the position of the beginning of the packet and its length.
*pFoundSentence = ptr;
*psentencelen = sentencelen;
// Search just after the packet.
res = FindSentenceWithAddressNMEA(*pFoundSentence+*psentencelen, buflen-*psentencelen-(*pnbBytesDiscarded),
talkerid, mnemonic, &sentencelen, &nbBytesToRequest, &ptr, &nbBytesDiscarded);
switch (res)
{
case EXIT_SUCCESS:
(*pnbBytesDiscarded) += (*psentencelen+nbBytesDiscarded);
break;
case EXIT_OUT_OF_MEMORY:
default:
return EXIT_SUCCESS;
}
}
}
/*
char* sentence : (IN) Must contain a valid sentence, as a null-terminated string.
char* talkerid, char* mnemonic : (IN) Null-terminated strings.
*/
inline int ProcessSentenceNMEA(char* sentence, int sentencelen, char* talkerid, char* mnemonic, NMEADATA* pNMEAData)
{
// Temporary buffers for sscanf().
char c0 = 0, c1 = 0, c2 = 0;
double f0 = 0, f1 = 0, f2 = 0;
int i0 = 0, i1 = 0;
char aisbuf[128];
int aisbuflen = 0, i = 0, offset = 0;
UNREFERENCED_PARAMETER(sentencelen);
//memset(pNMEAData, 0, sizeof(NMEADATA));
// GPS essential fix data.
if (strstr(mnemonic, "GGA"))
{
offset = 1+(int)strlen(talkerid);
memset(pNMEAData->szlatdeg, 0, sizeof(pNMEAData->szlatdeg));
memset(pNMEAData->szlongdeg, 0, sizeof(pNMEAData->szlongdeg));
if (
(sscanf(sentence+offset, "GGA,%lf,%c%c%lf,%c,%c%c%c%lf,%c,%d,%d,%lf,%lf,M,%lf,M", &pNMEAData->utc,
&pNMEAData->szlatdeg[0], &pNMEAData->szlatdeg[1], &pNMEAData->latmin, &pNMEAData->north,
&pNMEAData->szlongdeg[0], &pNMEAData->szlongdeg[1], &pNMEAData->szlongdeg[2], &pNMEAData->longmin, &pNMEAData->east,
&pNMEAData->GPS_quality_indicator, &pNMEAData->nbsat, &pNMEAData->hdop, &pNMEAData->Altitude, &pNMEAData->height_geoid) != 15)
&&
(sscanf(sentence+offset, "GGA,%lf,%c%c%lf,%c,%c%c%c%lf,%c,%d,%d,%lf,%lf,M", &pNMEAData->utc,
&pNMEAData->szlatdeg[0], &pNMEAData->szlatdeg[1], &pNMEAData->latmin, &pNMEAData->north,
&pNMEAData->szlongdeg[0], &pNMEAData->szlongdeg[1], &pNMEAData->szlongdeg[2], &pNMEAData->longmin, &pNMEAData->east,
&pNMEAData->GPS_quality_indicator, &pNMEAData->nbsat, &pNMEAData->hdop, &pNMEAData->Altitude) != 14)
&&
(sscanf(sentence+offset, "GGA,%lf,%c%c%lf,%c,%c%c%c%lf,%c,%d", &pNMEAData->utc,
&pNMEAData->szlatdeg[0], &pNMEAData->szlatdeg[1], &pNMEAData->latmin, &pNMEAData->north,
&pNMEAData->szlongdeg[0], &pNMEAData->szlongdeg[1], &pNMEAData->szlongdeg[2], &pNMEAData->longmin, &pNMEAData->east,
&pNMEAData->GPS_quality_indicator) != 11)
&&
(sscanf(sentence+offset, "GGA,%lf,,,,,%d", &pNMEAData->utc, &pNMEAData->GPS_quality_indicator) != 2)
&&
(sscanf(sentence+offset, "GGA,,,,,,%d", &pNMEAData->GPS_quality_indicator) != 1)
)
{
printf("Error parsing NMEA sentence : Invalid data. \n");
return EXIT_FAILURE;
}
if (pNMEAData->utc > 0)
{
pNMEAData->hour = (int)pNMEAData->utc/10000;
pNMEAData->minute = (int)pNMEAData->utc/100-pNMEAData->hour*100;
pNMEAData->second = (pNMEAData->utc-pNMEAData->hour*10000.0)-pNMEAData->minute*100.0;
}
if ((strlen(pNMEAData->szlatdeg) > 0)&&(strlen(pNMEAData->szlongdeg) > 0))
{
pNMEAData->latdeg = atoi(pNMEAData->szlatdeg);
pNMEAData->longdeg = atoi(pNMEAData->szlongdeg);
// Convert GPS latitude and longitude in decimal.
pNMEAData->Latitude = (pNMEAData->north == 'N')?(pNMEAData->latdeg+pNMEAData->latmin/60.0):-(pNMEAData->latdeg+pNMEAData->latmin/60.0);
pNMEAData->Longitude = (pNMEAData->east == 'E')?(pNMEAData->longdeg+pNMEAData->longmin/60.0):-(pNMEAData->longdeg+pNMEAData->longmin/60.0);
}
}
// GPS recommended minimum data.
if (strstr(mnemonic, "RMC"))
{
offset = 1+(int)strlen(talkerid);
memset(pNMEAData->szlatdeg, 0, sizeof(pNMEAData->szlatdeg));
memset(pNMEAData->szlongdeg, 0, sizeof(pNMEAData->szlongdeg));
if (
(sscanf(sentence+offset, "RMC,%lf,%c,%c%c%lf,%c,%c%c%c%lf,%c,%lf,%lf,%lf,%lf,%c,%c", &pNMEAData->utc, &pNMEAData->status,
&pNMEAData->szlatdeg[0], &pNMEAData->szlatdeg[1], &pNMEAData->latmin, &pNMEAData->north,
&pNMEAData->szlongdeg[0], &pNMEAData->szlongdeg[1], &pNMEAData->szlongdeg[2], &pNMEAData->longmin, &pNMEAData->east,
&pNMEAData->sog, &pNMEAData->cog, &pNMEAData->date, &pNMEAData->variation, &pNMEAData->var_east, &pNMEAData->posMode) != 17)
&&
(sscanf(sentence+offset, "RMC,%lf,%c,%c%c%lf,%c,%c%c%c%lf,%c,%lf,%lf,%lf,%lf,%c", &pNMEAData->utc, &pNMEAData->status,
&pNMEAData->szlatdeg[0], &pNMEAData->szlatdeg[1], &pNMEAData->latmin, &pNMEAData->north,
&pNMEAData->szlongdeg[0], &pNMEAData->szlongdeg[1], &pNMEAData->szlongdeg[2], &pNMEAData->longmin, &pNMEAData->east,
&pNMEAData->sog, &pNMEAData->cog, &pNMEAData->date, &pNMEAData->variation, &pNMEAData->var_east) != 16)
&&
(sscanf(sentence+offset, "RMC,%lf,%c,%c%c%lf,%c,%c%c%c%lf,%c,%lf,%lf,%lf,,,%c", &pNMEAData->utc, &pNMEAData->status,
&pNMEAData->szlatdeg[0], &pNMEAData->szlatdeg[1], &pNMEAData->latmin, &pNMEAData->north,
&pNMEAData->szlongdeg[0], &pNMEAData->szlongdeg[1], &pNMEAData->szlongdeg[2], &pNMEAData->longmin, &pNMEAData->east,
&pNMEAData->sog, &pNMEAData->cog, &pNMEAData->date, &pNMEAData->posMode) != 15)
&&
(sscanf(sentence+offset, "RMC,%lf,%c,%c%c%lf,%c,%c%c%c%lf,%c,%lf,%lf,%lf", &pNMEAData->utc, &pNMEAData->status,
&pNMEAData->szlatdeg[0], &pNMEAData->szlatdeg[1], &pNMEAData->latmin, &pNMEAData->north,
&pNMEAData->szlongdeg[0], &pNMEAData->szlongdeg[1], &pNMEAData->szlongdeg[2], &pNMEAData->longmin, &pNMEAData->east,
&pNMEAData->sog, &pNMEAData->cog, &pNMEAData->date) != 14)
&&
(sscanf(sentence+offset, "RMC,%lf,%c,%c%c%lf,%c,%c%c%c%lf,%c", &pNMEAData->utc, &pNMEAData->status,
&pNMEAData->szlatdeg[0], &pNMEAData->szlatdeg[1], &pNMEAData->latmin, &pNMEAData->north,
&pNMEAData->szlongdeg[0], &pNMEAData->szlongdeg[1], &pNMEAData->szlongdeg[2], &pNMEAData->longmin, &pNMEAData->east) != 11)
&&
(sscanf(sentence+offset, "RMC,%lf,%c", &pNMEAData->utc, &pNMEAData->status) != 2)
&&
(sscanf(sentence+offset, "RMC,,%c", &pNMEAData->status) != 1)
)
{
printf("Error parsing NMEA sentence : Invalid data. \n");
return EXIT_FAILURE;
}
if (pNMEAData->utc > 0)
{
pNMEAData->hour = (int)pNMEAData->utc/10000;
pNMEAData->minute = (int)pNMEAData->utc/100-pNMEAData->hour*100;
pNMEAData->second = (pNMEAData->utc-pNMEAData->hour*10000.0)-pNMEAData->minute*100.0;
}
if ((strlen(pNMEAData->szlatdeg) > 0)&&(strlen(pNMEAData->szlongdeg) > 0))
{
pNMEAData->latdeg = atoi(pNMEAData->szlatdeg);
pNMEAData->longdeg = atoi(pNMEAData->szlongdeg);
// Convert GPS latitude and longitude in decimal.
pNMEAData->Latitude = (pNMEAData->north == 'N')?(pNMEAData->latdeg+pNMEAData->latmin/60.0):-(pNMEAData->latdeg+pNMEAData->latmin/60.0);
pNMEAData->Longitude = (pNMEAData->east == 'E')?(pNMEAData->longdeg+pNMEAData->longmin/60.0):-(pNMEAData->longdeg+pNMEAData->longmin/60.0);
}
// Convert SOG to speed in m/s and COG to angle in rad.
pNMEAData->SOG = pNMEAData->sog/1.94;
pNMEAData->COG = pNMEAData->cog*M_PI/180.0;
if (pNMEAData->date > 0)
{
pNMEAData->day = (int)pNMEAData->date/10000;
pNMEAData->month = (int)pNMEAData->date/100-pNMEAData->day*100;
pNMEAData->year = 2000+((int)pNMEAData->date-pNMEAData->day*10000)-pNMEAData->month*100;
}
}
// GPS position, latitude / longitude and time.
if (strstr(mnemonic, "GLL"))
{
offset = 1+(int)strlen(talkerid);
memset(pNMEAData->szlatdeg, 0, sizeof(pNMEAData->szlatdeg));
memset(pNMEAData->szlongdeg, 0, sizeof(pNMEAData->szlongdeg));
if ((sscanf(sentence+offset, "GLL,%c%c%lf,%c,%c%c%c%lf,%c,%lf,%c,%c",
&pNMEAData->szlatdeg[0], &pNMEAData->szlatdeg[1], &pNMEAData->latmin, &pNMEAData->north,
&pNMEAData->szlongdeg[0], &pNMEAData->szlongdeg[1], &pNMEAData->szlongdeg[2], &pNMEAData->longmin, &pNMEAData->east,
&pNMEAData->utc, &pNMEAData->status, &pNMEAData->posMode) != 12)&&
(sscanf(sentence+offset, "GLL,%c%c%lf,%c,%c%c%c%lf,%c,%lf,%c",
&pNMEAData->szlatdeg[0], &pNMEAData->szlatdeg[1], &pNMEAData->latmin, &pNMEAData->north,
&pNMEAData->szlongdeg[0], &pNMEAData->szlongdeg[1], &pNMEAData->szlongdeg[2], &pNMEAData->longmin, &pNMEAData->east,
&pNMEAData->utc, &pNMEAData->status) != 11)&&
(sscanf(sentence+offset, "GLL,,,,,%lf,%c", &pNMEAData->utc, &pNMEAData->status) != 2)&&
(sscanf(sentence+offset, "GLL,,,,,,%c", &pNMEAData->status) != 1))
{
printf("Error parsing NMEA sentence : Invalid data. \n");
return EXIT_FAILURE;
}
if ((strlen(pNMEAData->szlatdeg) > 0)&&(strlen(pNMEAData->szlongdeg) > 0))
{
pNMEAData->latdeg = atoi(pNMEAData->szlatdeg);
pNMEAData->longdeg = atoi(pNMEAData->szlongdeg);
// Convert GPS latitude and longitude in decimal.
pNMEAData->Latitude = (pNMEAData->north == 'N')?(pNMEAData->latdeg+pNMEAData->latmin/60.0):-(pNMEAData->latdeg+pNMEAData->latmin/60.0);
pNMEAData->Longitude = (pNMEAData->east == 'E')?(pNMEAData->longdeg+pNMEAData->longmin/60.0):-(pNMEAData->longdeg+pNMEAData->longmin/60.0);
}
if (pNMEAData->utc > 0)
{
pNMEAData->hour = (int)pNMEAData->utc/10000;
pNMEAData->minute = (int)pNMEAData->utc/100-pNMEAData->hour*100;
pNMEAData->second = (pNMEAData->utc-pNMEAData->hour*10000.0)-pNMEAData->minute*100.0;
}
}
// GPS COG and SOG.
if (strstr(mnemonic, "VTG"))
{
offset = 1+(int)strlen(talkerid);
if ((sscanf(sentence+offset, "VTG,%lf,T,%lf,M,%lf,N,%lf,K,%c", &pNMEAData->cog, &pNMEAData->mag_cog, &pNMEAData->sog, &pNMEAData->kph, &pNMEAData->posMode) != 5)&&
(sscanf(sentence+offset, "VTG,%lf,T,,M,%lf,N,%lf,K,%c", &pNMEAData->cog, &pNMEAData->sog, &pNMEAData->kph, &pNMEAData->posMode) != 4)&&
(sscanf(sentence+offset, "VTG,%lf,T,,,%lf,N,%lf,K,%c", &pNMEAData->cog, &pNMEAData->sog, &pNMEAData->kph, &pNMEAData->posMode) != 4)&&
(sscanf(sentence+offset, "VTG,%lf,T,%lf,M,%lf,N", &pNMEAData->cog, &pNMEAData->mag_cog, &pNMEAData->sog) != 3)&&
(sscanf(sentence+offset, "VTG,%lf,T,,M,%lf,N", &pNMEAData->cog, &pNMEAData->sog) != 2)&&
(sscanf(sentence+offset, "VTG,%lf,T,,,%lf,N", &pNMEAData->cog, &pNMEAData->sog) != 2)&&
(sscanf(sentence+offset, "VTG,nan,T,nan,M,%lf,N,%lf,K,%c", &pNMEAData->sog, &pNMEAData->kph, &pNMEAData->posMode) != 3)&&
(sscanf(sentence+offset, "VTG,NAN,T,NAN,M,%lf,N,%lf,K,%c", &pNMEAData->sog, &pNMEAData->kph, &pNMEAData->posMode) != 3)&&
(sscanf(sentence+offset, "VTG,NaN,T,NaN,M,%lf,N,%lf,K,%c", &pNMEAData->sog, &pNMEAData->kph, &pNMEAData->posMode) != 3)&&
(sscanf(sentence+offset, "VTG,nan,T,nan,M,%lf,N", &pNMEAData->sog) != 1)&&
(sscanf(sentence+offset, "VTG,NAN,T,NAN,M,%lf,N", &pNMEAData->sog) != 1)&&
(sscanf(sentence+offset, "VTG,NaN,T,NaN,M,%lf,N", &pNMEAData->sog) != 1)&&
(sscanf(sentence+offset, "VTG,nan,T,,,%lf,N,%lf,K,%c", &pNMEAData->sog, &pNMEAData->kph, &pNMEAData->posMode) != 3)&&
(sscanf(sentence+offset, "VTG,NAN,T,,,%lf,N,%lf,K,%c", &pNMEAData->sog, &pNMEAData->kph, &pNMEAData->posMode) != 3)&&
(sscanf(sentence+offset, "VTG,NaN,T,,,%lf,N,%lf,K,%c", &pNMEAData->sog, &pNMEAData->kph, &pNMEAData->posMode) != 3)&&
(sscanf(sentence+offset, "VTG,nan,T,,,%lf,N", &pNMEAData->sog) != 1)&&
(sscanf(sentence+offset, "VTG,NAN,T,,,%lf,N", &pNMEAData->sog) != 1)&&
(sscanf(sentence+offset, "VTG,NaN,T,,,%lf,N", &pNMEAData->sog) != 1))
{
//printf("Error parsing NMEA sentence : Invalid data. \n");
//return EXIT_FAILURE;
}
// Convert SOG to speed in m/s and COG to angle in rad.
pNMEAData->SOG = pNMEAData->sog/1.94;
pNMEAData->COG = pNMEAData->cog*M_PI/180.0;
}
// Heading data (magnetic).
if (strstr(mnemonic, "HDG"))
{
offset = 1+(int)strlen(talkerid);
if ((sscanf(sentence+offset, "HDG,%lf,%lf,%c,%lf,%c",
&pNMEAData->heading, &pNMEAData->deviation, &pNMEAData->dev_east, &pNMEAData->variation, &pNMEAData->var_east) != 5)&&
(sscanf(sentence+offset, "HDG,%lf,%lf,%c",
&pNMEAData->heading, &pNMEAData->deviation, &pNMEAData->dev_east) != 3)&&
(sscanf(sentence+offset, "HDG,%lf", &pNMEAData->heading) != 1))
{
//printf("Error parsing NMEA sentence : Invalid data. \n");
//return EXIT_FAILURE;
}
// Convert heading to angle in rad.
pNMEAData->Heading = (pNMEAData->heading-((pNMEAData->dev_east == 'W')? -fabs(pNMEAData->deviation): fabs(pNMEAData->deviation)))*M_PI/180.0;
}
// Heading data (magnetic).
if (strstr(mnemonic, "HDM"))
{
offset = 1+(int)strlen(talkerid);
if (sscanf(sentence+offset, "HDM,%lf,M", &pNMEAData->heading) != 1)
{
//printf("Error parsing NMEA sentence : Invalid data. \n");
//return EXIT_FAILURE;
}
// Convert heading to angle in rad.
pNMEAData->Heading = (pNMEAData->heading-((pNMEAData->dev_east == 'W')? -fabs(pNMEAData->deviation): fabs(pNMEAData->deviation)))*M_PI/180.0;
}
// Heading data (true).
if (strstr(mnemonic, "HDT"))
{
offset = 1+(int)strlen(talkerid);
if (sscanf(sentence+offset, "HDT,%lf,T", &pNMEAData->heading) != 1)
{
//printf("Error parsing NMEA sentence : Invalid data. \n");
//return EXIT_FAILURE;
}
// Convert heading to angle in rad.
pNMEAData->Heading = pNMEAData->heading*M_PI/180.0;
}
// Rate Of Turn.
if (strstr(mnemonic, "ROT"))
{
offset = 1+(int)strlen(talkerid);
if (sscanf(sentence+offset, "ROT,%lf,A", &pNMEAData->rateofturn) != 1)
{
//printf("Error parsing NMEA sentence : Invalid data. \n");
//return EXIT_FAILURE;
}
// Convert to rad/s.
pNMEAData->RateOfTurn = pNMEAData->rateofturn*M_PI/(180.0*60.0);
}
// Wind speed and angle, in relation to the vessel's bow/centerline.
if (strstr(mnemonic, "MWV"))
{
offset = 1+(int)strlen(talkerid);
if (sscanf(sentence+offset, "MWV,%lf,R,%lf,%c,A",
&pNMEAData->awinddir, &pNMEAData->awindspeed, &pNMEAData->cawindspeed) != 3)
{
printf("Error parsing NMEA sentence : Invalid data. \n");
return EXIT_FAILURE;
}
// Convert apparent wind direction to angle in rad.
pNMEAData->ApparentWindDir = pNMEAData->awinddir*M_PI/180.0;
// Convert apparent wind speed to m/s.
switch (pNMEAData->cawindspeed)
{
case 'K': pNMEAData->ApparentWindSpeed = pNMEAData->awindspeed*0.28; break;
case 'M': pNMEAData->ApparentWindSpeed = pNMEAData->awindspeed; break;
case 'N': pNMEAData->ApparentWindSpeed = pNMEAData->awindspeed*0.51; break;
case 'S': pNMEAData->ApparentWindSpeed = pNMEAData->awindspeed*0.45; break;
default: break;
}
}
// Wind direction and speed, with respect to north.
if (strstr(mnemonic, "MWD"))
{
offset = 1+(int)strlen(talkerid);
if (sscanf(sentence+offset, "MWD,%lf,%c,%lf,%c,%lf,%c,%lf,%c",
&pNMEAData->winddir, &pNMEAData->cwinddir, &f1, &c1, &f2, &c2, &pNMEAData->windspeed, &pNMEAData->cwindspeed) != 8)
{
//printf("Error parsing NMEA sentence : Invalid data. \n");
//return EXIT_FAILURE;
}
// Do other else if (sscanf() != x) if more/less complete sentence...
// Convert wind direction to angle in rad.
pNMEAData->WindDir = pNMEAData->winddir*M_PI/180.0;
pNMEAData->WindSpeed = pNMEAData->windspeed;
}
// Meteorological composite data.
if (strstr(mnemonic, "MDA"))
{
offset = 1+(int)strlen(talkerid);
if (sscanf(sentence+offset, "MDA,%lf,%c,%lf,%c,%lf,%c,,,,,,,%lf,%c,%lf,%c,%lf,%c,%lf,%c",
&f0, &c0, &pNMEAData->pressure, &pNMEAData->cpressure, &pNMEAData->temperature, &pNMEAData->ctemperature,
&pNMEAData->winddir, &pNMEAData->cwinddir, &f1, &c1, &f2, &c2, &pNMEAData->windspeed, &pNMEAData->cwindspeed) != 14)
{
//printf("Error parsing NMEA sentence : Invalid data. \n");
//return EXIT_FAILURE;
}
// Do other else if (sscanf() != x) if more/less complete sentence...
// Convert wind direction to angle in rad.
pNMEAData->WindDir = pNMEAData->winddir*M_PI/180.0;
pNMEAData->WindSpeed = pNMEAData->windspeed;
}
// PRDID.
if (strstr(mnemonic, "DID"))
{
offset = 1+(int)strlen(talkerid);
if (sscanf(sentence+offset, "DID,%lf,%lf,%lf", &pNMEAData->pitch, &pNMEAData->roll, &pNMEAData->heading) != 3)
{
//printf("Error parsing NMEA sentence : Invalid data. \n");
//return EXIT_FAILURE;
}
// Convert to angle in rad.
pNMEAData->Heading = pNMEAData->heading*M_PI/180.0;
pNMEAData->Pitch = pNMEAData->pitch*M_PI/180.0;
pNMEAData->Roll = pNMEAData->roll*M_PI/180.0;
}
// Waypoint location data.
if (strstr(mnemonic, "WPL"))
{
offset = 1+(int)strlen(talkerid);
memset(pNMEAData->szwpname, 0, sizeof(pNMEAData->szwpname));
if (sscanf(sentence+offset, "WPL,%c%c%lf,%c,%c%c%c%lf,%c,%63s",
&pNMEAData->szwplatdeg[0], &pNMEAData->szwplatdeg[1], &pNMEAData->wplatmin, &pNMEAData->wpnorth,
&pNMEAData->szwplongdeg[0], &pNMEAData->szwplongdeg[1], &pNMEAData->szwplongdeg[2], &pNMEAData->wplongmin, &pNMEAData->wpeast,
pNMEAData->szwpname) != 10)
{
//printf("Error parsing NMEA sentence : Invalid data. \n");
//return EXIT_FAILURE;
}
// Do other else if (sscanf() != x) if more/less complete sentence...
if ((strlen(pNMEAData->szwplatdeg) > 0)&&(strlen(pNMEAData->szwplongdeg) > 0))
{
pNMEAData->wplatdeg = atoi(pNMEAData->szwplatdeg);
pNMEAData->wplongdeg = atoi(pNMEAData->szwplongdeg);
// Convert GPS latitude and longitude in decimal.
pNMEAData->wpLatitude = (pNMEAData->wpnorth == 'N')?(pNMEAData->wplatdeg+pNMEAData->wplatmin/60.0):-(pNMEAData->wplatdeg+pNMEAData->wplatmin/60.0);
pNMEAData->wpLongitude = (pNMEAData->wpeast == 'E')?(pNMEAData->wplongdeg+pNMEAData->wplongmin/60.0):-(pNMEAData->wplongdeg+pNMEAData->wplongmin/60.0);
}
}
// Routes data.
if (strstr(mnemonic, "RTE"))
{
offset = 1+(int)strlen(talkerid);
memset(pNMEAData->szrtewp1name, 0, sizeof(pNMEAData->szrtewp1name));
memset(pNMEAData->szrtewp2name, 0, sizeof(pNMEAData->szrtewp2name));
memset(pNMEAData->szrtewp3name, 0, sizeof(pNMEAData->szrtewp3name));
memset(pNMEAData->szrtewp4name, 0, sizeof(pNMEAData->szrtewp4name));
if (
(sscanf(sentence+offset, "RTE,%d,%d,%c,%63s,%63s,%63s,%63s",
&pNMEAData->totalrtemsg, &pNMEAData->rtemsgnb, &pNMEAData->rtemsgmode, pNMEAData->szrtewp1name, pNMEAData->szrtewp2name, pNMEAData->szrtewp3name, pNMEAData->szrtewp3name) != 7)
&&
(sscanf(sentence+offset, "RTE,%d,%d,%c,%63s,%63s,%63s",
&pNMEAData->totalrtemsg, &pNMEAData->rtemsgnb, &pNMEAData->rtemsgmode, pNMEAData->szrtewp1name, pNMEAData->szrtewp2name, pNMEAData->szrtewp3name) != 6)
&&
(sscanf(sentence+offset, "RTE,%d,%d,%c,%63s,%63s",
&pNMEAData->totalrtemsg, &pNMEAData->rtemsgnb, &pNMEAData->rtemsgmode, pNMEAData->szrtewp1name, pNMEAData->szrtewp2name) != 5)
&&
(sscanf(sentence+offset, "RTE,%d,%d,%c,%63s",
&pNMEAData->totalrtemsg, &pNMEAData->rtemsgnb, &pNMEAData->rtemsgmode, pNMEAData->szrtewp1name) != 4)
)