在PDA中获取GPS信息。

古城微笑少年丶 2022-08-07 13:40 368阅读 0赞
http://www.cnblogs.com/anhlyiling/archive/2008/12/04/1347623.html
在PDA中获取GPS信息。

近日一个项目需要用在PDA上读

取GPS卫星信号,在网上搜了一圈,整理出一个GPS的类,大家可以直接加入项目

使用。可以获得当前的经纬度、海拔、速度、连接卫星数等信息

ContractedBlock.gif ExpandedBlockStart.gif Code
1 None.gif using System;
2 None.gif using System.Collections.Generic;
3 None.gif using System.Text;
4 None.gif using System.Runtime.InteropServices;
5 None.gif
6 None.gif namespace AnhlYiling
7 ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
8 InBlock.gif public class GPS
9 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
10 InBlock.gif
11 InBlock.gif public string PortNum;
12 InBlock.gif public int BaudRate;
13 InBlock.gif public byte ByteSize;
14 InBlock.gif public byte Parity; // 0-4=no,odd,even,mark,space
15 InBlock.gif public byte StopBits; // 0,1,2 = 1, 1.5, 2
16 InBlock.gif public int ReadTimeout;
17 InBlock.gif
18 InBlock.gif // comm port win32 file handle
19 InBlock.gif private int hComm = - 1 ;
20 InBlock.gif
21 InBlock.gif public bool Opened = false ;
22 InBlock.gif
23 InBlock.gif // win32 api constants
24 InBlock.gif private const uint GENERIC_READ = 0x80000000 ;
25 InBlock.gif private const uint GENERIC_WRITE = 0x40000000 ;
26 InBlock.gif private const int OPEN_EXISTING = 3 ;
27 InBlock.gif private const int INVALID_HANDLE_VALUE = - 1 ;
28 InBlock.gif
29 InBlock.gif [StructLayout(LayoutKind.Sequential)]
30 InBlock.gif public struct DCB
31 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
32 InBlock.gif // taken from c struct in platform sdk
33 InBlock.gif public int DCBlength; // sizeof(DCB)
34 InBlock.gif public int BaudRate; // 指定当前波特率 current baud rate
35 InBlock.gif // these are the c struct bit fields, bit twiddle flag to set
36 InBlock.gif public int fBinary; // 指定是否允许二进制模式,在windows95中必须主TRUE binary mode, no EOF check
37 InBlock.gif public int fParity; // 指定是否允许奇偶校验 enable parity checking
38 InBlock.gif public int fOutxCtsFlow; // 指定CTS是否用于检测发送控制,当为TRUE是CTS为OFF,发送将被挂起。 CTS output flow control
39 InBlock.gif public int fOutxDsrFlow; // 指定CTS是否用于检测发送控制 DSR output flow control
40 InBlock.gif public int fDtrControl; // DTR_CONTROL_DISABLE值将DTR置为OFF, DTR_CONTROL_ENABLE值将DTR置为ON, DTR_CONTROL_HANDSHAKE允许DTR”握手” DTR flow control type
41 InBlock.gif public int fDsrSensitivity; // 当该值为TRUE时DSR为OFF时接收的字节被忽略 DSR sensitivity
42 InBlock.gif public int fTXContinueOnXoff; // 指 定当接收缓冲区已满,并且驱动程序已经发送出XoffChar字符时发送是否停止。TRUE时,在接收缓冲区接收到缓冲区已满的字节XoffLim且驱动 程序已经发送出XoffChar字符中止接收字节之后,发送继续进行。 FALSE时,在接收缓冲区接收到代表缓冲区已空的字节XonChar且驱动程序 已经发送出恢复发送的XonChar之后,发送继续进行。XOFF continues Tx
43 InBlock.gif public int fOutX; // TRUE时,接收到XoffChar之后便停止发送接收到XonChar之后将重新开始 XON/XOFF out flow control
44 InBlock.gif public int fInX; // TRUE时,接收缓冲区接收到代表缓冲区满的XoffLim之后,XoffChar发送出去接收缓冲区接收到代表缓冲区空的XonLim之后,XonChar发送出去 XON/XOFF in flow control
45 InBlock.gif public int fErrorChar; // 该值为TRUE且fParity为TRUE时,用ErrorChar 成员指定的字符代替奇偶校验错误的接收字符 enable error replacement
46 InBlock.gif public int fNull; // eTRUE时,接收时去掉空(0值)字节 enable null stripping
47 InBlock.gif public int fRtsControl; // RTS flow control
48 ExpandedSubBlockStart.gif ContractedSubBlock.gif /**/ /* RTS_CONTROL_DISABLE时,RTS置为OFF
49 InBlock.gif   RTS_CONTROL_ENABLE时, RTS置为ON
50 InBlock.gif    RTS_CONTROL_HANDSHAKE时,
51 InBlock.gif    当接收缓冲区小于半满时RTS为ON
52 InBlock.gif     当接收缓冲区超过四分之三满时RTS为OFF
53 InBlock.gif    RTS_CONTROL_TOGGLE时,
54 ExpandedSubBlockEnd.gif    当接收缓冲区仍有剩余字节时RTS为ON ,否则缺省为OFF */
55 InBlock.gif
56 InBlock.gif public int fAbortOnError; // TRUE时,有错误发生时中止读和写操作 abort on error
57 InBlock.gif public int fDummy2; // 未使用 reserved
58 InBlock.gif
59 InBlock.gif public uint flags;
60 InBlock.gif public ushort wReserved; // 未使用,必须为0 not currently used
61 InBlock.gif public ushort XonLim; // 指定在XON字符发送这前接收缓冲区中可允许的最小字节数 transmit XON threshold
62 InBlock.gif public ushort XoffLim; // 指定在XOFF字符发送这前接收缓冲区中可允许的最小字节数 transmit XOFF threshold
63 InBlock.gif public byte ByteSize; // 指定端口当前使用的数据位 number of bits/byte, 4-8
64 InBlock.gif public byte Parity; // 指定端口当前使用的奇偶校验方法,可能为:EVENPARITY,MARKPARITY,NOPARITY,ODDPARITY 0-4=no,odd,even,mark,space
65 InBlock.gif public byte StopBits; // 指定端口当前使用的停止位数,可能为:ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS 0,1,2 = 1, 1.5, 2
66 InBlock.gif public char XonChar; // 指定用于发送和接收字符XON的值 Tx and Rx XON character
67 InBlock.gif public char XoffChar; // 指定用于发送和接收字符XOFF值 Tx and Rx XOFF character
68 InBlock.gif public char ErrorChar; // 本字符用来代替接收到的奇偶校验发生错误时的值 error replacement character
69 InBlock.gif public char EofChar; // 当没有使用二进制模式时,本字符可用来指示数据的结束 end of input character
70 InBlock.gif public char EvtChar; // 当接收到此字符时,会产生一个事件 received event character
71 InBlock.gif public ushort wReserved1; // 未使用 reserved; do not use
72 ExpandedSubBlockEnd.gif }
73 InBlock.gif
74 InBlock.gif [StructLayout(LayoutKind.Sequential)]
75 InBlock.gif private struct COMMTIMEOUTS
76 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
77 InBlock.gif public int ReadIntervalTimeout;
78 InBlock.gif public int ReadTotalTimeoutMultiplier;
79 InBlock.gif public int ReadTotalTimeoutConstant;
80 InBlock.gif public int WriteTotalTimeoutMultiplier;
81 InBlock.gif public int WriteTotalTimeoutConstant;
82 ExpandedSubBlockEnd.gif }
83 InBlock.gif
84 InBlock.gif [StructLayout(LayoutKind.Sequential)]
85 InBlock.gif private struct OVERLAPPED
86 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
87 InBlock.gif public int Internal;
88 InBlock.gif public int InternalHigh;
89 InBlock.gif public int Offset;
90 InBlock.gif public int OffsetHigh;
91 InBlock.gif public int hEvent;
92 ExpandedSubBlockEnd.gif }
93 InBlock.gif
94 InBlock.gif [DllImport( “ coredll.dll “ )]
95 InBlock.gif private static extern int CreateFile(
96 InBlock.gif string lpFileName, // 要打开的串口名称
97 InBlock.gif uint dwDesiredAccess, // 指定串口的访问方式,一般设置为可读可写方式
98 InBlock.gif int dwShareMode, // 指定串口的共享模式,串口不能共享,所以设置为0
99 InBlock.gif int lpSecurityAttributes, // 设置串口的安全属性,WIN9X下不支持,应设为NULL
100 InBlock.gif int dwCreationDisposition, // 对于串口通信,创建方式只能为OPEN_EXISTING
101 InBlock.gif int dwFlagsAndAttributes, // 指定串口属性与标志,设置为FILE_FLAG_OVERLAPPED(重叠I/O操作),指定串口以异步方式通信
102 InBlock.gif int hTemplateFile // 对于串口通信必须设置为NULL
103 InBlock.gif );
104 InBlock.gif [DllImport( “ coredll.dll “ )]
105 InBlock.gif private static extern bool GetCommState(
106 InBlock.gif int hFile, // 通信设备句柄
107 InBlock.gif ref DCB lpDCB // 设备控制块DCB
108 InBlock.gif );
109 InBlock.gif [DllImport( “ coredll.dll “ )]
110 InBlock.gif private static extern bool BuildCommDCB(
111 InBlock.gif string lpDef, // 设备控制字符串
112 InBlock.gif ref DCB lpDCB // 设备控制块
113 InBlock.gif );
114 InBlock.gif [DllImport( “ coredll.dll “ )]
115 InBlock.gif private static extern bool SetCommState(
116 InBlock.gif int hFile, // 通信设备句柄
117 InBlock.gif ref DCB lpDCB // 设备控制块
118 InBlock.gif );
119 InBlock.gif [DllImport( “ coredll.dll “ )]
120 InBlock.gif private static extern bool GetCommTimeouts(
121 InBlock.gif int hFile, // 通信设备句柄 handle to comm device
122 InBlock.gif ref COMMTIMEOUTS lpCommTimeouts // 超时时间 time-out values
123 InBlock.gif );
124 InBlock.gif [DllImport( “ coredll.dll “ )]
125 InBlock.gif private static extern bool SetCommTimeouts(
126 InBlock.gif int hFile, // 通信设备句柄 handle to comm device
127 InBlock.gif ref COMMTIMEOUTS lpCommTimeouts // 超时时间 time-out values
128 InBlock.gif );
129 InBlock.gif [DllImport( “ coredll.dll “ )]
130 InBlock.gif private static extern bool ReadFile(
131 InBlock.gif int hFile, // 通信设备句柄 handle to file
132 InBlock.gif byte [] lpBuffer, // 数据缓冲区 data buffer
133 InBlock.gif int nNumberOfBytesToRead, // 多少字节等待读取 number of bytes to read
134 InBlock.gif ref int lpNumberOfBytesRead, // 读取多少字节 number of bytes read
135 InBlock.gif ref OVERLAPPED lpOverlapped // 溢出缓冲区 overlapped buffer
136 InBlock.gif );
137 InBlock.gif [DllImport( “ coredll.dll “ )]
138 InBlock.gif private static extern bool WriteFile(
139 InBlock.gif int hFile, // 通信设备句柄 handle to file
140 InBlock.gif byte [] lpBuffer, // 数据缓冲区 data buffer
141 InBlock.gif int nNumberOfBytesToWrite, // 多少字节等待写入 number of bytes to write
142 InBlock.gif ref int lpNumberOfBytesWritten, // 已经写入多少字节 number of bytes written
143 InBlock.gif ref OVERLAPPED lpOverlapped // 溢出缓冲区 overlapped buffer
144 InBlock.gif );
145 InBlock.gif [DllImport( “ coredll.dll “ )]
146 InBlock.gif private static extern bool CloseHandle(
147 InBlock.gif int hObject // handle to object
148 InBlock.gif );
149 InBlock.gif [DllImport( “ coredll.dll “ )]
150 InBlock.gif private static extern uint GetLastError();
151 InBlock.gif
152 InBlock.gif public void Open()
153 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
154 InBlock.gif
155 InBlock.gif DCB dcbCommPort = new DCB();
156 InBlock.gif COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
157 InBlock.gif
158 InBlock.gif // 打开串口 OPEN THE COMM PORT.
159 InBlock.gif hComm = CreateFile(PortNum ,GENERIC_READ | GENERIC_WRITE, 0 , 0 ,OPEN_EXISTING, 0 , 0 );
160 InBlock.gif // 如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL OUT.
161 InBlock.gif if (hComm == INVALID_HANDLE_VALUE)
162 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
163 InBlock.gif throw ( new ApplicationException( “ 非法操作,不能打开串口! “ ));
164 ExpandedSubBlockEnd.gif }
165 InBlock.gif
166 InBlock.gif // 设置通信超时时间 SET THE COMM TIMEOUTS.
167 InBlock.gif GetCommTimeouts(hComm, ref ctoCommPort);
168 InBlock.gif ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
169 InBlock.gif ctoCommPort.ReadTotalTimeoutMultiplier = 0 ;
170 InBlock.gif ctoCommPort.WriteTotalTimeoutMultiplier = 0 ;
171 InBlock.gif ctoCommPort.WriteTotalTimeoutConstant = 0 ;
172 InBlock.gif SetCommTimeouts(hComm, ref ctoCommPort);
173 InBlock.gif
174 InBlock.gif // 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
175 InBlock.gif GetCommState(hComm, ref dcbCommPort);
176 InBlock.gif dcbCommPort.BaudRate = BaudRate;
177 InBlock.gif dcbCommPort.flags = 0 ;
178 InBlock.gif // dcb.fBinary=1;
179 InBlock.gif dcbCommPort.flags |= 1 ;
180 InBlock.gif if (Parity > 0 )
181 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
182 InBlock.gif // dcb.fParity=1
183 InBlock.gif dcbCommPort.flags |= 2 ;
184 ExpandedSubBlockEnd.gif }
185 InBlock.gif dcbCommPort.Parity = Parity;
186 InBlock.gif dcbCommPort.ByteSize = ByteSize;
187 InBlock.gif dcbCommPort.StopBits = StopBits;
188 InBlock.gif if ( ! SetCommState(hComm, ref dcbCommPort))
189 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
190 InBlock.gif // uint ErrorNum=GetLastError();
191 InBlock.gif throw ( new ApplicationException( “ 非法操作,不能打开串口! “ ));
192 ExpandedSubBlockEnd.gif }
193 InBlock.gif // unremark to see if setting took correctly
194 InBlock.gif // DCB dcbCommPort2 = new DCB();
195 InBlock.gif // GetCommState(hComm, ref dcbCommPort2);
196 InBlock.gif Opened = true ;
197 ExpandedSubBlockEnd.gif }
198 InBlock.gif
199 InBlock.gif public void Close()
200 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
201 InBlock.gif if (hComm != INVALID_HANDLE_VALUE)
202 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
203 InBlock.gif CloseHandle(hComm);
204 ExpandedSubBlockEnd.gif }
205 ExpandedSubBlockEnd.gif }
206 InBlock.gif
207 InBlock.gif public byte [] Read( int NumBytes)
208 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
209 InBlock.gif byte [] BufBytes;
210 InBlock.gif byte [] OutBytes;
211 InBlock.gif BufBytes = new byte [NumBytes];
212 InBlock.gif if (hComm != INVALID_HANDLE_VALUE)
213 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
214 InBlock.gif OVERLAPPED ovlCommPort = new OVERLAPPED();
215 InBlock.gif int BytesRead = 0 ;
216 InBlock.gif ReadFile(hComm,BufBytes,NumBytes, ref BytesRead, ref ovlCommPort);
217 InBlock.gif try
218 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
219 InBlock.gif OutBytes = new byte [BytesRead];
220 InBlock.gif Array.Copy(BufBytes, 0 ,OutBytes, 0 ,BytesRead);
221 ExpandedSubBlockEnd.gif }
222 InBlock.gif catch
223 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
224 InBlock.gif return BufBytes;
225 ExpandedSubBlockEnd.gif }
226 InBlock.gif
227 ExpandedSubBlockEnd.gif }
228 InBlock.gif else
229 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
230 InBlock.gif throw ( new ApplicationException( “ 串口未打开! “ ));
231 ExpandedSubBlockEnd.gif }
232 InBlock.gif return OutBytes;
233 InBlock.gif // return BufBytes;
234 ExpandedSubBlockEnd.gif }
235 InBlock.gif
236 InBlock.gif public void Write( byte [] WriteBytes)
237 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
238 InBlock.gif if (hComm != INVALID_HANDLE_VALUE)
239 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
240 InBlock.gif OVERLAPPED ovlCommPort = new OVERLAPPED();
241 InBlock.gif int BytesWritten = 0 ;
242 InBlock.gif WriteFile(hComm,WriteBytes,WriteBytes.Length, ref BytesWritten, ref ovlCommPort);
243 ExpandedSubBlockEnd.gif }
244 InBlock.gif else
245 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
246 InBlock.gif throw ( new ApplicationException( “ 串口未打开! “ ));
247 ExpandedSubBlockEnd.gif }
248 ExpandedSubBlockEnd.gif }
249 InBlock.gif
250 InBlock.gif public string GetGPS( string hander, string strGPS, string strFind)
251 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
252 ExpandedSubBlockStart.gif ContractedSubBlock.gif /**/ /// 从GPS中读取的数据中,找出想要的数据
253 InBlock.gif /// GPSstring原始字符串,
254 InBlock.gif /// strFind要查找的内容,X:经度,Y:纬度,T:时间,V:速度,N 当前可用卫星数量,H 当前海拔
255 ExpandedSubBlockEnd.gif /// 返回查找到指定位置的字符串
256 InBlock.gif if (hander.Equals( “ $GPRMC “ ))
257 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
258 InBlock.gif string handerStr = hander; // GPS串头
259 InBlock.gif int findHander = strGPS.IndexOf(handerStr); // 看是否含有GPS串头
260 InBlock.gif if (findHander < 0 )
261 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
262 InBlock.gif return “ -1 “ ;
263 ExpandedSubBlockEnd.gif }
264 InBlock.gif else
265 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
266 InBlock.gif strGPS = strGPS.Substring(findHander, strGPS.Length - findHander);
267 InBlock.gif string [] ArryTmp = strGPS.Split( “ , “ .ToCharArray());
268 InBlock.gif try
269 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
270 InBlock.gif if (ArryTmp[ 2 ] == “ V “ )
271 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
272 InBlock.gif return “ GPS没有信号 “ ; // 没有信号
273 ExpandedSubBlockEnd.gif }
274 InBlock.gif else
275 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
276 InBlock.gif switch (strFind)
277 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
278 InBlock.gif case “ X “ :
279 InBlock.gif return DM2DD(ArryTmp[ 5 ]);
280 InBlock.gif
281 InBlock.gif case “ Y “ :
282 InBlock.gif return DM2DD(ArryTmp[ 3 ]);
283 InBlock.gif
284 InBlock.gif case “ T “ :
285 InBlock.gif return T2Time(ArryTmp[ 9 ], ArryTmp[ 1 ]);
286 InBlock.gif
287 InBlock.gif case “ V “ :
288 InBlock.gif return Convert.ToString(Convert.ToDouble(ArryTmp[ 7 ]) * 1.852 );
289 InBlock.gif
290 InBlock.gif case “ K “ :
291 InBlock.gif return ArryTmp[ 7 ];
292 InBlock.gif default :
293 InBlock.gif return “ V “ ;
294 InBlock.gif
295 ExpandedSubBlockEnd.gif }
296 ExpandedSubBlockEnd.gif }
297 ExpandedSubBlockEnd.gif }
298 InBlock.gif catch
299 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
300 InBlock.gif return “ V “ ;
301 ExpandedSubBlockEnd.gif }
302 ExpandedSubBlockEnd.gif }
303 ExpandedSubBlockEnd.gif }
304 InBlock.gif else if (hander.Equals( “ $GPGGA “ ))
305 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
306 InBlock.gif string handerStr = hander; // GPS串头
307 InBlock.gif int findHander = strGPS.IndexOf(handerStr); // 看是否含有GPS串头
308 InBlock.gif if (findHander < 0 )
309 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
310 InBlock.gif return “ -1 “ ;
311 ExpandedSubBlockEnd.gif }
312 InBlock.gif else
313 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
314 InBlock.gif strGPS = strGPS.Substring(findHander, strGPS.Length - findHander);
315 InBlock.gif string [] ArryTmp = strGPS.Split( “ , “ .ToCharArray());
316 InBlock.gif switch (strFind)
317 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
318 InBlock.gif case “ N “ :
319 InBlock.gif return ArryTmp[ 7 ];
320 InBlock.gif case “ H “ :
321 InBlock.gif return ArryTmp[ 9 ];
322 InBlock.gif default :
323 InBlock.gif return “ 无法解析的命令. “ ;
324 ExpandedSubBlockEnd.gif }
325 ExpandedSubBlockEnd.gif }
326 ExpandedSubBlockEnd.gif }
327 InBlock.gif
328 InBlock.gif return null ;
329 InBlock.gif
330 ExpandedSubBlockEnd.gif }
331 InBlock.gif
332 InBlock.gif public string T2Time( string strDate, string strTime)
333 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
334 InBlock.gif string dT = “ 20 “ + strDate.Substring( 4 , 2 ) + “ - “ + strDate.Substring( 2 , 2 ) + “ - “ + strDate.Substring( 0 , 2 );
335 InBlock.gif string TT = Convert.ToString(Convert.ToInt32(strTime.Substring( 0 , 2 ))) + “ : “ + strTime.Substring( 2 , 2 ) + “ : “ + strTime.Substring( 4 , 2 );
336 InBlock.gif DateTime T = Convert.ToDateTime(dT + “ “ + TT);
337 InBlock.gif T = T.AddHours( 8 );
338 InBlock.gif return T.ToString();
339 ExpandedSubBlockEnd.gif }
340 InBlock.gif
341 InBlock.gif public string DM2DD( string DegreeMinutes)
342 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
343 InBlock.gif // 转换NMEA协议的“度分”格式为十进制“度度”格式
344 InBlock.gif string sDegree;
345 InBlock.gif string sMinute;
346 InBlock.gif string sReturn = “” ;
347 InBlock.gif if (DegreeMinutes.IndexOf( “ . “ ) == 4 )
348 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
349 InBlock.gif // DegreeMinutes = Replace(DegreeMinutes, “.”, “”)
350 InBlock.gif // DM2DD = CDbl(Left(DegreeMinutes, 2)) + CDbl(Left(CStr(CDbl(Right(DegreeMinutes, Len(DegreeMinutes) - 2)) / 60), 8)) / 10000
351 InBlock.gif DegreeMinutes = DegreeMinutes.Replace( “ . “ , “” );
352 InBlock.gif double sDegree1 = Convert.ToDouble(DegreeMinutes.Substring( 0 , 2 ));
353 InBlock.gif double sDegree2 = Convert.ToDouble(DegreeMinutes.Substring( 2 ,DegreeMinutes.Length - 2 ));
354 InBlock.gif string sTmp = Convert.ToString(sDegree2 / 60 );
355 InBlock.gif sDegree2 = Convert.ToDouble(sTmp.Substring( 0 ,sTmp.Length));
356 InBlock.gif sDegree2 = sDegree2 / 10000 ;
357 InBlock.gif sDegree = Convert.ToString(sDegree1 + sDegree2);
358 InBlock.gif if (sDegree.Length > 11 )
359 InBlock.gif sDegree = sDegree.Substring( 0 , 11 );
360 InBlock.gif sReturn = sDegree;
361 ExpandedSubBlockEnd.gif }
362 InBlock.gif else if (DegreeMinutes.IndexOf( “ . “ ) == 5 )
363 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
364 InBlock.gif // DegreeMinutes = Replace(DegreeMinutes, “.”, “”)
365 InBlock.gif // DM2DD = CDbl(Left(DegreeMinutes, 2)) + CDbl(Left(CStr(CDbl(Right(DegreeMinutes, Len(DegreeMinutes) - 2)) / 60), 8)) / 10000
366 InBlock.gif DegreeMinutes = DegreeMinutes.Replace( “ . “ , “” );
367 InBlock.gif double sMinute1 = Convert.ToDouble(DegreeMinutes.Substring( 0 , 3 ));
368 InBlock.gif double sMinute2 = Convert.ToDouble(DegreeMinutes.Substring( 3 ,DegreeMinutes.Length - 3 ));
369 InBlock.gif string sTmp = Convert.ToString(sMinute2 / 60 );
370 InBlock.gif sMinute2 = Convert.ToDouble(sTmp.Substring( 0 ,sTmp.Length));
371 InBlock.gif sMinute2 = sMinute2 / 10000 ;
372 InBlock.gif sMinute = Convert.ToString(sMinute1 + sMinute2);
373 InBlock.gif if (sMinute.Length > 10 )
374 InBlock.gif sMinute = sMinute.Substring( 0 , 10 );
375 InBlock.gif sReturn = sMinute;
376 ExpandedSubBlockEnd.gif }
377 InBlock.gif return sReturn;
378 ExpandedSubBlockEnd.gif }
379 InBlock.gif
380 InBlock.gif public bool ScanPort()
381 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
382 InBlock.gif
383 InBlock.gif try
384 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
385 InBlock.gif if (Opened)
386 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
387 InBlock.gif Close();
388 InBlock.gif Open();
389 ExpandedSubBlockEnd.gif }
390 InBlock.gif else
391 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
392 InBlock.gif Open(); // 打开串口
393 InBlock.gif
394 ExpandedSubBlockEnd.gif }
395 InBlock.gif byte [] bytRead = Read( 512 );
396 InBlock.gif Close();
397 InBlock.gif if (Encoding.ASCII.GetString(bytRead, 0 ,bytRead.Length).IndexOf( “ $GP “ ) >= 0 )
398 InBlock.gif return true ;
399 InBlock.gif else
400 InBlock.gif return false ;
401 ExpandedSubBlockEnd.gif }
402 InBlock.gif catch
403 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
404 InBlock.gif return false ;
405 ExpandedSubBlockEnd.gif }
406 InBlock.gif
407 ExpandedSubBlockEnd.gif }
408 ExpandedSubBlockEnd.gif }
409 InBlock.gif
410 InBlock.gif class HexCon
411 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
412 InBlock.gif // 把十六进制字符串转换成字节型和把字节型转换成十六进制字符串 converter hex string to byte and byte to hex string
413 InBlock.gif public static string ByteToString( byte [] InBytes)
414 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
415 InBlock.gif string StringOut = “” ;
416 InBlock.gif foreach ( byte InByte in InBytes)
417 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
418 InBlock.gif StringOut = StringOut + String.Format( “ {0:X2} “ ,InByte);
419 ExpandedSubBlockEnd.gif }
420 InBlock.gif return StringOut;
421 ExpandedSubBlockEnd.gif }
422 InBlock.gif public static byte [] StringToByte( string InString)
423 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
424 InBlock.gif string [] ByteStrings;
425 InBlock.gif ByteStrings = InString.Split( “ “ .ToCharArray());
426 InBlock.gif byte [] ByteOut;
427 InBlock.gif ByteOut = new byte [ByteStrings.Length - 1 ];
428 InBlock.gif for ( int i = 0 ;i == ByteStrings.Length - 1 ;i ++ )
429 ExpandedSubBlockStart.gif ContractedSubBlock.gif dot.gif {
430 InBlock.gif ByteOut[i] = Convert.ToByte(( “ 0x “ + ByteStrings[i]));
431 ExpandedSubBlockEnd.gif }
432 InBlock.gif return ByteOut;
433 ExpandedSubBlockEnd.gif }
434 InBlock.gif
435 ExpandedSubBlockEnd.gif }
436 ExpandedBlockEnd.gif }
437 None.gif

调用很简单:

1 GPS ss_port = new GPS();
2 ss_port.PortNum = “ COM4: “ ; // 串口号.后面一定要有’:’
3 ss_port.BaudRate = 4800 ;
4 ss_port.ByteSize = 8 ;
5 ss_port.Parity = 0 ;
6 ss_port.StopBits = 1 ;
7 ss_port.ReadTimeout = 1000 ;
8
9 try
10 {
11 if (ss_port.Opened)
12 {
13 ss_port.Close();
14 ss_port.Open();
15 timer1.Enabled = true ;
16 }
17 else
18 {
19 ss_port.Open(); // 打开串口
20 label1.Text = “ 已打开 “ ;
21 timer1.Enabled = true ;
22 }
23
24 }
25 catch (Exception e1)
26 {
27 label1.Text = e1.Message;
28
29 }

发表评论

表情:
评论列表 (有 0 条评论,368人围观)

还没有评论,来说两句吧...

相关阅读