This repository was archived by the owner on Aug 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPS.pas
More file actions
1688 lines (1475 loc) · 43.1 KB
/
Copy pathGPS.pas
File metadata and controls
1688 lines (1475 loc) · 43.1 KB
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
(******************************************************************************)
(* Copyright © 2010, PLAIS Lionel All rights reserved. *)
(* *)
(* Redistribution and use in source and binary forms, with or without *)
(* modification, are permitted provided that the following conditions are *)
(* met: *)
(* *)
(* Redistributions of source code must retain the above copyright notice, *)
(* this list of conditions and the following disclaimer. Redistributions in *)
(* binary form must reproduce the above copyright notice, this list of *)
(* conditions and the following disclaimer in the documentation and/or *)
(* other materials provided with the distribution. Neither the name of the *)
(* ILP-WEB NETWORK nor the names of its contributors may be used to endorse *)
(* or promote products derived from this software without specific prior *)
(* written permission. *)
(* *)
(* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS *)
(* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *)
(* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *)
(* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *)
(* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *)
(* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED *)
(* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *)
(* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *)
(* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *)
(* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *)
(* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *)
(******************************************************************************)
(******************************************************************************)
(* File: GPS.pas *)
(* Component(s): TGPS *)
(* Components for the management of the NMEA 0183 frames received on COM *)
(* ports. *)
(* Copyright © 2010 ILP-WEB NETWORK *)
(* Author(s) : PLAIS Lionel (lionel.plais@ilp-web.net) *)
(******************************************************************************)
unit GPS;
{$D+}
interface
uses
SysUtils, Classes, Windows, CPortTypes, CPort, Math, StrUtils, ConvUtils,
StdConvs, DateUtils, Forms, StdCtrls, Controls, Graphics, Imglist, Messages,
GPX;
const
// Maximum number of satellites referenced
MAX_SATS = 12;
// Characters starting and ending frames NMEA 0183
MSG_START: String = '$';
MSG_STOP: String = '*';
// Default seconds between two segments
SEC_BETWEEN_SEG = 5;
type
// Satellite properties
TSatellite = record
Identification: Shortint;
Elevation: 0..90;
Azimut: Smallint;
SignLevel: Smallint;
end;
// Array of satellites referenced
TSatellites = array[1..MAX_SATS] of TSatellite;
TGPSSatEvent = procedure(Sender: TObject; NbSat, NbSatUse: Shortint;
Sats: TSatellites) of object;
// GPS status informations
TGPSDatas = record
Latitude: Double;
Longitude: Double;
HeightAboveSea: Double;
Speed: Double;
UTCTime: TDateTime;
Valid: Boolean;
NbrSats: Shortint;
NbrSatsUsed: Shortint;
Course: Double;
end;
TGPSDatasEvent = procedure(Sender: TObject; GPSDatas: TGPSDatas) of object;
// NMEA 0185's messages used
TMsgGP = (
msgGP, // Unknown message
msgGPGGA, // Global Positioning System Fix Data
msgGPGLL, // Geographic position, Latitude and Longitude
msgGPGSV, // Satellites in view
msgGPRMA, // Recommended minimum specific GPS/Transit data Loran C
msgGPRMC, // Recommended minimum specific GPS/Transit data
msgGPZDA // Date and time
);
// Speed units
TSpeedUnit = (suKilometre, suMile, suNauticalMile);
// GPS componants links
TGPSLink = class
private
FOnSatellitesChange: TGPSSatEvent;
FOnGPSDatasChange: TGPSDatasEvent;
FOnAfterOpen: TNotifyEvent;
FOnAfterClose: TNotifyEvent;
public
property OnSatellitesChange: TGPSSatEvent
read FOnSatellitesChange write FOnSatellitesChange;
property OnGPSDatasChange: TGPSDatasEvent
read FOnGPSDatasChange write FOnGPSDatasChange;
property OnAfterOpen: TNotifyEvent read FOnAfterOpen write FOnAfterOpen;
property OnAfterClose: TNotifyEvent read FOnAfterClose write FOnAfterClose;
end;
// The GPS base componant
TCustomGPS = class (TComponent)
private
// COM Port components
FCOMPort: TComPort;
FCOMDataPacket: TComDataPacket;
// State of the TGPS component
FConnected: Boolean;
// COM Port parameters
FPort: TPort;
FBaudRate: TBaudRate;
FDataBits: TDataBits;
FStopBits: TStopBits;
FParity: TComParity;
FFlowControl: TComFlowControl;
// GPS informations
FSatellites: TSatellites;
FGPSDatas: TGPSDatas;
// Componants links
FLinks: TList;
FHasLink: Boolean;
// Notifications
FOnSatellitesChange: TGPSSatEvent;
FOnGPSDatasChange: TGPSDatasEvent;
FOnAfterOpen: TNotifyEvent;
FOnAfterClose: TNotifyEvent;
procedure CallOnSatellitesChange();
procedure CallOnGPSDatasChange();
procedure SetConnected(const Value: Boolean);
procedure SetPort(const Value: TPort);
procedure SetBaudRate(const Value: TBaudRate);
procedure SetDataBits(const Value: TDataBits);
procedure SetStopBits(const Value: TStopBits);
procedure SetParity(const Value: TComParity);
procedure SetFlowControl(const Value: TComFlowControl);
// Packets interpreting
procedure PacketRecv(Sender: TObject; const Str: String);
// COM Port connection
procedure COMPortAfterConnect(Sender: TObject);
procedure COMPortAfterDisconnect(Sender: TObject);
// Componants links
function HasLink(): Boolean;
protected
procedure DoOnSatellitesChange(NbSat, NbSatUse: Shortint;
Sats: TSatellites); dynamic;
procedure DoOnGPSDatasChange(GPSDatas: TGPSDatas); dynamic;
procedure DoAfterClose(); dynamic;
procedure DoAfterOpen(); dynamic;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
procedure Open();
procedure Close();
procedure ShowSetupDialog();
// Componants links
procedure RegisterLink(AGPSLink: TGPSLink);
procedure UnRegisterLink(AGPSLink: TGPSLink);
// GPS informations
property Satellites: TSatellites read FSatellites;
property GPSDatas: TGPSDatas read FGPSDatas;
published
// State of the TGPS component
property Connected: Boolean
read FConnected write SetConnected default False;
// COM Port parameters
property Port: TPort read FPort write SetPort;
property BaudRate: TBaudRate read FBaudRate write SetBaudRate;
property DataBits: TDataBits read FDataBits write SetDataBits;
property StopBits: TStopBits read FStopBits write SetStopBits;
property Parity: TComParity read FParity write SetParity;
property FlowControl: TComFlowControl
read FFlowControl write SetFlowControl;
// Notifications
property OnSatellitesChange: TGPSSatEvent
read FOnSatellitesChange write FOnSatellitesChange;
property OnGPSDatasChange: TGPSDatasEvent
read FOnGPSDatasChange write FOnGPSDatasChange;
property OnAfterOpen: TNotifyEvent read FOnAfterOpen write FOnAfterOpen;
property OnAfterClose: TNotifyEvent read FOnAfterClose write FOnAfterClose;
end;
TGPS = class (TCustomGPS)
// State of the TGPS component
property Connected;
// COM Port parameters
property Port;
property BaudRate;
property DataBits;
property StopBits;
property Parity;
property FlowControl;
// Notifications
property OnSatellitesChange;
property OnGPSDatasChange;
property OnAfterOpen;
property OnAfterClose;
end;
TGPStoGPX = class (TComponent)
private
FActive: Boolean;
FGPS: TCustomGPS;
FGPSLink: TGPSLink;
FFileName: String;
FGPXFile: IXMLGpxType;
FGPXTrack: IXMLTrkType;
FGPXSeg: IXMLTrksegType;
LastFixTime: TDateTime;
procedure SetActive(const Value: Boolean);
procedure SetGPS(const Value: TCustomGPS);
procedure SetFileName(const Value: String);
procedure GPSChange(Sender: TObject; GPSDatas: TGPSDatas);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
procedure Connect();
procedure Disconnect();
published
property Active: Boolean read FActive write SetActive;
property GPS: TCustomGPS read FGPS write SetGPS;
property FileName: String read FFileName write SetFileName;
end;
TGPSSpeed = class (TGraphicControl)
private
FGPS: TCustomGPS;
FGPSLink: TGPSLink;
FSpeedUnit: TSpeedUnit;
procedure SetGPS(const Value: TCustomGPS);
procedure SetSpeedUnit(const Value: TSpeedUnit);
procedure SpeedChange(Sender: TObject; GPSDatas: TGPSDatas);
protected
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure Paint(); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
published
property GPS: TCustomGPS read FGPS write SetGPS;
property Font;
property SpeedUnit: TSpeedUnit
read FSpeedUnit write SetSpeedUnit default suKilometre;
end;
TGPSSatellitesPosition = class (TGraphicControl)
private
FGPS: TCustomGPS;
FGPSLink: TGPSLink;
ImgSats: TBitmap;
IML_Sats: TImageList;
FCardFont: TFont;
FSatFont: TFont;
FPen: TPen;
FBackgroundColor: TColor;
procedure SetGPS(const Value: TCustomGPS);
procedure SetCardFont(const Value: TFont);
procedure SetSatFont(const Value: TFont);
procedure SetPen(const Value: TPen);
procedure SetBackgroundColor(const Value: TColor);
procedure SatChange(Sender: TObject; NbSat, NbSatUse: Shortint;
Sats: TSatellites);
protected
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure Paint(); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
published
procedure StyleChanged(Sender: TObject);
property GPS: TCustomGPS read FGPS write SetGPS;
property CardFont: TFont read FCardFont write SetCardFont;
property SatFont: TFont read FSatFont write SetSatFont;
property Pen: TPen read FPen write SetPen;
property BackgroundColor: TColor read FBackgroundColor
write SetBackgroundColor default clWindow;
end;
TGPSSatellitesReception = class (TGraphicControl)
private
FGPS: TCustomGPS;
FGPSLink: TGPSLink;
FBackColor: TColor;
FBorderColor: TColor;
FForeColor: TColor;
ImgSats: TBitmap;
procedure SetGPS(const Value: TCustomGPS);
procedure SetBackColor(const Value: TColor);
procedure SetBorderColor(const Value: TColor);
procedure SetForeColor(const Value: TColor);
procedure SatChange(Sender: TObject; NbSat, NbSatUse: Shortint;
Sats: TSatellites);
protected
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure Paint(); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
published
procedure StyleChanged(Sender: TObject);
property GPS: TCustomGPS read FGPS write SetGPS;
property Font;
property BackColor: TColor
read FBackColor write SetBackColor stored True default clWindow;
property BorderColor: TColor
read FBorderColor write SetBorderColor stored True default clBlack;
property ForeColor: TColor
read FForeColor write SetForeColor stored True default clHotLight;
end;
TGPSCompass = class (TGraphicControl)
private
FGPS: TCustomGPS;
FGPSLink: TGPSLink;
ImgCompass: TBitmap;
FCardFont: TFont;
FPen: TPen;
FBrush: TBrush;
FBackgroundColor: TColor;
procedure SetGPS(const Value: TCustomGPS);
procedure SetCardFont(const Value: TFont);
procedure SetPen(const Value: TPen);
procedure SetBrush(const Value: TBrush);
procedure SetBackgroundColor(const Value: TColor);
procedure SatChange(Sender: TObject; NbSat, NbSatUse: Shortint;
Sats: TSatellites);
protected
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure Paint(); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
published
procedure StyleChanged(Sender: TObject);
property GPS: TCustomGPS read FGPS write SetGPS;
property CardFont: TFont read FCardFont write SetCardFont;
property Pen: TPen read FPen write SetPen;
property Brush: TBrush read FBrush write SetBrush;
property BackgroundColor: TColor read FBackgroundColor
write SetBackgroundColor default clWindow;
end;
procedure Register();
// Return the NMEA 0185's message type by its name
function IndexMsgGP(StrMsgGP: String): TMsgGP;
// Convert an angle string to its value
function StrCoordToAngle(Point: Char; Angle: String): Double;
// Convert a time string to its value
function StrTimeToTime(const Time: String): TDateTime;
// Convert an integer string to its value
function StrToInteger(const Str: String): Integer;
// Convert an real string to its value
function StrToReal(const Str: String): Extended;
function RotatePoint(Angle: Double; Ct, Pt: TPoint): TPoint;
procedure LoadRessource(RessourceName: String; ImageList: TImageList);
const
// NMEA 0185's messages used
LstMsgDiffGP: array[msgGPGGA..msgGPZDA] of String = (
'GGA', // Global Positioning System Fix Data
'GLL', // Geographic position, Latitude and Longitude
'GSV', // Satellites in view
'RMA', // Recommended minimum specific GPS/Transit data Loran C
'RMC', // Recommended minimum specific GPS/Transit data
'ZDA' // Date and time
);
var
// Number of satellites in view
SatRef: Smallint = 0;
implementation
{$R GPSIcons.res}
procedure Register();
begin
RegisterComponents('GPS',
[TGPS, TGPStoGPX, TGPSSpeed, TGPSSatellitesPosition,
TGPSSatellitesReception, TGPSCompass]);
end;
constructor TCustomGPS.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// Create componants links list
FLinks := TList.Create();
// Create ComPort component
FCOMPort := TComPort.Create(Nil);
// Get back the ComPort parameters
with FCOMPort do
begin
Port := FPort;
FBaudRate := BaudRate;
FDataBits := DataBits;
FStopBits := StopBits;
FParity := Parity;
FFlowControl := FlowControl;
// Implements triggers
OnAfterOpen := COMPortAfterConnect;
OnAfterClose := COMPortAfterDisconnect;
end;
// Create data packet's manager component
FCOMDataPacket := TComDataPacket.Create(Nil);
with FCOMDataPacket do
begin
// Specify characters starting and ending frames NMEA 0183
StartString := MSG_START;
StopString := MSG_STOP;
// Implements trigger for frames receiving
OnPacket := PacketRecv;
// Specify the ComPort used
ComPort := FCOMPort;
end;
end;
destructor TCustomGPS.Destroy();
begin
// Close le ComPort connexion (trying
)
Close();
// Free components
FCOMPort.Free();
FCOMDataPacket.Free();
inherited Destroy;
// Destroy componants links list
FLinks.Free();
end;
procedure TCustomGPS.Open();
begin
// Open the ComPort connection
FCOMPort.Open();
FConnected := FCOMPort.Connected;
end;
procedure TCustomGPS.Close();
begin
// Close the ComPort connection
FCOMPort.Close();
FConnected := FCOMPort.Connected;
end;
procedure TCustomGPS.ShowSetupDialog();
begin
// Show the ComPort setup dialog
FCOMPort.ShowSetupDialog();
end;
// Componants links
function TCustomGPS.HasLink(): Boolean;
var
i: Integer;
GPSLink: TGPSLink;
begin
Result := False;
// Examine links
if FLinks.Count > 0 then
for i := 0 to FLinks.Count - 1 do
begin
GPSLink := TGPSLink(FLinks[i]);
if Assigned(GPSLink.OnSatellitesChange) then
Result := True;
end;
end;
procedure TCustomGPS.RegisterLink(AGPSLink: TGPSLink);
begin
if FLinks.IndexOf(Pointer(AGPSLink)) > -1 then
raise Exception.Create('Link (un)registration failed')
else
FLinks.Add(Pointer(AGPSLink));
FHasLink := HasLink();
end;
procedure TCustomGPS.UnRegisterLink(AGPSLink: TGPSLink);
begin
if FLinks.IndexOf(Pointer(AGPSLink)) = -1 then
raise Exception.Create('Link (un)registration failed')
else
FLinks.Remove(Pointer(AGPSLink));
FHasLink := HasLink();
end;
procedure TCustomGPS.CallOnSatellitesChange();
var
i: Integer;
GPSLink: TGPSLink;
begin
// Trigger the satellites changing procedure
DoOnSatellitesChange(FGPSDatas.NbrSats, FGPSDatas.NbrSatsUsed, FSatellites);
for i := 0 to FLinks.Count - 1 do
begin
GPSLink := TGPSLink(FLinks[i]);
if Assigned(GPSLink.OnSatellitesChange) then
GPSLink.OnSatellitesChange(Self, FGPSDatas.NbrSats,
FGPSDatas.NbrSatsUsed, FSatellites);
end;
end;
procedure TCustomGPS.CallOnGPSDatasChange();
var
i: Integer;
GPSLink: TGPSLink;
begin
// Trigger the GPS datas changing procedure
DoOnGPSDatasChange(FGPSDatas);
for i := 0 to FLinks.Count - 1 do
begin
GPSLink := TGPSLink(FLinks[i]);
if Assigned(GPSLink.OnGPSDatasChange) then
GPSLink.OnGPSDatasChange(Self, FGPSDatas);
end;
end;
procedure TCustomGPS.SetConnected(const Value: Boolean);
begin
// Open or close the connection if the value change
if not ((csDesigning in ComponentState) or (csLoading in
ComponentState)) then
begin
if Value <> FConnected then
if Value then
Open()
else
Close();
end
else
FConnected := Value;
end;
procedure TCustomGPS.SetPort(const Value: TPort);
begin
FCOMPort.Port := Value;
FPort := FCOMPort.Port;
end;
procedure TCustomGPS.SetBaudRate(const Value: TBaudRate);
begin
FCOMPort.BaudRate := Value;
FBaudRate := FCOMPort.BaudRate;
end;
procedure TCustomGPS.SetDataBits(const Value: TDataBits);
begin
FCOMPort.DataBits := Value;
FDataBits := FCOMPort.DataBits;
end;
procedure TCustomGPS.SetStopBits(const Value: TStopBits);
begin
FCOMPort.StopBits := Value;
FStopBits := FCOMPort.StopBits;
end;
procedure TCustomGPS.SetParity(const Value: TComParity);
begin
FCOMPort.Parity := Value;
FParity := FCOMPort.Parity;
end;
procedure TCustomGPS.SetFlowControl(const Value: TComFlowControl);
begin
FCOMPort.FlowControl := Value;
FFlowControl := FCOMPort.FlowControl;
end;
procedure TCustomGPS.PacketRecv(Sender: TObject; const Str: String);
var
Resultat: TStringList;
MsgCorrect, TypeMsg: String;
i: Integer;
begin
Resultat := TStringList.Create();
try
// Split the message into different parts.
MsgCorrect := AnsiReplaceStr('$' + Str, ',,', ' , , ');
Resultat.Text := AnsiReplaceStr(
LeftStr(MsgCorrect, Length(MsgCorrect) - 1), ',', #13#10);
// Get the message type
TypeMsg := MidStr(Resultat[0], 4, 3);
// Retrieves data based on message type
case IndexMsgGP(TypeMsg) of
msgGPGGA:
begin
with FGPSDatas do
begin
UTCTime := StrTimeToTime(Resultat[1]);
Latitude := StrCoordToAngle(Resultat[3][1], Resultat[2]);
Longitude := StrCoordToAngle(Resultat[5][1], Resultat[4]);
HeightAboveSea := StrToReal(Resultat[9]);
NbrSatsUsed := StrToInteger(Resultat[7]);
end;
// Trigger change
CallOnGPSDatasChange();
end;
msgGPGLL:
begin
with FGPSDatas do
begin
UTCTime := StrTimeToTime(Resultat[5]);
Latitude := StrCoordToAngle(Resultat[2][1], Resultat[1]);
Longitude := StrCoordToAngle(Resultat[4][1], Resultat[3]);
Valid := AnsiSameText(Resultat[6], 'A');
end;
// Trigger change
CallOnGPSDatasChange();
end;
msgGPGSV:
begin
// If there are satellites in referenced in the frame
if Resultat.Count < 4 then
FGPSDatas.NbrSats := 0
else
FGPSDatas.NbrSats := StrToInteger(Resultat[3]);
if Resultat[2] = '1' then
begin
SatRef := 0;
// Initiate satellites values
for i := 1 to 12 do
with FSatellites[i] do
begin
Identification := 0;
Elevation := 0;
Azimut := 0;
SignLevel := 0;
end;
end;
i := 4;
// For each referenced satellites
while (i + 4) <= (Resultat.Count) do
begin
with FSatellites[SatRef + 1] do
begin
Identification := StrToInteger(Resultat[i]);
Elevation := StrToInteger(Resultat[i + 1]);
Azimut := StrToInteger(Resultat[i + 2]);
if Resultat[i + 3] <> '' then
SignLevel := StrToInteger(Resultat[i + 3])
else
SignLevel := 0;
end;
Inc(i, 4);
Inc(SatRef);
end;
// Trigger change
CallOnSatellitesChange();
end;
msgGPRMA:
begin
with FGPSDatas do
begin
Latitude := StrCoordToAngle(Resultat[3][1], Resultat[2]);
Longitude := StrCoordToAngle(Resultat[5][1], Resultat[4]);
Valid := AnsiSameText(Resultat[1], 'A');
Speed := Convert(StrToReal(Resultat[6]), duNauticalMiles,
duKilometers);
end;
// Trigger change
CallOnGPSDatasChange();
end;
msgGPRMC:
begin
with FGPSDatas do
begin
UTCTime := StrTimeToTime(Resultat[1]);
Latitude := StrCoordToAngle(Resultat[4][1], Resultat[3]);
Longitude := StrCoordToAngle(Resultat[6][1], Resultat[5]);
Valid := AnsiSameText(Resultat[2], 'A');
Speed := Convert(StrToReal(Resultat[7]), duNauticalMiles,
duKilometers);
Course := StrToReal(Resultat[8]);
end;
// Trigger change
CallOnGPSDatasChange();
end;
msgGPZDA:
begin
FGPSDatas.UTCTime := StrTimeToTime(Resultat[1]);
// Trigger change
CallOnGPSDatasChange();
end;
end;
finally
Resultat.Free();
end;
end;
procedure TCustomGPS.COMPortAfterConnect(Sender: TObject);
begin
FConnected := True;
DoAfterOpen();
end;
procedure TCustomGPS.COMPortAfterDisconnect(Sender: TObject);
begin
FConnected := False;
DoAfterClose();
end;
procedure TCustomGPS.DoOnSatellitesChange(NbSat, NbSatUse: Shortint;
Sats: TSatellites);
begin
if Assigned(FOnSatellitesChange) then
FOnSatellitesChange(Self, NbSat, NbSatUse, Sats);
end;
procedure TCustomGPS.DoOnGPSDatasChange(GPSDatas: TGPSDatas);
begin
if Assigned(FOnGPSDatasChange) then
FOnGPSDatasChange(Self, GPSDatas);
end;
procedure TCustomGPS.DoAfterClose();
begin
if Assigned(FOnAfterClose) then
FOnAfterClose(Self);
end;
procedure TCustomGPS.DoAfterOpen();
begin
if Assigned(FOnAfterOpen) then
FOnAfterOpen(Self);
end;
constructor TGPStoGPX.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
LastFixTime := 0;
FGPSLink := TGPSLink.Create();
FGPSLink.OnGPSDatasChange := Nil;
FGPSLink.OnGPSDatasChange := GPSChange;
end;
destructor TGPStoGPX.Destroy();
begin
if Active then
Disconnect();
GPS := Nil;
FGPSLink.Free();
inherited Destroy();
end;
procedure TGPStoGPX.Connect();
begin
if not FileExists(FFileName) then
begin
FGPXFile := Newgpx();
with FGPXFile do
begin
Version := '1.1';
if Application.Title <> '' then
Creator := Application.Title
else
Creator := ExtractFileName(Application.ExeName);
with Metadata do
begin
Name := FFileName;
Author.Name := 'PLAIS Lionel';
Author.Email.Id := 'lionel.plais';
Author.Email.Domain := 'ilp-web.net';
Author.Link.Text := 'Site Web de PLAIS Lionel';
Author.Link.Href := 'http://www.ilp-web.net/';
end;
end;
end
else
FGPXFile := Loadgpx(FFileName);
FGPXTrack := FGPXFile.Trk.Add();
FGPXTrack.Name := 'Track: ' + DateTimeToStr(Now());
FGPXSeg := FGPXTrack.Trkseg.Add();
FActive := True;
end;
procedure TGPStoGPX.Disconnect();
var
XMLFile: TextFile;
begin
if Assigned(FGPXFile) then
begin
AssignFile(XMLFile, FFileName);
Rewrite(XMLFile);
Write(XMLFile, FGPXFile.XML);
CloseFile(XMLFile);
end;
FActive := False;
end;
procedure TGPStoGPX.SetActive(const Value: Boolean);
begin
if not ((csDesigning in ComponentState) or (csLoading in
ComponentState)) then
begin
if Value <> FActive then
if Value then
Connect()
else
Disconnect();
end
else
FActive := Value;
end;
procedure TGPStoGPX.SetGPS(const Value: TCustomGPS);
begin
if FGPS <> Value then
begin
if Assigned(FGPS) then
FGPS.UnRegisterLink(FGPSLink);
FGPS := Value;
if Assigned(FGPS) then
begin
FGPS.FreeNotification(Self);
FGPS.RegisterLink(FGPSLink);
end;
end;
end;
procedure TGPStoGPX.SetFileName(const Value: String);
begin
if Active then
Disconnect();
FFileName := Value;
end;
procedure TGPStoGPX.GPSChange(Sender: TObject; GPSDatas: TGPSDatas);
var
ID: Integer;
OldLat, OldLon: Double;
FormatDate: TFormatSettings;
GPSTime: TDateTime;
TZICurrent: TTimeZoneInformation;
begin
if Active and GPSDatas.Valid then
begin
GetLocaleFormatSettings($0409, FormatDate);
FormatDate.ShortDateFormat := 'yyyy-mm-dd"T"hh:nn:ss"Z"';
ID := FGPXSeg.Trkpt.Count;
if ID > 0 then
with FGPXSeg.Trkpt.Items[ID - 1] do
begin
OldLat := StrToFloat(Lat, FormatDate);
OldLon := StrToFloat(Lon, FormatDate);
end
else
begin
OldLat := 0;
OldLon := 0;
end;
if not (SameValue(GPSDatas.Latitude, OldLat, 0.00001) or
SameValue(GPSDatas.Longitude, OldLon, 0.00001)) and not
(IsZero(GPSDatas.Latitude) or IsZero(GPSDatas.Longitude)) then
begin
GetTimeZoneInformation(TZICurrent);
GPSTime := Date() + GPSDatas.UTCTime + TZICurrent.Bias;
if (SecondsBetween(GPSTime, LastFixTime) > SEC_BETWEEN_SEG) and
(LastFixTime > 0) then
begin
FGPXSeg := FGPXTrack.Trkseg.Add();
FGPXTrack.Name := 'Track: ' + DateTimeToStr(Now());
end;
with FGPXSeg.Trkpt.Add() do
begin
Ele := FloatToStr(GPSDatas.HeightAboveSea, FormatDate);
Time := DateToStr(GPSTime, FormatDate);
Lat := FloatToStrF(GPSDatas.Latitude, ffFixed, 8, 6, FormatDate);
Lon := FloatToStrF(GPSDatas.Longitude, ffFixed, 8, 6, FormatDate);
Sat := GPSDatas.NbrSatsUsed;
AddChild('speed').NodeValue := GPSDatas.Speed;
LastFixTime := GPSTime;
end;
end;
end;
end;
procedure TGPStoGPX.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (AComponent = FGPS) and (Operation = opRemove) then
GPS := Nil;
end;
constructor TGPSSpeed.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FGPSLink := TGPSLink.Create();
FGPSLink.OnGPSDatasChange := Nil;
FGPSLink.OnGPSDatasChange := SpeedChange;
end;
destructor TGPSSpeed.Destroy();
begin
GPS := Nil;
FGPSLink.Free();
inherited Destroy();
end;
procedure TGPSSpeed.SetGPS(const Value: TCustomGPS);
begin
if FGPS <> Value then
begin
if Assigned(FGPS) then
FGPS.UnRegisterLink(FGPSLink);
FGPS := Value;
if Assigned(FGPS) then
begin
FGPS.FreeNotification(Self);
FGPS.RegisterLink(FGPSLink);
end;
end;
end;
procedure TGPSSpeed.SetSpeedUnit(const Value: TSpeedUnit);
begin
if FSpeedUnit <> Value then
begin
FSpeedUnit := Value;
Refresh();
end;
end;
procedure TGPSSpeed.SpeedChange(Sender: TObject; GPSDatas: TGPSDatas);