OSDN Git Service

初期コミット
[yubeshi/yubeshi.git] / Yubeshi / Nmea / GpGga.cs
1 /*\r
2  *      Yubeshi GPS Parser\r
3  *\r
4  *      This software is distributed under a zlib-style license.\r
5  *      See license.txt for more information.\r
6  */\r
7 \r
8 using System;\r
9 using System.Collections.Generic;\r
10 using System.Text;\r
11 \r
12 namespace Yubeshi.Nmea\r
13 {\r
14     /// <summary>\r
15     ///     Global Positioning System Fix Data\r
16     /// </summary>\r
17         public class GpGga : Packet\r
18     {\r
19         #region type definitions\r
20         public enum FixQualityClass\r
21         {\r
22             Invalid = 0,\r
23             Gps = 1,\r
24             Dgps = 2,\r
25             Pps = 3,\r
26             Rtk = 4,\r
27             FloatRtk = 5,\r
28             Estimated = 6,\r
29         }\r
30         #endregion\r
31         #region fields\r
32 \r
33         private static readonly byte[] header;\r
34         private const int elementNum = 13;\r
35 \r
36         #endregion\r
37 \r
38         #region constructors\r
39 \r
40         static GpGga()\r
41         {\r
42             header = Encoding.ASCII.GetBytes("$GPGGA,");\r
43         }\r
44 \r
45                 public GpGga()\r
46                 {\r
47                 }\r
48 \r
49                 public GpGga(byte[] sentence)\r
50             : this(sentence, GetElements(sentence, elementNum))\r
51                 {\r
52         }\r
53 \r
54         private GpGga(byte[] sentence, Elements elements)\r
55         {\r
56             Raw = new byte[elements.PacketLength];\r
57             Array.Copy(sentence, Raw, elements.PacketLength);\r
58 \r
59             TimeOfFix = ParseTime(elements.Values[0]);\r
60             Position = new Coordinate(elements.Values[1], elements.Values[2],\r
61                                     elements.Values[3], elements.Values[4]);\r
62             FixQuality = (FixQualityClass)Int32.Parse(elements.Values[5]);\r
63             TrackedSatellites = Int32.Parse(elements.Values[6]);\r
64             Dilution = Decimal.Parse(elements.Values[7]);\r
65             CheckSum = elements.CheckSum;\r
66         }\r
67 \r
68         #endregion\r
69 \r
70         #region properties\r
71 \r
72         public TimeSpan TimeOfFix\r
73         {\r
74             get;\r
75             private set;\r
76         }\r
77         \r
78         public Coordinate Position\r
79         {\r
80             get;\r
81             private set;\r
82         }\r
83 \r
84         public FixQualityClass FixQuality\r
85         {\r
86             get;\r
87             private set;\r
88         }\r
89 \r
90         public int TrackedSatellites\r
91         {\r
92             get;\r
93             private set;\r
94         }\r
95 \r
96         public decimal Dilution\r
97         {\r
98             get;\r
99             private set;\r
100         }\r
101 \r
102 \r
103         #endregion\r
104 \r
105 \r
106         public static bool TryParse(byte[] sentence, out Packet packet)\r
107         {\r
108             return TryParse(sentence, out packet, header, elementNum, Build);\r
109         }\r
110 \r
111         private static Packet Build(byte[] sentence, Elements elements)\r
112         {\r
113             return new GpGga(sentence, elements);\r
114         }\r
115 \r
116         private TimeSpan ParseTime(string time)\r
117         {\r
118             string h = time.Substring(0, 2);\r
119             string m = time.Substring(2, 2);\r
120             string s = time.Substring(4, 2);\r
121             return new TimeSpan(Int32.Parse(h), Int32.Parse(m), Int32.Parse(s));\r
122         }\r
123 \r
124         }\r
125 }