forked from ttlappalainen/NMEA2000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN2kMessages.cpp
1096 lines (953 loc) · 45.3 KB
/
N2kMessages.cpp
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
/*
N2kMessages.cpp
2015-2016 Copyright (c) Kave Oy, www.kave.fi All right reserved.
Author: Timo Lappalainen
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-
1301 USA
*/
#include <N2kMessages.h>
//*****************************************************************************
// System time
void SetN2kPGN126992(tN2kMsg &N2kMsg, unsigned char SID, uint16_t SystemDate,
double SystemTime, tN2kTimeSource TimeSource) {
N2kMsg.SetPGN(126992L);
N2kMsg.Priority=3;
N2kMsg.AddByte(SID);
N2kMsg.AddByte(TimeSource);
N2kMsg.Add2ByteInt(SystemDate);
N2kMsg.Add4ByteUDouble(SystemTime,0.0001);
}
//*****************************************************************************
bool ParseN2kPGN126992(const tN2kMsg &N2kMsg, unsigned char &SID, uint16_t &SystemDate,
double &SystemTime, tN2kTimeSource &TimeSource) {
if (N2kMsg.PGN!=126992L) return false;
int Index=0;
SID=N2kMsg.GetByte(Index);
TimeSource=(tN2kTimeSource)(N2kMsg.GetByte(Index));
SystemDate=N2kMsg.Get2ByteUInt(Index);
SystemTime=N2kMsg.Get4ByteDouble(0.0001,Index);
return true;
}
//*****************************************************************************
// Rudder
// Angles should be in radians
void SetN2kPGN127245(tN2kMsg &N2kMsg, double RudderPosition, unsigned char Instance,
tN2kRudderDirectionOrder RudderDirectionOrder, double AngleOrder) {
N2kMsg.SetPGN(127245L);
N2kMsg.Priority=2;
N2kMsg.AddByte(Instance);
N2kMsg.AddByte(0xf8 | (RudderDirectionOrder&0x07));
N2kMsg.Add2ByteDouble(AngleOrder,0.0001);
N2kMsg.Add2ByteDouble(RudderPosition,0.0001);
N2kMsg.AddByte(0xff); // Reserved
N2kMsg.AddByte(0xff); // Reserved
}
//*****************************************************************************
// Vessel Heading
// Angles should be in radians
void SetN2kPGN127250(tN2kMsg &N2kMsg, unsigned char SID, double Heading, double Deviation, double Variation, tN2kHeadingReference ref) {
N2kMsg.SetPGN(127250L);
N2kMsg.Priority=2;
N2kMsg.AddByte(SID);
N2kMsg.Add2ByteUDouble(Heading,0.0001);
N2kMsg.Add2ByteDouble(Deviation,0.0001);
N2kMsg.Add2ByteDouble(Variation,0.0001);
N2kMsg.AddByte(0xfc | ref);
}
bool ParseN2kPGN127250(const tN2kMsg &N2kMsg, unsigned char &SID, double &Heading, double &Deviation, double &Variation, tN2kHeadingReference &ref) {
if (N2kMsg.PGN!=127250L) return false;
int Index=0;
SID=N2kMsg.GetByte(Index);
Heading=N2kMsg.Get2ByteDouble(0.0001,Index);
Deviation=N2kMsg.Get2ByteUDouble(0.0001,Index);
Variation=N2kMsg.Get2ByteUDouble(0.0001,Index);
ref=(tN2kHeadingReference)(N2kMsg.GetByte(Index)&0x03);
return true;
}
//*****************************************************************************
// Rate of turn
// Angles should be in radians
void SetN2kPGN127251(tN2kMsg &N2kMsg, unsigned char SID, double RateOfTurn) {
N2kMsg.SetPGN(127251L);
N2kMsg.Priority=2;
N2kMsg.AddByte(SID);
N2kMsg.Add4ByteUDouble(RateOfTurn,((1e-3/32.0) * 0.0001));
}
bool ParseN2kPGN127251(const tN2kMsg &N2kMsg, unsigned char &SID, double &RateOfTurn) {
if (N2kMsg.PGN!=127251L) return false;
int Index=0;
SID=N2kMsg.GetByte(Index);
RateOfTurn=N2kMsg.Get4ByteDouble(((1e-3/32.0) * 0.0001),Index);
return true;
}
//*****************************************************************************
// Attitude
// Input:
// - SID Sequence ID. If your device is e.g. boat speed and heading at same time, you can set same SID for different messages
// to indicate that they are measured at same time.
// - Yaw Heading in radians.
// - Pitch Pitch in radians. Positive, when your bow rises.
// - Roll Roll in radians. Positive, when tilted right.
// Output:
// - N2kMsg NMEA2000 message ready to be send.
void SetN2kPGN127257(tN2kMsg &N2kMsg, unsigned char SID, double Yaw, double Pitch, double Roll) {
N2kMsg.SetPGN(127257L);
N2kMsg.Priority=2;
N2kMsg.AddByte(SID);
N2kMsg.Add2ByteDouble(Yaw,0.0001);
N2kMsg.Add2ByteDouble(Pitch,0.0001);
N2kMsg.Add2ByteDouble(Roll,0.0001);
N2kMsg.AddByte(0xff); // Reserved
}
bool ParseN2kPGN127257(const tN2kMsg &N2kMsg, unsigned char &SID, double &Yaw, double &Pitch, double &Roll){
if (N2kMsg.PGN!=127257L) return false;
int Index=0;
SID=N2kMsg.GetByte(Index);
Yaw=N2kMsg.Get2ByteDouble(0.0001,Index);
Pitch=N2kMsg.Get2ByteDouble(0.0001,Index);
Roll=N2kMsg.Get2ByteDouble(0.0001,Index);
return true;
}
//*****************************************************************************
// Engine rapid param
void SetN2kPGN127488(tN2kMsg &N2kMsg, unsigned char EngineInstance, double EngineSpeed,
double EngineBoostPressure, int8_t EngineTiltTrim) {
N2kMsg.SetPGN(127488L);
N2kMsg.Priority=3;
N2kMsg.AddByte(EngineInstance);
N2kMsg.Add2ByteDouble(EngineSpeed,0.25);
N2kMsg.Add2ByteUDouble(EngineBoostPressure, 100);
N2kMsg.AddByte(EngineTiltTrim);
N2kMsg.AddByte(0xff); // Reserved
N2kMsg.AddByte(0xff); // Reserved
}
bool ParseN2kPGN127488(const tN2kMsg &N2kMsg, unsigned char &EngineInstance, double &EngineSpeed,
double &EngineBoostPressure, int8_t &EngineTiltTrim) {
if (N2kMsg.PGN!=127488L) return false;
int Index=0;
EngineInstance=N2kMsg.GetByte(Index);
EngineSpeed=N2kMsg.Get2ByteDouble(0.25,Index);
EngineBoostPressure=N2kMsg.Get2ByteUDouble(100,Index);
EngineTiltTrim=N2kMsg.GetByte(Index);
return true;
}
//*****************************************************************************
// Engine parameters dynamic
void SetN2kPGN127489(tN2kMsg &N2kMsg, unsigned char EngineInstance, double EngineOilPress, double EngineOilTemp, double EngineCoolantTemp, double AltenatorVoltage,
double FuelRate, double EngineHours, double EngineCoolantPress, double EngineFuelPress, int8_t EngineLoad, int8_t EngineTorque,
bool flagCheckEngine,
bool flagOverTemp, bool flagLowOilPress, bool flagLowOilLevel, bool flagLowFuelPress, bool flagLowSystemVoltage, bool flagLowCoolantLevel,
bool flagWaterFlow, bool flagWaterInFuel, bool flagChargeIndicator, bool flagPreheatIndicator, bool flagHighBoostPress, bool flagRevLimitExceeded,
bool flagEgrSystem, bool flagTPS, bool flagEmergencyStopMode, bool flagWarning1, bool flagWarning2, bool flagPowerReduction,
bool flagMaintenanceNeeded, bool flagEngineCommError, bool flagSubThrottle, bool flagNeutralStartProtect, bool flagEngineShuttingDown) {
N2kMsg.SetPGN(127489L);
N2kMsg.Priority=6;
N2kMsg.AddByte(EngineInstance);
N2kMsg.Add2ByteUDouble(EngineOilPress, 100);
N2kMsg.Add2ByteUDouble(EngineOilTemp, 0.1);
N2kMsg.Add2ByteUDouble(EngineCoolantTemp, 0.01);
N2kMsg.Add2ByteDouble(AltenatorVoltage, 0.01);
N2kMsg.Add2ByteDouble(FuelRate, 0.1);
N2kMsg.Add4ByteUDouble(EngineHours, 1);
N2kMsg.Add2ByteUDouble(EngineCoolantPress, 100);
N2kMsg.Add2ByteUDouble(EngineFuelPress, 1000);
N2kMsg.AddByte(0xff); // reserved
int engineStatus1P1 = B00000000;
int engineStatus1P2 = B00000000;
int engineStatus2 = B00000000;
if (flagCheckEngine) engineStatus1P1 |= B00000001;
if (flagOverTemp) engineStatus1P1 |= B00000010;
if (flagLowOilPress) engineStatus1P1 |= B00000100;
if (flagLowOilLevel) engineStatus1P1 |= B00001000;
if (flagLowFuelPress) engineStatus1P1 |= B00010000;
if (flagLowSystemVoltage) engineStatus1P1 |= B00100000;
if (flagLowCoolantLevel) engineStatus1P1 |= B01000000;
if (flagWaterFlow) engineStatus1P1 |= B10000000;
if (flagWaterInFuel) engineStatus1P2 |= B00000001;
if (flagChargeIndicator) engineStatus1P2 |= B00000010;
if (flagPreheatIndicator) engineStatus1P2 |= B00000100;
if (flagHighBoostPress) engineStatus1P2 |= B00001000;
if (flagRevLimitExceeded) engineStatus1P2 |= B00010000;
if (flagEgrSystem) engineStatus1P2 |= B00100000;
if (flagTPS) engineStatus1P2 |= B01000000;
if (flagEmergencyStopMode) engineStatus1P2 |= B10000000;
if (flagWarning1) engineStatus2 |= B00000001;
if (flagWarning2) engineStatus2 |= B00000010;
if (flagPowerReduction) engineStatus2 |= B00000100;
if (flagMaintenanceNeeded) engineStatus2 |= B00001000;
if (flagEngineCommError) engineStatus2 |= B00010000;
if (flagSubThrottle) engineStatus2 |= B00100000;
if (flagNeutralStartProtect) engineStatus2 |= B01000000;
if (flagEngineShuttingDown) engineStatus2 |= B10000000;
N2kMsg.Add2ByteInt(engineStatus1P2<<8 | engineStatus1P1); // Discrete Status 1
N2kMsg.Add2ByteInt(engineStatus2); // Discrete Status 1
N2kMsg.AddByte(EngineLoad);
N2kMsg.AddByte(EngineTorque);
}
bool ParseN2kPGN127489(const tN2kMsg &N2kMsg, unsigned char &EngineInstance, double &EngineOilPress,
double &EngineOilTemp, double &EngineCoolantTemp, double &AltenatorVoltage,
double &FuelRate, double &EngineHours, double &EngineCoolantPress, double &EngineFuelPress,
int8_t &EngineLoad, int8_t &EngineTorque) {
if (N2kMsg.PGN != 127489L) return false;
int Index = 0;
EngineInstance = N2kMsg.GetByte(Index);
EngineOilPress = N2kMsg.Get2ByteUDouble(100, Index);
EngineOilTemp = N2kMsg.Get2ByteUDouble(0.1, Index);
EngineCoolantTemp = N2kMsg.Get2ByteUDouble(0.01, Index);
AltenatorVoltage = N2kMsg.Get2ByteDouble(0.01, Index);
FuelRate = N2kMsg.Get2ByteDouble(0.1, Index);
EngineHours = N2kMsg.Get4ByteUDouble(1, Index);
EngineCoolantPress=N2kMsg.Get2ByteUDouble(100, Index);
EngineFuelPress=N2kMsg.Get2ByteUDouble(1000, Index);
N2kMsg.GetByte(Index); // reserved
N2kMsg.Get2ByteInt(Index); // Discrete Status 1
N2kMsg.Get2ByteInt(Index); // Discrete Status 2
EngineLoad=N2kMsg.GetByte(Index);
EngineTorque=N2kMsg.GetByte(Index);
return true;
}
//*****************************************************************************
// Transmission parameters, dynamic
void SetN2kPGN127493(tN2kMsg &N2kMsg, unsigned char EngineInstance, tN2kTransmissionGear TransmissionGear,
double OilPressure, double OilTemperature, unsigned char DiscreteStatus1) {
N2kMsg.SetPGN(127493L);
N2kMsg.Priority=6;
N2kMsg.AddByte(EngineInstance);
N2kMsg.AddByte((TransmissionGear & 0x03) | 0xfc );
N2kMsg.Add2ByteUDouble(OilPressure, 100);
N2kMsg.Add2ByteUDouble(OilTemperature, 0.1);
N2kMsg.AddByte(DiscreteStatus1);
N2kMsg.AddByte(0xff); // Reserved
}
bool ParseN2kPGN127493(const tN2kMsg &N2kMsg, unsigned char &EngineInstance, tN2kTransmissionGear &TransmissionGear,
double &OilPressure, double &OilTemperature, unsigned char &DiscreteStatus1) {
if (N2kMsg.PGN!=127493L) return false;
int Index=0;
EngineInstance=N2kMsg.GetByte(Index);
TransmissionGear=(tN2kTransmissionGear)(N2kMsg.GetByte(Index) & 0x03);
OilPressure=N2kMsg.Get2ByteUDouble(100,Index);
OilTemperature=N2kMsg.Get2ByteUDouble(0.1,Index);
DiscreteStatus1=N2kMsg.GetByte(Index);
return true;
}
//*****************************************************************************
// Fluid level
void SetN2kPGN127505(tN2kMsg &N2kMsg, unsigned char Instance, tN2kFluidType FluidType, double Level, double Capacity) {
N2kMsg.SetPGN(127505L);
N2kMsg.Priority=6;
N2kMsg.AddByte((Instance&0x0f) | ((FluidType&0x0f)<<4));
N2kMsg.Add2ByteDouble(Level,0.004);
N2kMsg.Add4ByteUDouble(Capacity,0.1);
N2kMsg.AddByte(0xff); // Reserved
}
//*****************************************************************************
bool ParseN2kPGN127505(const tN2kMsg &N2kMsg, unsigned char &Instance, tN2kFluidType &FluidType, double &Level, double &Capacity) {
if (N2kMsg.PGN!=127505L) return false;
int Index=0;
unsigned char IFt=N2kMsg.GetByte(Index);
Instance=IFt&0x0f;
FluidType=(tN2kFluidType)((IFt>>4)&0x0f);
Level=N2kMsg.Get2ByteDouble(0.004,Index);
Capacity=N2kMsg.Get4ByteUDouble(0.1,Index);
return true;
}
//*****************************************************************************
// DC Detailed Status
//
void SetN2kPGN127506(tN2kMsg &N2kMsg, unsigned char SID, unsigned char DCInstance, tN2kDCType DCType,
uint8_t StateOfCharge, uint8_t StateOfHealth, double TimeRemaining, double RippleVoltage) {
N2kMsg.SetPGN(127506L);
N2kMsg.Priority=6;
N2kMsg.AddByte(SID);
N2kMsg.AddByte(DCInstance);
N2kMsg.AddByte((unsigned char)DCType);
N2kMsg.AddByte(StateOfCharge);
N2kMsg.AddByte(StateOfHealth);
N2kMsg.Add2ByteUDouble(TimeRemaining,1.0);
N2kMsg.Add2ByteUDouble(RippleVoltage,0.001);
}
//*****************************************************************************
bool ParseN2kPGN127506(const tN2kMsg &N2kMsg, unsigned char &SID, unsigned char &DCInstance, tN2kDCType &DCType,
uint8_t &StateOfCharge, uint8_t &StateOfHealth, double &TimeRemaining, double &RippleVoltage){
if (N2kMsg.PGN!=127506L) return false;
int Index=0;
SID=N2kMsg.GetByte(Index);
DCInstance=N2kMsg.GetByte(Index);
DCType=(tN2kDCType)(N2kMsg.GetByte(Index));
StateOfCharge=N2kMsg.GetByte(Index);
StateOfHealth=N2kMsg.GetByte(Index);
TimeRemaining=N2kMsg.Get2ByteUDouble(1.0,Index);
RippleVoltage=N2kMsg.Get2ByteUDouble(0.001,Index);
return true;
}
//*****************************************************************************
// Battery Status
// Temperatures should be in Kelvins
void SetN2kPGN127508(tN2kMsg &N2kMsg, unsigned char BatteryInstance, double BatteryVoltage, double BatteryCurrent,
double BatteryTemperature, unsigned char SID) {
N2kMsg.SetPGN(127508L);
N2kMsg.Priority=6;
N2kMsg.AddByte(BatteryInstance);
N2kMsg.Add2ByteDouble(BatteryVoltage,0.01);
N2kMsg.Add2ByteDouble(BatteryCurrent,0.1);
N2kMsg.Add2ByteUDouble(BatteryTemperature,0.01);
N2kMsg.AddByte(SID);
}
//*****************************************************************************
bool ParseN2kPGN127508(const tN2kMsg &N2kMsg, unsigned char &BatteryInstance, double &BatteryVoltage, double &BatteryCurrent,
double &BatteryTemperature, unsigned char &SID) {
if (N2kMsg.PGN!=127508L) return false;
int Index=0;
BatteryInstance=N2kMsg.GetByte(Index);
BatteryVoltage=N2kMsg.Get2ByteDouble(0.01,Index);
BatteryCurrent=N2kMsg.Get2ByteDouble(0.1,Index);
BatteryTemperature=N2kMsg.Get2ByteUDouble(0.01,Index);
SID=N2kMsg.GetByte(Index);
return true;
}
//*****************************************************************************
// Battery Configuration Status
void SetN2kPGN127513(tN2kMsg &N2kMsg, unsigned char BatInstance, tN2kBatType BatType, tN2kBatEqSupport SupportsEqual,
tN2kBatNomVolt BatNominalVoltage, tN2kBatChem BatChemistry, double BatCapacity, int8_t BatTemperatureCoefficient,
double PeukertExponent, int8_t ChargeEfficiencyFactor) {
N2kMsg.SetPGN(127513L);
N2kMsg.Priority=6;
N2kMsg.AddByte(BatInstance);
N2kMsg.AddByte(0xc0 | ((SupportsEqual & 0x03) << 4) | (BatType & 0x0f)); // BatType (4 bit), SupportsEqual (2 bit), Reserved (2 bit)
N2kMsg.AddByte( ((BatChemistry & 0x0f) << 4) | (BatNominalVoltage & 0x0f) ); // BatNominalVoltage (4 bit), BatChemistry (4 bit)
N2kMsg.Add2ByteUDouble(BatCapacity,3600);
N2kMsg.AddByte((int8_t)BatTemperatureCoefficient);
PeukertExponent-=1; // Is this right or not I am not yet sure!
if (PeukertExponent<0 || PeukertExponent>0.504) { N2kMsg.AddByte(0xff); } else { N2kMsg.Add1ByteUDouble(PeukertExponent,0.002,-1); }
N2kMsg.AddByte((int8_t)ChargeEfficiencyFactor);
}
//*****************************************************************************
bool ParseN2kPGN127513(const tN2kMsg &N2kMsg, unsigned char &BatInstance, tN2kBatType &BatType, tN2kBatEqSupport &SupportsEqual,
tN2kBatNomVolt &BatNominalVoltage, tN2kBatChem &BatChemistry, double &BatCapacity, int8_t &BatTemperatureCoefficient,
double &PeukertExponent, int8_t &ChargeEfficiencyFactor) {
if (N2kMsg.PGN!=127513L) return false;
int Index=0;
unsigned char v;
BatInstance = N2kMsg.GetByte(Index);
v = N2kMsg.GetByte(Index); BatType=(tN2kBatType)(v & 0x0f); SupportsEqual=(tN2kBatEqSupport)((v>>4) & 0x03);
v = N2kMsg.GetByte(Index); BatNominalVoltage=(tN2kBatNomVolt)(v & 0x0f); BatChemistry=(tN2kBatChem)((v>>4) & 0x0f);
BatCapacity=N2kMsg.Get2ByteDouble(3600,Index);
BatTemperatureCoefficient=N2kMsg.GetByte(Index);
PeukertExponent=N2kMsg.Get1ByteUDouble(0.002,Index); PeukertExponent+=1;
ChargeEfficiencyFactor=N2kMsg.GetByte(Index);
return true;
}
//*****************************************************************************
// Boat speed
void SetN2kPGN128259(tN2kMsg &N2kMsg, unsigned char SID, double WaterRefereced, double GroundReferenced, tN2kSpeedWaterReferenceType SWRT) {
N2kMsg.SetPGN(128259L);
N2kMsg.Priority=6;
N2kMsg.AddByte(SID);
N2kMsg.Add2ByteUDouble(WaterRefereced,0.01);
N2kMsg.Add2ByteUDouble(GroundReferenced,0.01);
N2kMsg.AddByte(SWRT);
N2kMsg.AddByte(0xff); // Reserved
}
bool ParseN2kPGN128259(const tN2kMsg &N2kMsg, unsigned char &SID, double &WaterRefereced, double &GroundReferenced, tN2kSpeedWaterReferenceType &SWRT) {
if (N2kMsg.PGN!=128259L) return false;
int Index=0;
SID=N2kMsg.GetByte(Index);
WaterRefereced=N2kMsg.Get2ByteUDouble(0.01,Index);
GroundReferenced=N2kMsg.Get2ByteDouble(0.01,Index);
SWRT=(tN2kSpeedWaterReferenceType)(N2kMsg.GetByte(Index)&0x0F);
return true;
}
//*****************************************************************************
// Water depth
void SetN2kPGN128267(tN2kMsg &N2kMsg, unsigned char SID, double DepthBelowTransducer, double Offset) {
N2kMsg.SetPGN(128267L);
N2kMsg.Priority=6;
N2kMsg.AddByte(SID);
N2kMsg.Add4ByteUDouble(DepthBelowTransducer,0.01);
N2kMsg.Add2ByteDouble(Offset,0.001);
N2kMsg.AddByte(0xff); // Reserved
}
bool ParseN2kPGN128267(const tN2kMsg &N2kMsg, unsigned char &SID, double &DepthBelowTransducer, double &Offset) {
if (N2kMsg.PGN!=128267L) return false;
int Index=0;
SID=N2kMsg.GetByte(Index);
DepthBelowTransducer=N2kMsg.Get4ByteUDouble(0.01,Index);
Offset=N2kMsg.Get2ByteDouble(0.001,Index);
return true;
}
//*****************************************************************************
// Distance log
void SetN2kPGN128275(tN2kMsg &N2kMsg, uint16_t DaysSince1970, double SecondsSinceMidnight, uint32_t Log, uint32_t TripLog) {
N2kMsg.SetPGN(128275L);
N2kMsg.Priority=6;
N2kMsg.Add2ByteInt(DaysSince1970);
N2kMsg.Add4ByteUDouble(SecondsSinceMidnight,0.0001);
N2kMsg.Add4ByteUInt(Log);
N2kMsg.Add4ByteUInt(TripLog);
}
bool ParseN2kPGN128275(const tN2kMsg &N2kMsg, uint16_t &DaysSince1970, double &SecondsSinceMidnight, uint32_t &Log, uint32_t &TripLog) {
if (N2kMsg.PGN!=128275L) return false;
int Index=0;
DaysSince1970=N2kMsg.Get2ByteUInt(Index);
SecondsSinceMidnight=N2kMsg.Get4ByteDouble(0.0001,Index);
Log=N2kMsg.Get4ByteUDouble(1,Index);
TripLog=N2kMsg.Get4ByteUDouble(1,Index);
return true;
}
//*****************************************************************************
// Lat long rapid
void SetN2kPGN129025(tN2kMsg &N2kMsg, double Latitude, double Longitude) {
N2kMsg.SetPGN(129025L);
N2kMsg.Priority=3;
N2kMsg.Add4ByteDouble(Latitude,1e-7);
N2kMsg.Add4ByteDouble(Longitude,1e-7);
}
bool ParseN2kPGN129025(const tN2kMsg &N2kMsg, double &Latitude, double &Longitude) {
if (N2kMsg.PGN!=129025L) return false;
int Index = 0;
Latitude=N2kMsg.Get4ByteDouble(1e-7, Index);
Longitude=N2kMsg.Get4ByteDouble(1e-7, Index);
return true;
}
//*****************************************************************************
// COG SOG rapid
// COG should be in radians
// SOG should be in m/s
void SetN2kPGN129026(tN2kMsg &N2kMsg, unsigned char SID, tN2kHeadingReference ref, double COG, double SOG) {
N2kMsg.SetPGN(129026L);
N2kMsg.Priority=3;
N2kMsg.AddByte(SID);
N2kMsg.AddByte( (((unsigned char)(ref)) & 0x03) | 0xfc );
N2kMsg.Add2ByteUDouble(COG,0.0001); //0.0057295779513082332);
N2kMsg.Add2ByteUDouble(SOG,0.01);
N2kMsg.AddByte(0xff); // Reserved
N2kMsg.AddByte(0xff); // Reserved
}
bool ParseN2kPGN129026(const tN2kMsg &N2kMsg, unsigned char &SID, tN2kHeadingReference &ref, double &COG, double &SOG) {
if (N2kMsg.PGN!=129026L) return false;
int Index=0;
unsigned char b;
SID=N2kMsg.GetByte(Index);
b=N2kMsg.GetByte(Index); ref=(tN2kHeadingReference)( b & 0x03 );
COG=N2kMsg.Get2ByteUDouble(0.0001,Index);
SOG=N2kMsg.Get2ByteUDouble(0.01,Index);
return true;
}
//*****************************************************************************
// GNSS Position Data
void SetN2kPGN129029(tN2kMsg &N2kMsg, unsigned char SID, uint16_t DaysSince1970, double SecondsSinceMidnight,
double Latitude, double Longitude, double Altitude,
tN2kGNSStype GNSStype, tN2kGNSSmethod GNSSmethod,
unsigned char nSatellites, double HDOP, double PDOP, double GeoidalSeparation,
unsigned char nReferenceStations, tN2kGNSStype ReferenceStationType, uint16_t ReferenceSationID,
double AgeOfCorrection
) {
N2kMsg.SetPGN(129029L);
N2kMsg.Priority=6;
N2kMsg.AddByte(SID);
N2kMsg.Add2ByteInt(DaysSince1970);
N2kMsg.Add4ByteUDouble(SecondsSinceMidnight,0.0001);
N2kMsg.Add8ByteDouble(Latitude,1e-16);
N2kMsg.Add8ByteDouble(Longitude,1e-16);
N2kMsg.Add8ByteDouble(Altitude,1e-6);
N2kMsg.AddByte( (((unsigned char) GNSStype) & 0x0f) | (((unsigned char) GNSSmethod) & 0x0f)<<4 );
N2kMsg.AddByte(1); // Integrity 2 bit, reserved 6 bits
N2kMsg.AddByte(nSatellites);
N2kMsg.Add2ByteDouble(HDOP,0.01);
N2kMsg.Add2ByteDouble(PDOP,0.01);
N2kMsg.Add4ByteDouble(GeoidalSeparation,0.01);
if (nReferenceStations!=0xff && nReferenceStations>0) {
N2kMsg.AddByte(1); // Note that we have values for only one reference station, so pass only one values.
N2kMsg.Add2ByteInt( (((int)ReferenceStationType) & 0x0f) | ReferenceSationID<<4 );
N2kMsg.Add2ByteUDouble(AgeOfCorrection,0.01);
} else N2kMsg.AddByte(nReferenceStations);
}
bool ParseN2kPGN129029(const tN2kMsg &N2kMsg, unsigned char &SID, uint16_t &DaysSince1970, double &SecondsSinceMidnight,
double &Latitude, double &Longitude, double &Altitude,
tN2kGNSStype &GNSStype, tN2kGNSSmethod &GNSSmethod,
uint8_t &nSatellites, double &HDOP, double &PDOP, double &GeoidalSeparation,
uint8_t &nReferenceStations, tN2kGNSStype &ReferenceStationType, uint16_t &ReferenceSationID,
double &AgeOfCorrection
) {
if (N2kMsg.PGN!=129029L) return false;
int Index=0;
unsigned char vb;
int16_t vi;
SID=N2kMsg.GetByte(Index);
DaysSince1970=N2kMsg.Get2ByteUInt(Index);
SecondsSinceMidnight=N2kMsg.Get4ByteDouble(0.0001,Index);
Latitude=N2kMsg.Get8ByteDouble(1e-16,Index);
Longitude=N2kMsg.Get8ByteDouble(1e-16,Index);
Altitude=N2kMsg.Get8ByteDouble(1e-6,Index);
vb=N2kMsg.GetByte(Index); GNSStype=(tN2kGNSStype)(vb & 0x0f); GNSSmethod=(tN2kGNSSmethod)((vb>>4) & 0x0f);
vb=N2kMsg.GetByte(Index); // Integrity 2 bit, reserved 6 bits
nSatellites=N2kMsg.GetByte(Index);
HDOP=N2kMsg.Get2ByteDouble(0.01,Index);
PDOP=N2kMsg.Get2ByteDouble(0.01,Index);
GeoidalSeparation=N2kMsg.Get4ByteDouble(0.01,Index);
nReferenceStations=N2kMsg.GetByte(Index);
if (nReferenceStations!=N2kUInt8NA && nReferenceStations>0) {
// Note that we return real number of stations, but we only have variabes for one.
vi=N2kMsg.Get2ByteUInt(Index); ReferenceStationType=(tN2kGNSStype)(vi & 0x0f); ReferenceSationID=(vi>>4);
AgeOfCorrection=N2kMsg.Get2ByteUDouble(0.01,Index);
}
return true;
}
//*****************************************************************************
// AIS position report (class A 129038)
void SetN2kPGN129038(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRepeat Repeat, uint32_t UserID,
double Latitude, double Longitude, bool Accuracy, bool RAIM, uint8_t Seconds,
double COG, double SOG, double Heading, double ROT, tN2kAISNavStatus NavStatus)
{
N2kMsg.SetPGN(129038L);
N2kMsg.Priority=6;
N2kMsg.AddByte((Repeat & 0x03)<<6 | (MessageID & 0x3f));
N2kMsg.Add4ByteUInt(UserID);
N2kMsg.Add4ByteDouble(Longitude, 1e-07);
N2kMsg.Add4ByteDouble(Latitude, 1e-07);
N2kMsg.AddByte((Seconds & 0x3f)<<2 | (RAIM & 0x01)<<1 | (Accuracy & 0x01));
N2kMsg.Add2ByteUDouble(COG, 1e-04);
N2kMsg.Add2ByteUDouble(SOG, 0.01);
N2kMsg.AddByte(0xff); // Communication State (19 bits)
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff); // AIS transceiver information (5 bits)
N2kMsg.Add2ByteUDouble(Heading, 1e-04);
N2kMsg.Add2ByteDouble(ROT, ((1e-3/32.0) * 0.0001));
N2kMsg.AddByte(0xF0 | (NavStatus & 0x0f));
N2kMsg.AddByte(0xff); // Reserved
}
bool ParseN2kPGN129038(const tN2kMsg &N2kMsg, uint8_t &MessageID, tN2kAISRepeat &Repeat, uint32_t &UserID,
double &Latitude, double &Longitude, bool &Accuracy, bool &RAIM, uint8_t &Seconds,
double &COG, double &SOG, double &Heading, double &ROT, tN2kAISNavStatus &NavStatus)
{
if (N2kMsg.PGN!=129038L) return false;
int Index=0;
unsigned char vb;
vb=N2kMsg.GetByte(Index); MessageID=(vb & 0x3f); Repeat=(tN2kAISRepeat)(vb>>6 & 0x03);
UserID=N2kMsg.Get4ByteUInt(Index);
Longitude=N2kMsg.Get4ByteDouble(1e-07, Index);
Latitude=N2kMsg.Get4ByteDouble(1e-07, Index);
vb=N2kMsg.GetByte(Index); Accuracy=(vb & 0x01); RAIM=(vb>>1 & 0x01); Seconds=(vb>>2 & 0x3f);
COG=N2kMsg.Get2ByteUDouble(1e-04, Index);
SOG=N2kMsg.Get2ByteUDouble(0.01, Index);
vb=N2kMsg.GetByte(Index); // Communication State (19 bits)
vb=N2kMsg.GetByte(Index);
vb=N2kMsg.GetByte(Index); // AIS transceiver information (5 bits)
Heading=N2kMsg.Get2ByteUDouble(1e-04, Index);
ROT=N2kMsg.Get2ByteDouble(((1e-3/32.0) * 0.0001), Index);
vb=N2kMsg.GetByte(Index); NavStatus=(tN2kAISNavStatus)(vb & 0x0f);
vb=N2kMsg.GetByte(Index); // Reserved
return true;
}
//*****************************************************************************
// AIS position report (class B 129039)
void SetN2kPGN129039(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRepeat Repeat, uint32_t UserID,
double Latitude, double Longitude, bool Accuracy, bool RAIM,
uint8_t Seconds, double COG, double SOG, double Heading, tN2kAISUnit Unit,
bool Display, bool DSC, bool Band, bool Msg22, tN2kAISMode Mode, bool State)
{
N2kMsg.SetPGN(129039L);
N2kMsg.Priority=6;
N2kMsg.AddByte((Repeat & 0x03)<<6 | (MessageID & 0x3f));
N2kMsg.Add4ByteUInt(UserID);
N2kMsg.Add4ByteDouble(Longitude, 1e-07);
N2kMsg.Add4ByteDouble(Latitude, 1e-07);
N2kMsg.AddByte((Seconds & 0x3f)<<2 | (RAIM & 0x01)<<1 | (Accuracy & 0x01));
N2kMsg.Add2ByteUDouble(COG, 1e-04);
N2kMsg.Add2ByteUDouble(SOG, 0.01);
N2kMsg.AddByte(0xff); // Communication State (19 bits)
N2kMsg.AddByte(0xff);
N2kMsg.AddByte(0xff); // AIS transceiver information (5 bits)
N2kMsg.Add2ByteUDouble(Heading, 1e-04);
N2kMsg.AddByte(0xff); // Regional application
N2kMsg.AddByte((Mode & 0x01)<<7 | (Msg22 & 0x01)<<6 | (Band & 0x01)<<5 |
(DSC & 0x01)<<4 | (Display & 0x01)<<3 | (Unit & 0x01)<<2);
N2kMsg.AddByte(0xfe | (State & 0x01));
}
bool ParseN2kPGN129039(const tN2kMsg &N2kMsg, uint8_t &MessageID, tN2kAISRepeat &Repeat, uint32_t &UserID,
double &Latitude, double &Longitude, bool &Accuracy, bool &RAIM,
uint8_t &Seconds, double &COG, double &SOG, double &Heading, tN2kAISUnit &Unit,
bool &Display, bool &DSC, bool &Band, bool &Msg22, tN2kAISMode &Mode, bool &State)
{
if (N2kMsg.PGN!=129039L) return false;
int Index=0;
unsigned char vb;
vb=N2kMsg.GetByte(Index); MessageID=(vb & 0x3f); Repeat=(tN2kAISRepeat)(vb>>6 & 0x03);
UserID=N2kMsg.Get4ByteUInt(Index);
Longitude=N2kMsg.Get4ByteDouble(1e-07, Index);
Latitude=N2kMsg.Get4ByteDouble(1e-07, Index);
vb=N2kMsg.GetByte(Index); Accuracy=(vb & 0x01); RAIM=(vb>>1 & 0x01); Seconds=(vb>>2 & 0x3f);
COG=N2kMsg.Get2ByteUDouble(1e-04, Index);
SOG=N2kMsg.Get2ByteUDouble(0.01, Index);
vb=N2kMsg.GetByte(Index); // Communication State (19 bits)
vb=N2kMsg.GetByte(Index);
vb=N2kMsg.GetByte(Index); // AIS transceiver information (5 bits)
Heading=N2kMsg.Get2ByteUDouble(1e-04, Index);
vb=N2kMsg.GetByte(Index); // Regional application
vb=N2kMsg.GetByte(Index);
Unit=(tN2kAISUnit)(vb>>2 & 0x01); Display=(vb>>3 & 0x01); DSC=(vb>>4 & 0x01);
Band=(vb>>5 & 0x01); Msg22=(vb>>6 & 0x01); Mode=(tN2kAISMode)(vb>>7 & 0x01);
vb=N2kMsg.GetByte(Index); State=(vb & 0x01);
return true;
}
//*****************************************************************************
// Cross Track Error
void SetN2kPGN129283(tN2kMsg &N2kMsg, unsigned char SID, tN2kXTEMode XTEMode, bool NavigationTerminated, double XTE) {
N2kMsg.SetPGN(129283L);
N2kMsg.Priority=6;
N2kMsg.AddByte(SID);
N2kMsg.AddByte((char)XTEMode | (NavigationTerminated?0x40:0));
N2kMsg.Add4ByteDouble(XTE,0.01);
N2kMsg.AddByte(0xff); // Reserved
N2kMsg.AddByte(0xff); // Reserved
}
//*****************************************************************************
// Navigation info
void SetN2kPGN129284(tN2kMsg &N2kMsg, unsigned char SID, double DistanceToWaypoint, tN2kHeadingReference BearingReference,
bool PerpendicularCrossed, bool ArrivalCircleEntered, tN2kDistanceCalculationType CalculationType,
double ETATime, int16_t ETADate, double BearingOriginToDestinationWaypoint, double BearingPositionToDestinationWaypoint,
uint8_t OriginWaypointNumber, uint8_t DestinationWaypointNumber,
double DestinationLatitude, double DestinationLongitude, double WaypointClosingVelocity) {
N2kMsg.SetPGN(129284L);
N2kMsg.Priority=6;
N2kMsg.AddByte(SID);
N2kMsg.Add4ByteUDouble(DistanceToWaypoint,0.01);
N2kMsg.AddByte((char)BearingReference | (PerpendicularCrossed?0x04:0) | (ArrivalCircleEntered?0x10:0) | (CalculationType==N2kdct_RhumbLine?0x40:0));
N2kMsg.Add4ByteUDouble(ETATime,0.0001);
N2kMsg.Add2ByteUInt(ETADate);
N2kMsg.Add2ByteUDouble(BearingOriginToDestinationWaypoint,0.0001);
N2kMsg.Add2ByteUDouble(BearingPositionToDestinationWaypoint,0.0001);
N2kMsg.Add4ByteUInt(OriginWaypointNumber);
N2kMsg.Add4ByteUInt(DestinationWaypointNumber);
N2kMsg.Add4ByteDouble(DestinationLatitude,1e-07);
N2kMsg.Add4ByteDouble(DestinationLongitude,1e-07);
N2kMsg.Add2ByteDouble(WaypointClosingVelocity,0.01);
}
//*****************************************************************************
// Waypoint list
void SetN2kPGN129285(tN2kMsg &N2kMsg, uint16_t Start, uint16_t Database, uint16_t Route,
bool NavDirection, bool SupplementaryData, char* RouteName) {
unsigned int i;
N2kMsg.SetPGN(129285L);
N2kMsg.Priority=6;
N2kMsg.Add2ByteUInt(Start);
N2kMsg.Add2ByteUInt(0); // number of items initially 0
N2kMsg.Add2ByteUInt(Database);
N2kMsg.Add2ByteUInt(Route);
N2kMsg.AddByte(0xC0 | (SupplementaryData & 0x03)<<4 | (NavDirection & 0x0F));
if (strlen(RouteName) == 0) {
N2kMsg.AddByte(0x03);N2kMsg.AddByte(0x01);N2kMsg.AddByte(0x00);
} else {
N2kMsg.AddByte(strlen(RouteName)+2);N2kMsg.AddByte(0x01);
for (i=0; i<strlen(RouteName); i++)
N2kMsg.AddByte(RouteName[i]);
}
N2kMsg.AddByte(0xff); // reserved
}
bool AppendN2kPGN129285(tN2kMsg &N2kMsg, uint16_t ID, char* Name, double Latitude, double Longitude) {
if (N2kMsg.PGN!=129285L) return false;
unsigned int i;
int NumItemsIdx, len;
uint16_t NumItems;
if (strlen(Name) > 0)
len = 12 + strlen(Name);
else
len = 13;
if (N2kMsg.DataLen + len < N2kMsg.MaxDataLen) {
NumItemsIdx = 2;
NumItems = N2kMsg.Get2ByteUInt(NumItemsIdx); // get and increment the number of items
NumItemsIdx = 2;
N2kMsg.Set2ByteUInt(++NumItems, NumItemsIdx); // increment the number of items
N2kMsg.Add2ByteUInt(ID); // add the new item
if (strlen(Name) == 0) {
N2kMsg.AddByte(0x03);N2kMsg.AddByte(0x01);N2kMsg.AddByte(0x00);
} else {
N2kMsg.AddByte(strlen(Name)+2);N2kMsg.AddByte(0x01);
for (i=0; i<strlen(Name); i++)
N2kMsg.AddByte(Name[i]);
}
N2kMsg.Add4ByteDouble(Latitude,1e-07);
N2kMsg.Add4ByteDouble(Longitude,1e-07);
return true;
} else
return false;
}
//*****************************************************************************
// AIS static data A
void SetN2kPGN129794(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRepeat Repeat, uint32_t UserID,
uint32_t IMOnumber, char *Callsign, char *Name, uint8_t VesselType, double Length,
double Beam, double PosRefStbd, double PosRefBow, uint16_t ETAdate, double ETAtime,
double Draught, char *Destination, tN2kAISVersion AISversion, tN2kGNSStype GNSStype,
tN2kAISDTE DTE, tN2kAISTranceiverInfo AISinfo)
{
N2kMsg.SetPGN(129794L);
N2kMsg.Priority=6;
N2kMsg.AddByte((Repeat & 0x03)<<6 | (MessageID & 0x3f));
N2kMsg.Add4ByteUInt(UserID);
N2kMsg.Add4ByteUInt(IMOnumber);
N2kMsg.AddStr(Callsign, 7);
N2kMsg.AddStr(Name, 20);
N2kMsg.AddByte(VesselType);
N2kMsg.Add2ByteDouble(Length, 0.1);
N2kMsg.Add2ByteDouble(Beam, 0.1);
N2kMsg.Add2ByteDouble(PosRefStbd, 0.1);
N2kMsg.Add2ByteDouble(PosRefBow, 0.1);
N2kMsg.Add2ByteUInt(ETAdate);
N2kMsg.Add4ByteUDouble(ETAtime, 0.0001);
N2kMsg.Add2ByteDouble(Draught, 0.01);
N2kMsg.AddStr(Destination, 20);
N2kMsg.AddByte((DTE & 0x01)<<6 | (GNSStype & 0x0f)<<2 | (AISversion & 0x03));
N2kMsg.AddByte(AISinfo & 0x1f);
}
bool ParseN2kPGN129794(const tN2kMsg &N2kMsg, uint8_t &MessageID, tN2kAISRepeat &Repeat, uint32_t &UserID,
uint32_t &IMOnumber, char *Callsign, char *Name, uint8_t &VesselType, double &Length,
double &Beam, double &PosRefStbd, double &PosRefBow, uint16_t &ETAdate, double &ETAtime,
double &Draught, char *Destination, tN2kAISVersion &AISversion, tN2kGNSStype &GNSStype,
tN2kAISDTE &DTE, tN2kAISTranceiverInfo &AISinfo)
{
if (N2kMsg.PGN!=129794L) return false;
int Index=0;
unsigned char vb;
vb=N2kMsg.GetByte(Index); MessageID=(vb & 0x3f); Repeat=(tN2kAISRepeat)(vb>>6 & 0x03);
UserID=N2kMsg.Get4ByteUInt(Index);
IMOnumber=N2kMsg.Get4ByteUInt(Index);
N2kMsg.GetStr(Callsign, 7, Index);
N2kMsg.GetStr(Name, 20, Index);
VesselType=N2kMsg.GetByte(Index);
Length=N2kMsg.Get2ByteDouble(0.1, Index);
Beam=N2kMsg.Get2ByteDouble(0.1, Index);
PosRefStbd=N2kMsg.Get2ByteDouble(0.1, Index);
PosRefBow=N2kMsg.Get2ByteDouble(0.1, Index);
ETAdate=N2kMsg.Get2ByteUInt(Index);
ETAtime=N2kMsg.Get4ByteUDouble(0.0001, Index);
Draught=N2kMsg.Get2ByteDouble(0.01, Index);
N2kMsg.GetStr(Destination, 20, Index);
vb=N2kMsg.GetByte(Index); AISversion=(tN2kAISVersion)(vb & 0x03); GNSStype=(tN2kGNSStype)(vb>>2 & 0x0f); DTE=(tN2kAISDTE)(vb>>6 & 0x01);
vb=N2kMsg.GetByte(Index); AISinfo=(tN2kAISTranceiverInfo)(vb & 0x1f);
return true;
}
//*****************************************************************************
// AIS static data class B part A
void SetN2kPGN129809(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRepeat Repeat, uint32_t UserID, char *Name)
{
N2kMsg.SetPGN(129809L);
N2kMsg.Priority=6;
N2kMsg.AddByte((Repeat & 0x03)<<6 | (MessageID & 0x3f));
N2kMsg.Add4ByteUInt(UserID);
N2kMsg.AddStr(Name, 20);
}
bool ParseN2kPGN129809(const tN2kMsg &N2kMsg, uint8_t &MessageID, tN2kAISRepeat &Repeat, uint32_t &UserID, char *Name)
{
if (N2kMsg.PGN!=129809L) return false;
int Index=0;
unsigned char vb;
vb=N2kMsg.GetByte(Index); MessageID=(vb & 0x3f); Repeat=(tN2kAISRepeat)(vb>>6 & 0x03);
UserID=N2kMsg.Get4ByteUInt(Index);
N2kMsg.GetStr(Name, 20, Index);
return true;
}
//*****************************************************************************
// AIS static data class B part B
void SetN2kPGN129810(tN2kMsg &N2kMsg, uint8_t MessageID, tN2kAISRepeat Repeat, uint32_t UserID,
uint8_t VesselType, char *Vendor, char *Callsign, double Length, double Beam,
double PosRefStbd, double PosRefBow, uint32_t MothershipID)
{
N2kMsg.SetPGN(129810L);
N2kMsg.Priority=6;
N2kMsg.AddByte((Repeat & 0x03)<<6 | (MessageID & 0x3f));
N2kMsg.Add4ByteUInt(UserID);
N2kMsg.AddByte(VesselType);
N2kMsg.AddStr(Vendor, 7);
N2kMsg.AddStr(Callsign, 7);
N2kMsg.Add2ByteUDouble(Length, 0.1);
N2kMsg.Add2ByteUDouble(Beam, 0.1);
N2kMsg.Add2ByteUDouble(PosRefStbd, 0.1);
N2kMsg.Add2ByteUDouble(PosRefBow, 0.1);
N2kMsg.Add4ByteUInt(MothershipID);
N2kMsg.AddByte(0xff); // Reserved
}
bool ParseN2kPGN129810(const tN2kMsg &N2kMsg, uint8_t &MessageID, tN2kAISRepeat &Repeat, uint32_t &UserID,
uint8_t &VesselType, char *Vendor, char *Callsign, double &Length, double &Beam,
double &PosRefStbd, double &PosRefBow, uint32_t &MothershipID)
{
if (N2kMsg.PGN!=129810L) return false;
int Index=0;
unsigned char vb;
vb=N2kMsg.GetByte(Index); MessageID=(vb & 0x3f); Repeat=(tN2kAISRepeat)(vb>>6 & 0x03);
UserID=N2kMsg.Get4ByteUInt(Index);
VesselType=N2kMsg.GetByte(Index);
N2kMsg.GetStr(Vendor, 7, Index);
N2kMsg.GetStr(Callsign, 7, Index);
Length = N2kMsg.Get2ByteUDouble(0.1, Index);
Beam = N2kMsg.Get2ByteUDouble(0.1, Index);
PosRefStbd = N2kMsg.Get2ByteUDouble(0.1, Index);
PosRefBow = N2kMsg.Get2ByteUDouble(0.1, Index);
MothershipID = N2kMsg.Get4ByteUInt(Index);
return true;
}
//*****************************************************************************
// Waypoint list
void SetN2kPGN130074(tN2kMsg &N2kMsg, uint16_t Start, uint16_t NumWaypoints, uint16_t Database) {
N2kMsg.SetPGN(130074L);
N2kMsg.Priority=6;
N2kMsg.Add2ByteUInt(Start);
N2kMsg.Add2ByteUInt(0); // set number of items to 0 initially
N2kMsg.Add2ByteUInt(NumWaypoints);
N2kMsg.Add2ByteUInt(Database);
N2kMsg.AddByte(0xff); // Reserved
N2kMsg.AddByte(0xff); // Reserved
}
bool AppendN2kPGN130074(tN2kMsg &N2kMsg, uint16_t ID, char* Name, double Latitude, double Longitude) {
if (N2kMsg.PGN!=130074L) return false;
unsigned int i;
int NumItemsIdx, len;
uint16_t NumItems;
if (strlen(Name) > 0)
len = 12 + strlen(Name);
else
len = 13;
if (N2kMsg.DataLen + len < N2kMsg.MaxDataLen) {
NumItemsIdx = 2;
NumItems = N2kMsg.Get2ByteUInt(NumItemsIdx); // get and increment the number of items
NumItemsIdx = 2;
N2kMsg.Set2ByteUInt(++NumItems, NumItemsIdx); // increment the number of items
N2kMsg.Add2ByteUInt(ID);
if (strlen(Name) == 0) {
N2kMsg.AddByte(0x03);N2kMsg.AddByte(0x01);N2kMsg.AddByte(0x00);
} else {
N2kMsg.AddByte(strlen(Name)+2);N2kMsg.AddByte(0x01);
for (i=0; i<strlen(Name); i++)
N2kMsg.AddByte(Name[i]);
}
N2kMsg.Add4ByteDouble(Latitude,1e-07);
N2kMsg.Add4ByteDouble(Longitude,1e-07);
return true;
} else
return false;
}
//*****************************************************************************
// Wind Speed
void SetN2kPGN130306(tN2kMsg &N2kMsg, unsigned char SID, double WindSpeed, double WindAngle, tN2kWindReference WindReference) {
N2kMsg.SetPGN(130306L);
N2kMsg.Priority=6;
N2kMsg.AddByte(SID);
N2kMsg.Add2ByteUDouble(WindSpeed,0.01);
N2kMsg.Add2ByteUDouble(WindAngle,0.0001);
N2kMsg.AddByte((unsigned char)WindReference);
//N2kMsg.AddByte(0xff); // Reserved
//N2kMsg.AddByte(0xff); // Reserved
}
bool ParseN2kPGN130306(const tN2kMsg &N2kMsg, unsigned char &SID, double &WindSpeed, double &WindAngle, tN2kWindReference &WindReference) {
if (N2kMsg.PGN!=130306L) return false;
int Index=0;
SID=N2kMsg.GetByte(Index);
WindSpeed=N2kMsg.Get2ByteUDouble(0.01,Index);
WindAngle=N2kMsg.Get2ByteUDouble(0.0001,Index);
WindReference=(tN2kWindReference)(N2kMsg.GetByte(Index)&0x07);
return true;
}
//*****************************************************************************
// Outside Environmental parameters
void SetN2kPGN130310(tN2kMsg &N2kMsg, unsigned char SID, double WaterTemperature,
double OutsideAmbientAirTemperature, double AtmosphericPressure) {
N2kMsg.SetPGN(130310L);
N2kMsg.Priority=6;
N2kMsg.AddByte(SID);
N2kMsg.Add2ByteUDouble(WaterTemperature,0.01);
N2kMsg.Add2ByteUDouble(OutsideAmbientAirTemperature,0.01);
N2kMsg.Add2ByteUDouble(AtmosphericPressure,100);
N2kMsg.AddByte(0xff); // reserved
}