forked from jperson2000/GPS.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGpgsaSentence.cs
329 lines (252 loc) · 9.89 KB
/
GpgsaSentence.cs
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
using System;
using System.Collections.Generic;
using System.Text;
/*
* $GPGSA
GPS DOP and active satellites
eg1. $GPGSA,A,3,,,,,,16,18,,22,24,,,3.6,2.1,2.2*3C
* $GPGSA,A,1,,,,,,,,,,,,,6,6,6
eg2. $GPGSA,A,3,19,28,14,18,27,22,31,39,,,,,1.7,1.0,1.3*35
1 = Method:
M=Manual, forced to operate in 2D or 3D
A=Automatic, 3D/2D
2 = Mode:
1=Fix not available
2=2D
3=3D
3-14 = IDs of SVs used in position fix (null for unused fields)
15 = PDOP
16 = HDOP
17 = VDOP
*/
namespace GeoFramework.Gps.Nmea
{
public sealed class GpgsaSentence : NmeaSentence, IFixMethodSentence, IFixModeSentence, IFixedSatellitesSentence, IPositionDilutionOfPrecisionSentence,
IHorizontalDilutionOfPrecisionSentence, IVerticalDilutionOfPrecisionSentence
{
private FixMethod _FixMethod;
private FixMode _FixMode;
private List<Satellite> _FixedSatellites;
private DilutionOfPrecision _PositionDop;
private DilutionOfPrecision _HorizontalDop;
private DilutionOfPrecision _VerticalDop;
#region Constructors
public GpgsaSentence(string sentence)
: base(sentence)
{ }
internal GpgsaSentence(string sentence, string commandWord, string[] words, string validChecksum)
: base(sentence, commandWord, words, validChecksum)
{ }
public GpgsaSentence(FixMode fixMode, FixMethod fixMethod, IList<Satellite> satellites,
DilutionOfPrecision positionDilutionOfPrecision,
DilutionOfPrecision horizontalDilutionOfPrecision,
DilutionOfPrecision verticalDilutionOfPrecision)
{
// Use a string builder to create the sentence text
StringBuilder builder = new StringBuilder(128);
// Append the command word
builder.Append("$GPGSA");
// Append a comma
builder.Append(',');
#region Append the fix method
switch (_FixMode)
{
case FixMode.Automatic:
builder.Append("A");
break;
default:
builder.Append("M");
break;
}
#endregion
// Append a comma
builder.Append(',');
#region Append the fix method
switch (_FixMethod)
{
case FixMethod.Fix2D:
builder.Append("2");
break;
case FixMethod.Fix3D:
builder.Append("3");
break;
default:
builder.Append("1");
break;
}
#endregion
// Append a comma
builder.Append(',');
#region Fixed satellites
/* A comma-delimited list of satellites involved in a fix. Up to 12 satellites can be serialized.
* This one concerns me, because while the limit is 12, ever more satellites are being launched.
* Should we just serialize everything??
*/
// Get a count of satellites to write, up to 123. We'll scrub the list to ensure only fixed satellites are written
int fixedSatellitesWritten = 0;
for(int index = 0; index < satellites.Count; index++)
{
// Get the satellite
Satellite item = satellites[index];
// Is it fixed?
if(item.IsFixed)
{
// Yes. It cannot have babies
builder.Append(item.PseudorandomNumber.ToString(NmeaCultureInfo));
// Append a comma
builder.Append(",");
// Update the count
fixedSatellitesWritten++;
// If we're at 12, that's the limit. Stop here
if(fixedSatellitesWritten == 12)
break;
}
}
// If we wrote less than 12 satellites, write commas for the remainder
for(int index = 0; index < 12 - fixedSatellitesWritten; index++)
builder.Append(",");
#endregion
// NOTE: Commas have been written at this point
// Position Dilution of Precision
builder.Append(positionDilutionOfPrecision.Value.ToString(NmeaCultureInfo));
// Append a comma
builder.Append(",");
// Horizontal Dilution of Precision
builder.Append(horizontalDilutionOfPrecision.Value.ToString(NmeaCultureInfo));
// Append a comma
builder.Append(",");
// Vertical Dilution of Precision
builder.Append(verticalDilutionOfPrecision.Value.ToString(NmeaCultureInfo));
// Set this object's sentence
SetSentence(builder.ToString());
// Finally, append the checksum
AppendChecksum();
}
#endregion
#region Overrides
protected override void OnSentenceChanged()
{
// First, process the basic info for the sentence
base.OnSentenceChanged();
// Cache the sentence words
string[] words = base.Words;
int wordCount = words.Length;
#region Fix mode
if (wordCount >= 1 && words[0].Length != 0)
{
switch (words[0])
{
case "A":
_FixMode = FixMode.Automatic;
break;
case "M":
_FixMode = FixMode.Manual;
break;
default:
_FixMode = FixMode.Unknown;
break;
}
}
else
{
_FixMode = FixMode.Unknown;
}
#endregion
#region Fix method
// Get the fix quality information
if (wordCount >= 2 && words[1].Length != 0)
{
switch (words[1])
{
case "1":
_FixMethod = FixMethod.NoFix;
break;
case "2":
_FixMethod = FixMethod.Fix2D;
break;
case "3":
_FixMethod = FixMethod.Fix3D;
break;
default:
_FixMethod = FixMethod.Unknown;
break;
}
}
else
{
_FixMethod = FixMethod.Unknown;
}
#endregion
#region Fixed satellites
if (wordCount >= 3)
{
// The sentence supports up to 12 satellites
_FixedSatellites = new List<Satellite>(12);
// Get each satellite PRN number
int count = wordCount < 14 ? wordCount : 14;
for (int index = 2; index < count; index++)
// Is the word empty?
if (words[index].Length != 0)
// No. Add a satellite
_FixedSatellites.Add(
// We'll only have an empty object for now
new Satellite(int.Parse(words[index], NmeaCultureInfo)));
}
#endregion
#region Dilution of Precision
// Set overall dilution of precision
if (wordCount >= 15 && words[14].Length != 0)
_PositionDop = new DilutionOfPrecision(float.Parse(words[14], NmeaCultureInfo));
else
_PositionDop = DilutionOfPrecision.Invalid;
// Set horizontal dilution of precision
if (wordCount >= 16 && words[15].Length != 0)
_HorizontalDop = new DilutionOfPrecision(float.Parse(words[15], NmeaCultureInfo));
else
_HorizontalDop = DilutionOfPrecision.Invalid;
// Set vertical dilution of precision
if (wordCount >= 17 && words[16].Length != 0)
_VerticalDop = new DilutionOfPrecision(float.Parse(words[16], NmeaCultureInfo));
else
_VerticalDop = DilutionOfPrecision.Invalid;
#endregion
}
#endregion
#region IFixedSatellitesSentence Members
public IList<Satellite> FixedSatellites
{
get { return _FixedSatellites; }
}
#endregion
#region IFixMethodSentence Members
public FixMethod FixMethod
{
get { return _FixMethod; }
}
#endregion
#region IMeanDilutionOfPrecisionSentence Members
public DilutionOfPrecision PositionDilutionOfPrecision
{
get { return _PositionDop; }
}
#endregion
#region IVerticalDilutionOfPrecisionSentence Members
public DilutionOfPrecision VerticalDilutionOfPrecision
{
get { return _VerticalDop; }
}
#endregion
#region IHorizontalDilutionOfPrecisionSentence Members
public DilutionOfPrecision HorizontalDilutionOfPrecision
{
get { return _HorizontalDop; }
}
#endregion
#region IFixModeSentence Members
public FixMode FixMode
{
get { return _FixMode; }
}
#endregion
}
}