wangjh
论坛版主
论坛版主
  • 注册日期2003-08-22
  • 发帖数994
  • QQ55359982
  • 铜币2579枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:10264回复:21

[推荐]C#, VB.NET下开发GPS程序的好东东

楼主#
更多 发布于:2005-03-07 09:05
C#, VB.NET下开发GPS程序的好东东,

不但有详细的论述,还有详细的代码。

试运行了一下,效果很好



网址:http://www.codeguru.com/vb/mobile/pocketpc/article.php/c8079__1/

(共9页,这里不一一贴出来了)




[此贴子已经被作者于2005-3-7 9:07:47编辑过]
喜欢0 评分0
网 站: www.52xoo.com (3S,信息融合,数字图像处理,模式识别与人工智能等专业电子书、学术文章及源代码共享) E-mail: Jianhong72@163.com QQ: 88128745 (55359982用了近10年,最近被盗了,郁闷!!!)
wangjh
论坛版主
论坛版主
  • 注册日期2003-08-22
  • 发帖数994
  • QQ55359982
  • 铜币2579枚
  • 威望0点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2005-03-07 09:08
我上传的源代码怎么没有显示!
郁闷!!
网 站: www.52xoo.com (3S,信息融合,数字图像处理,模式识别与人工智能等专业电子书、学术文章及源代码共享) E-mail: Jianhong72@163.com QQ: 88128745 (55359982用了近10年,最近被盗了,郁闷!!!)
举报 回复(0) 喜欢(0)     评分
wangjh
论坛版主
论坛版主
  • 注册日期2003-08-22
  • 发帖数994
  • QQ55359982
  • 铜币2579枚
  • 威望0点
  • 贡献值0点
  • 银元0个
2楼#
发布于:2005-03-09 14:46
难道没人对这个东东感兴趣!!

这个帖子上不但有理论上介绍,
还有实现程序的说明,
最后还给出了整个运行程序的源代码。
对用C#,或VB.NET开发GPS程序有一定的帮助。

不过,是E文...

<img src="images/post/smile/dvbbs/em06.gif" />
网 站: www.52xoo.com (3S,信息融合,数字图像处理,模式识别与人工智能等专业电子书、学术文章及源代码共享) E-mail: Jianhong72@163.com QQ: 88128745 (55359982用了近10年,最近被盗了,郁闷!!!)
举报 回复(0) 喜欢(0)     评分
tsindong
路人甲
路人甲
  • 注册日期2005-03-10
  • 发帖数1
  • QQ
  • 铜币102枚
  • 威望0点
  • 贡献值0点
  • 银元0个
3楼#
发布于:2005-03-10 20:43
<P>进不去!</P>
举报 回复(0) 喜欢(0)     评分
wangjh
论坛版主
论坛版主
  • 注册日期2003-08-22
  • 发帖数994
  • QQ55359982
  • 铜币2579枚
  • 威望0点
  • 贡献值0点
  • 银元0个
4楼#
发布于:2005-03-11 09:05
<center>
<a href="http://www.codeguru.com/RealMedia/ads/click_lx.cgi/ew/ewsoftware/www.codeguru.com/vb/mobile/pocketpc/article/c8079__1/i/971194714/accessunit/default/empty.gif/64613530646434653432323961653830" target="_blank" ><img src="http://www.codeguru.com/RealMedia/ads/Creatives/default/empty.gif"></a><img src="http://www.codeguru.com/RealMedia/ads/adstream_lx.cgi/ew/ewsoftware/www.codeguru.com/vb/mobile/pocketpc/article/c8079__1/i/971194714/accessunit/default/empty.gif/64613530646434653432323961653830?_RM_EMPTY_"></p></center><p><img src="http://www.codeguru.com/dbfiles/get_image.php?id=8079;lbl=GPSPOCKETPC3_JPG;ds=20041229"></p><p>"I am continually amazed by how little code is required to use atomic clocks in satellites 11,000 miles above my head."</p><p>What is it that GPS applications need to be good enough to handlein-car navigation? Also, how does the process of interpreting GPS dataactually work? In this two-part series, I will cover both topics andgive you the skills you need to write a commercial-grade GPSapplication that works with a majority of GPS devices in the industrytoday.</p><h3>One Powerful Sentence</h3><p>This first part in the series will explore the task of interpretingraw GPS data. Fortunately, the task is simplified thanks to theNational Marine Electronics Association (www.nmea.org), whichintroduced a standard for the industry now in use by a vast majority ofGPS devices. To give developers a head start, I chose to use someVisual Studio.NET source code from my "GPS.NET Global Positioning SDK"component. (The code is stripped of features like multithreading anderror handling for brevity.)</p><p>NMEA data is sent as comma-delimited "sentences" that containinformation based on the first word of the sentence. There are overfifty kinds of sentences, yet an interpreter really only needs tohandle a few to get the job done. The most common NMEA sentence of allis the "Recommended Minimum" sentence, which begins with "$GPRMC." Hereis an example:</p><pre>$GPRMC,040302.663,A,3939.7,N,10506.6,W,0.27,358.86,200804,,*1A</pre><p>This one sentence contains nearly everything a GPS applicationneeds: latitude, longitude, speed, bearing, satellite-derived time, fixstatus, and magnetic variation.</p><h3>The Core of An Interpreter</h3><p>The first step in making an NMEA interpreter is writing a methodthat does two things: separating each sentence into its individualwords and then examining the first word to figure out what informationis available to extract. Listing 1-1 shows the start of the interpreterclass.</p><p><b>Listing 1-1:</b> The core of an NMEA interpreter is a function which divides NMEA sentences into individual words.</p><pre>'*******************************************************
'**  Listing 1-1.  The core of an NMEA interpreter
'*******************************************************
Public Class NmeaInterpreter
 ' Processes information from the GPS receiver
 Public Function Parse(ByVal sentence As String) As Boolean
  ' Divide the sentence into words
  Dim Words() As String = GetWords(sentence)
  ' Look at the first word to decide where to go next
  Select Case Words(0)
   Case "$GPRMC"    ' A "Recommended Minimum" sentence was found!
    ' Indicate that the sentence was recognized
    Return True
   Case Else
    ' Indicate that the sentence was not recognized
    Return False
  End Select
 End Function
 ' Divides a sentence into individual words
 Public Function GetWords(ByVal sentence As String) As String()
  Return sentence.Split(","c)
 End Function
End Class</pre>
网 站: www.52xoo.com (3S,信息融合,数字图像处理,模式识别与人工智能等专业电子书、学术文章及源代码共享) E-mail: Jianhong72@163.com QQ: 88128745 (55359982用了近10年,最近被盗了,郁闷!!!)
举报 回复(0) 喜欢(0)     评分
wangjh
论坛版主
论坛版主
  • 注册日期2003-08-22
  • 发帖数994
  • QQ55359982
  • 铜币2579枚
  • 威望0点
  • 贡献值0点
  • 银元0个
5楼#
发布于:2005-03-11 09:07
<h3>The Core of An Interpreter (Continued)</h3>
The next step is to perform actual extraction of information,starting with latitude and longitude. Latitude and longitude are storedin the form "DD°MM'SS.S" where D represents hours (also called"degrees"), M represents minutes, and S represents seconds. Coordinatescan be displayed in shorthand, such as "DD°MM.M" or even "DD°." Thefourth word in the sentence, "3939.7," shows the current latitude ashours and minutes (39°39.7'), except the numbers are squished together.The first two characters (39) represent hours and the remainder of theword (39.7) represents minutes. Longitude is structured the same way,except that the first three characters represent hours (105006.6').Words five and seven indicate the "hemisphere," where "N" means"North," "W" means "West," and so forth. The hemisphere is appended tothe end of the numeric portion to make a complete measurement.</p><center><p><img src="http://www.codeguru.com/RealMedia/ads/adstream_lx.cgi/ew/ewsoftware/www.codeguru.com/vb/mobile/pocketpc/article/c8079__2/i/345006380/accessunit/default/empty.gif/64613530646434653432323961653830?_RM_EMPTY_"></p></center><p>I'vefound that NMEA interpreters are much easier to work with; they areevent-driven. This is because data arrives in no particular order. Anevent-driven class gives the interpreter the most flexibility andresponsiveness to an application. So, I'll design the interpreter toreport information using events. The first event, PositionReceived,will be raised whenever the current latitude and longitude arereceived. Listing 1-2 expands the interpreter to report the currentposition.</p><p><b>Listing 1-2:</b> The interpreter can now report the current latitude and longitude.</p><pre>'********************************************************
'**  Listing 1-2.  Extracting information from a sentence
'********************************************************
Public Class NmeaInterpreter
 ' Raised when the current location has changed
 Public Event PositionReceived(ByVal latitude As String, _
                               ByVal longitude As String)
 ' Processes information from the GPS receiver
 Public Function Parse(ByVal sentence As String) As Boolean
  ' Look at the first word to decide where to go next
  Select Case GetWords(sentence)(0)
   Case "$GPRMC"    ' A "Recommended Minimum" sentence was found!
    Return ParseGPRMC(sentence)
   Case Else
    ' Indicate that the sentence was not recognized
    Return False
  End Select
 End Function
 ' Divides a sentence into individual words
 Public Function GetWords(ByVal sentence As String) As String()
  Return sentence.Split(","c)
 End Function
 ' Interprets a $GPRMC message
 Public Function ParseGPRMC(ByVal sentence As String) As Boolean
  ' Divide the sentence into words
  Dim Words() As String = GetWords(sentence)
  ' Do we have enough values to describe our location?
  If Words(3) <> "" And Words(4) <> "" And Words(5) <> "" _
                    And Words(6) <> "" Then
   ' Yes. Extract latitude and longitude
   Dim Latitude As String = Words(3).Substring(0, 2) ; "0"
      ' Append hours
   Latitude = Latitude ; Words(3).Substring(2) ; """"
      ' Append minutes
   Latitude = Latitude ; Words(4)    ' Append the hemisphere
   Dim Longitude As String = Words(5).Substring(0, 3) ; "0"
      ' Append hours
   Longitude = Longitude ; Words(5).Substring(3) ; """"
      ' Append minutes
   Longitude = Longitude ; Words(6)     ' Append the hemisphere
   ' Notify the calling application of the change
   RaiseEvent PositionReceived(Latitude, Longitude)
  End If
  ' Indicate that the sentence was recognized
  Return True
 End Function
End Class</pre><p>One thing to watch out for here is that some GPS devices will reportblank values when no information is known. Therefore, it's a good ideato test each word for a value before parsing. If you need to type thedegree symbol (°), hold down the Alt key and type "0176" on the numerickeypad.</p>
网 站: www.52xoo.com (3S,信息融合,数字图像处理,模式识别与人工智能等专业电子书、学术文章及源代码共享) E-mail: Jianhong72@163.com QQ: 88128745 (55359982用了近10年,最近被盗了,郁闷!!!)
举报 回复(0) 喜欢(0)     评分
wangjh
论坛版主
论坛版主
  • 注册日期2003-08-22
  • 发帖数994
  • QQ55359982
  • 铜币2579枚
  • 威望0点
  • 贡献值0点
  • 银元0个
6楼#
发布于:2005-03-11 09:08
<h3>Taking Out the Garbage</h3>
Each NMEA sentence ends with an asterisk followed by a two-letterhexadecimal checksum. A checksum is calculated as the XOR of bytesbetween (but not including) the dollar sign and asterisk. This checksumis then compared with the checksum from the sentence. If the checksumsdo not match, the sentence is typically discarded. This is okay to dobecause the GPS devices tend to repeat the same information every fewseconds. With the ability to compare checksums, the interpreter is ableto throw out any sentence with an invalid checksum. Listing 1-3 expandsthe interpreter to do this.</p><p><b>Listing 1-3:</b> The interpreter can now detect errors and parse only error-free NMEA data.</p><pre>'*******************************************************
'**  Listing 1-3.  Detecting and handling NMEA errors
'*******************************************************
Public Class NmeaInterpreter
 ' Raised when the current location has changed
 Public Event PositionReceived(ByVal latitude As String, _
                               ByVal longitude As String)
 ' Processes information from the GPS receiver
 Public Function Parse(ByVal sentence As String) As Boolean
  ' Discard the sentence if its checksum does not match our
  ' calculated checksum
  If Not IsValid(sentence) Then Return False
  ' Look at the first word to decide where to go next
  Select Case GetWords(sentence)(0)
   Case "$GPRMC"    ' A "Recommended Minimum" sentence was found!
    Return ParseGPRMC(sentence)
   Case Else
    ' Indicate that the sentence was not recognized
    Return False
  End Select
 End Function
 ' Divides a sentence into individual words
 Public Function GetWords(ByVal sentence As String) As String()
  Return sentence.Split(","c)
 End Function
 ' Interprets a $GPRMC message
 Public Function ParseGPRMC(ByVal sentence As String) As Boolean
  ' Divide the sentence into words
  Dim Words() As String = GetWords(sentence)
  ' Do we have enough values to describe our location?
  If Words(3) <> "" And Words(4) <> "" And Words(5) <> "" _
                    And Words(6) <> "" Then
   ' Yes. Extract latitude and longitude
   Dim Latitude As String = Words(3).Substring(0, 2) ; "0"
      ' Append hours
   Latitude = Latitude ; Words(3).Substring(2) ; """"
      ' Append minutes
   Latitude = Latitude ; Words(4)     ' Append the hemisphere
   Dim Longitude As String = Words(5).Substring(0, 3) ; "0"
      ' Append hours
   Longitude = Longitude ; Words(5).Substring(3) ; """"
      ' Append minutes
   Longitude = Longitude ; Words(6)     ' Append the hemisphere
   ' Notify the calling application of the change
   RaiseEvent PositionReceived(Latitude, Longitude)
  End If
  ' Indicate that the sentence was recognized
  Return True
 End Function
 ' Returns True if a sentence's checksum matches the calculated
 ' checksum
 Public Function IsValid(ByVal sentence As String) As Boolean
  ' Compare the characters after the asterisk to the calculation
  Return sentence.Substring(sentence.IndexOf("*") + 1) = _
         GetChecksum(sentence)
 End Function
 ' Calculates the checksum for a sentence
 Public Function GetChecksum(ByVal sentence As String) As String
  ' Loop through all chars to get a checksum
  Dim Character As Char
  Dim Checksum As Integer
  For Each Character In sentence
   Select Case Character
    Case "$"c
     ' Ignore the dollar sign
    Case "*"c
     ' Stop processing before the asterisk
     Exit For
    Case Else
     ' Is this the first value for the checksum?
     If Checksum = 0 Then
      ' Yes. Set the checksum to the value
      Checksum = Convert.ToByte(Character)
     Else
      ' No. XOR the checksum with this character's value
      Checksum = Checksum Xor Convert.ToByte(Character)
     End If
   End Select
  Next
  ' Return the checksum formatted as a two-character hexadecimal
  Return Checksum.ToString("X2")
 End Function
End Class</pre>
网 站: www.52xoo.com (3S,信息融合,数字图像处理,模式识别与人工智能等专业电子书、学术文章及源代码共享) E-mail: Jianhong72@163.com QQ: 88128745 (55359982用了近10年,最近被盗了,郁闷!!!)
举报 回复(0) 喜欢(0)     评分
wangjh
论坛版主
论坛版主
  • 注册日期2003-08-22
  • 发帖数994
  • QQ55359982
  • 铜币2579枚
  • 威望0点
  • 贡献值0点
  • 银元0个
7楼#
发布于:2005-03-11 09:09
<h3>Wireless Atomic Time</h3>
Time is the cornerstone of GPS technology because distances aremeasured at the speed of light. Each GPS satellite contains four atomicclocks that it uses to time its radio transmissions within a fewnanoseconds. One fascinating feature is that with just a few lines ofcode, these atomic clocks can be used to synchronize a computer's clockwith millisecond accuracy. The second word of the $GPRMC sentence,"040302.663," contains satellite-derived time in a compressed format.The first two characters represent hours, the next two representminutes, the next two represent seconds, and everything after thedecimal place is milliseconds. So, the time is 4:03:02.663 AM. However,satellites report time in universal time (GMT+0), so the time must tobe adjusted to the local time zone. Listing 1-4 adds support forsatellite-derived time and uses the DateTime.ToLocalTime method toconvert satellite time to the local time zone.</p><p><b>Listing 1-4:</b> This class can now use atomic clocks to synchronize your computer's clock wirelessly.</p><pre>'********************************************************
'**  Listing 1-4.  Add support for satellite-derived time
'********************************************************
Public Class NmeaInterpreter
 ' Raised when the current location has changed
 Public Event PositionReceived(ByVal latitude As String, _
                               ByVal longitude As String)
 Public Event DateTimeChanged(ByVal dateTime As DateTime)
 ' Processes information from the GPS receiver
 Public Function Parse(ByVal sentence As String) As Boolean
  ' Discard the sentence if its checksum does not match our
  ' calculated checksum
  If Not IsValid(sentence) Then Return False
  ' Look at the first word to decide where to go next
  Select Case GetWords(sentence)(0)
   Case "$GPRMC"    ' A "Recommended Minimum" sentence was found!
    Return ParseGPRMC(sentence)
   Case Else
    ' Indicate that the sentence was not recognized
    Return False
  End Select
 End Function
 ' Divides a sentence into individual words
 Public Function GetWords(ByVal sentence As String) As String()
  Return sentence.Split(","c)
 End Function
 ' Interprets a $GPRMC message
 Public Function ParseGPRMC(ByVal sentence As String) As Boolean
  ' Divide the sentence into words
  Dim Words() As String = GetWords(sentence)
  ' Do we have enough values to describe our location?
  If Words(3) <> "" And Words(4) <> "" And Words(5) <> "" _
                    And Words(6) <> "" Then
   ' Yes. Extract latitude and longitude
   Dim Latitude As String = Words(3).Substring(0, 2) ; "0"
      ' Append hours
   Latitude = Latitude ; Words(3).Substring(2) ; """"
      ' Append minutes
   Latitude = Latitude ; Words(4)     ' Append the hemisphere
   Dim Longitude As String = Words(5).Substring(0, 3) ; "0"
      ' Append hours
   Longitude = Longitude ; Words(5).Substring(3) ; """"
      ' Append minutes
   Longitude = Longitude ; Words(6)     ' Append the hemisphere
   ' Notify the calling application of the change
   RaiseEvent PositionReceived(Latitude, Longitude)
  End If
  ' Do we have enough values to parse satellite-derived time?
  If Words(1) <> "" Then
   ' Yes. Extract hours, minutes, seconds and milliseconds
   Dim UtcHours As Integer   = CType(Words(1).Substring(0, 2), _
                                     Integer)
   Dim UtcMinutes As Integer = CType(Words(1).Substring(2, 2), _
                                     Integer)
   Dim UtcSeconds As Integer = CType(Words(1).Substring(4, 2), _
                                     Integer)
   Dim UtcMilliseconds As Integer
   ' Extract milliseconds if it is available
   If Words(1).Length > 7 Then UtcMilliseconds = _
      CType(Words(1).Substring(7), Integer)
   ' Now build a DateTime object with all values
   Dim Today As DateTime = System.DateTime.Now.ToUniversalTime
   Dim SatelliteTime As New System.DateTime(Today.Year, _
       Today.Month, Today.Day, UtcHours, UtcMinutes, UtcSeconds, _
       UtcMilliseconds)
   ' Notify of the new time, adjusted to the local time zone
   RaiseEvent DateTimeChanged(SatelliteTime.ToLocalTime)
  End If
  ' Indicate that the sentence was recognized
  Return True
 End Function
 ' Returns True if a sentence's checksum matches the calculated
 ' checksum
 Public Function IsValid(ByVal sentence As String) As Boolean
  ' Compare the characters after the asterisk to the calculation
  Return sentence.Substring(sentence.IndexOf("*") + 1) = _
         GetChecksum(sentence)
 End Function
 ' Calculates the checksum for a sentence
 Public Function GetChecksum(ByVal sentence As String) As String
  ' Loop through all chars to get a checksum
  Dim Character As Char
  Dim Checksum As Integer
  For Each Character In sentence
   Select Case Character
    Case "$"c
     ' Ignore the dollar sign
    Case "*"c
     ' Stop processing before the asterisk
     Exit For
    Case Else
     ' Is this the first value for the checksum?
     If Checksum = 0 Then
      ' Yes. Set the checksum to the value
      Checksum = Convert.ToByte(Character)
     Else
      ' No. XOR the checksum with this character's value
      Checksum = Checksum Xor Convert.ToByte(Character)
     End If
   End Select
  Next
  ' Return the checksum formatted as a two-character hexadecimal
  Return Checksum.ToString("X2")
 End Function
End Class</pre>
网 站: www.52xoo.com (3S,信息融合,数字图像处理,模式识别与人工智能等专业电子书、学术文章及源代码共享) E-mail: Jianhong72@163.com QQ: 88128745 (55359982用了近10年,最近被盗了,郁闷!!!)
举报 回复(0) 喜欢(0)     评分
wangjh
论坛版主
论坛版主
  • 注册日期2003-08-22
  • 发帖数994
  • QQ55359982
  • 铜币2579枚
  • 威望0点
  • 贡献值0点
  • 银元0个
8楼#
发布于:2005-03-11 09:10
<h3>Direction ; Speed Alerts</h3>
GPS devices analyze your position over time to calculate speed andbearing. The $GPRMC sentence at the beginning of this article alsoincludes these readings. Speed is always reported in knots and bearingis reported as an "azimuth," a measurement around the horizon measuredclockwise from 00 to 3600 where 00 represents north, 900 means east,and so on. A little math is applied to convert knots into miles perhour. The power of GPS is again demonstrated with one line of code inListing 1-5 that figures out whether a car is traveling over the speedlimit.</p><p><b>Listing 1-5:</b> This class now can tell you which direction you're going and help prevent a speeding ticket.</p><pre>'*******************************************************
'**  Listing 1-5.  Extracting speed and bearing
'*******************************************************
Public Class NmeaInterpreter
 ' Raised when the current location has changed
 Public Event PositionReceived(ByVal latitude As String, _
                               ByVal longitude As String)
 Public Event DateTimeChanged(ByVal dateTime As DateTime)
 Public Event BearingReceived(ByVal bearing As Double)
 Public Event SpeedReceived(ByVal speed As Double)
 Public Event SpeedLimitReached()
 ' Processes information from the GPS receiver
 Public Function Parse(ByVal sentence As String) As Boolean
  ' Discard the sentence if its checksum does not match our
  ' calculated checksum
  If Not IsValid(sentence) Then Return False
  ' Look at the first word to decide where to go next
  Select Case GetWords(sentence)(0)
   Case "$GPRMC"    ' A "Recommended Minimum" sentence was found!
    Return ParseGPRMC(sentence)
   Case Else
    ' Indicate that the sentence was not recognized
    Return False
  End Select
 End Function
 ' Divides a sentence into individual words
 Public Function GetWords(ByVal sentence As String) As String()
  Return sentence.Split(","c)
 End Function
 ' Interprets a $GPRMC message
 Public Function ParseGPRMC(ByVal sentence As String) As Boolean
  ' Divide the sentence into words
  Dim Words() As String = GetWords(sentence)
  ' Do we have enough values to describe our location?
  If Words(3) <> "" And Words(4) <> "" And Words(5) <> "" _
                    And Words(6) <> "" Then
   ' Yes. Extract latitude and longitude
   Dim Latitude As String = Words(3).Substring(0, 2) ; "0"
      ' Append hours
   Latitude = Latitude ; Words(3).Substring(2) ; """"
      ' Append minutes
   Latitude = Latitude ; Words(4)     ' Append the hemisphere
   Dim Longitude As String = Words(5).Substring(0, 3) ; "0"
      ' Append hours
   Longitude = Longitude ; Words(5).Substring(3) ; """"
      ' Append minutes
   Longitude = Longitude ; Words(6)     ' Append the hemisphere
   ' Notify the calling application of the change
   RaiseEvent PositionReceived(Latitude, Longitude)
  End If
  ' Do we have enough values to parse satellite-derived time?
  If Words(1) <> "" Then
   ' Yes. Extract hours, minutes, seconds and milliseconds
   Dim UtcHours As Integer   = CType(Words(1).Substring(0, 2), _
                                     Integer)
   Dim UtcMinutes As Integer = CType(Words(1).Substring(2, 2), _
                                     Integer)
   Dim UtcSeconds As Integer = CType(Words(1).Substring(4, 2), _
                                     Integer)
   Dim UtcMilliseconds As Integer
   ' Extract milliseconds if it is available
   If Words(1).Length > 7 Then UtcMilliseconds = _
      CType(Words(1).Substring(7), Integer)
   ' Now build a DateTime object with all values
   Dim Today As DateTime = System.DateTime.Now.ToUniversalTime
   Dim SatelliteTime As New System.DateTime(Today.Year, _
       Today.Month, Today.Day, UtcHours, UtcMinutes, UtcSeconds, _
       UtcMilliseconds)
   ' Notify of the new time, adjusted to the local time zone
   RaiseEvent DateTimeChanged(SatelliteTime.ToLocalTime)
  End If
  ' Do we have enough information to extract the current speed?
  If Words(7) <> "" Then
   ' Yes.  Convert it into MPH
   Dim Speed As Double = CType(Words(7), Double) * 1.150779
   ' If we're over 55MPH then trigger a speed alarm!
   If Speed > 55 Then RaiseEvent SpeedLimitReached()
   ' Notify of the new speed
   RaiseEvent SpeedReceived(Speed)
  End If
  ' Do we have enough information to extract bearing?
  If Words(8) <> "" Then
   ' Indicate that the sentence was recognized
   Dim Bearing As Double = CType(Words(8), Double)
   RaiseEvent BearingReceived(Bearing)
  End If
  ' Indicate that the sentence was recognized
  Return True
 End Function
 ' Returns True if a sentence's checksum matches the calculated
 " checksum
 Public Function IsValid(ByVal sentence As String) As Boolean
  ' Compare the characters after the asterisk to the calculation
  Return sentence.Substring(sentence.IndexOf("*") + 1) = _
         GetChecksum(sentence)
 End Function
 ' Calculates the checksum for a sentence
 Public Function GetChecksum(ByVal sentence As String) As String
  ' Loop through all chars to get a checksum
  Dim Character As Char
  Dim Checksum As Integer
  For Each Character In sentence
   Select Case Character
    Case "$"c
     ' Ignore the dollar sign
    Case "*"c
     ' Stop processing before the asterisk
     Exit For
    Case Else
     ' Is this the first value for the checksum?
     If Checksum = 0 Then
      ' Yes. Set the checksum to the value
      Checksum = Convert.ToByte(Character)
     Else
      ' No. XOR the checksum with this character's value
      Checksum = Checksum Xor Convert.ToByte(Character)
     End If
   End Select
  Next
  ' Return the checksum formatted as a two-character hexadecimal
  Return Checksum.ToString("X2")
 End Function
End Class
</pre>
网 站: www.52xoo.com (3S,信息融合,数字图像处理,模式识别与人工智能等专业电子书、学术文章及源代码共享) E-mail: Jianhong72@163.com QQ: 88128745 (55359982用了近10年,最近被盗了,郁闷!!!)
举报 回复(0) 喜欢(0)     评分
wangjh
论坛版主
论坛版主
  • 注册日期2003-08-22
  • 发帖数994
  • QQ55359982
  • 铜币2579枚
  • 威望0点
  • 贡献值0点
  • 银元0个
9楼#
发布于:2005-03-11 09:12
<h3>Are We Fixed Yet?</h3>
The $GPRMC sentence includes a value which indicates whether or nota "fix" has been obtained. A fix is possible when the signal strengthof at least three satellites is strong enough to be involved incalculating your location. If at least four satellites are involved,altitude also becomes known. The third word of the $GPRMC sentence isone of two letters: "A" for "active," where a fix is obtained, or "V"for "invalid" where no fix is present. Listing 1-6 includes code toexamine this character and report on fix status.
<p><b>Listing 1-6:</b> The interpreter now knows when the device has obtained a fix.</p><pre>'*******************************************************
'**  Listing 1-6.  Extracting satellite fix status
'*******************************************************
Public Class NmeaInterpreter
 ' Raised when the current location has changed
 Public Event PositionReceived(ByVal latitude As String, _
                               ByVal longitude As String)
 Public Event DateTimeChanged(ByVal dateTime As DateTime)
 Public Event BearingReceived(ByVal bearing As Double)
 Public Event SpeedReceived(ByVal speed As Double)
 Public Event SpeedLimitReached()
 Public Event FixObtained()
 Public Event FixLost()
 ' Processes information from the GPS receiver
 Public Function Parse(ByVal sentence As String) As Boolean
  ' Discard the sentence if its checksum does not match our
  ' calculated checksum
  If Not IsValid(sentence) Then Return False
  ' Look at the first word to decide where to go next
  Select Case GetWords(sentence)(0)
   Case "$GPRMC"      ' A "Recommended Minimum" sentence was found!
    Return ParseGPRMC(sentence)
   Case Else
    ' Indicate that the sentence was not recognized
    Return False
  End Select
 End Function
 ' Divides a sentence into individual words
 Public Function GetWords(ByVal sentence As String) As String()
  Return sentence.Split(","c)
 End Function
 ' Interprets a $GPRMC message
 Public Function ParseGPRMC(ByVal sentence As String) As Boolean
  ' Divide the sentence into words
  Dim Words() As String = GetWords(sentence)
  ' Do we have enough values to describe our location?
  If Words(3) <> "" And Words(4) <> "" And Words(5) <> ""
                    And Words(6) <> "" Then
   ' Yes. Extract latitude and longitude
   Dim Latitude As String = Words(3).Substring(0, 2) ; "0"
      ' Append hours
   Latitude = Latitude ; Words(3).Substring(2) ; """"
      ' Append minutes
   Latitude = Latitude ; Words(4)     ' Append the hemisphere
   Dim Longitude As String = Words(5).Substring(0, 3) ; "0"
      ' Append hours
   Longitude = Longitude ; Words(5).Substring(3) ; """"
      ' Append minutes
   Longitude = Longitude ; Words(6)     ' Append the hemisphere
   ' Notify the calling application of the change
   RaiseEvent PositionReceived(Latitude, Longitude)
  End If
  ' Do we have enough values to parse satellite-derived time?
  If Words(1) <> "" Then
   ' Yes. Extract hours, minutes, seconds and milliseconds
   Dim UtcHours As Integer   = CType(Words(1).Substring(0, 2), _
                                     Integer)
   Dim UtcMinutes As Integer = CType(Words(1).Substring(2, 2), _
                                     Integer)
   Dim UtcSeconds As Integer = CType(Words(1).Substring(4, 2), _
                                     Integer)
   Dim UtcMilliseconds As Integer
   ' Extract milliseconds if it is available
   If Words(1).Length > 7 Then UtcMilliseconds = _
      CType(Words(1).Substring(7), Integer)
   ' Now build a DateTime object with all values
   Dim Today As DateTime = System.DateTime.Now.ToUniversalTime
   Dim SatelliteTime As New System.DateTime(Today.Year, _
       Today.Month, Today.Day, UtcHours, UtcMinutes, UtcSeconds, _
       UtcMilliseconds)
   ' Notify of the new time, adjusted to the local time zone
   RaiseEvent DateTimeChanged(SatelliteTime.ToLocalTime)
  End If
  ' Do we have enough information to extract the current speed?
  If Words(7) <> "" Then
   ' Yes.  Convert it into MPH
   Dim Speed As Double = CType(Words(7), Double) * 1.150779
   ' If we're over 55MPH then trigger a speed alarm!
   If Speed > 55 Then RaiseEvent SpeedLimitReached()
   ' Notify of the new speed
   RaiseEvent SpeedReceived(Speed)
  End If
  ' Do we have enough information to extract bearing?
  If Words(8) <> "" Then
   ' Yes.  Report the bearing information
   Dim Bearing As Double = CType(Words(8), Double)
   RaiseEvent BearingReceived(Bearing)
  End If
  ' Does the device currently have a satellite fix?
  If Words(2) <> "" Then
   Select Case Words(2)
    Case "A"
     RaiseEvent FixObtained()
    Case "V"
     RaiseEvent FixLost()
   End Select
  End If
  ' Indicate that the sentence was recognized
  Return True
 End Function
 ' Returns True if a sentence's checksum matches the calculated
 ' checksum
 Public Function IsValid(ByVal sentence As String) As Boolean
  ' Compare the characters after the asterisk to the calculation
  Return sentence.Substring(sentence.IndexOf("*") + 1) = _
         GetChecksum(sentence)
 End Function
 ' Calculates the checksum for a sentence
 Public Function GetChecksum(ByVal sentence As String) As String
  ' Loop through all chars to get a checksum
  Dim Character As Char
  Dim Checksum As Integer
  For Each Character In sentence
   Select Case Character
    Case "$"c
     ' Ignore the dollar sign
    Case "*"c
     ' Stop processing before the asterisk
     Exit For
    Case Else
     ' Is this the first value for the checksum?
     If Checksum = 0 Then
      ' Yes. Set the checksum to the value
      Checksum = Convert.ToByte(Character)
     Else
      ' No. XOR the checksum with this character's value
      Checksum = Checksum Xor Convert.ToByte(Character)
     End If
   End Select
  Next
  ' Return the checksum formatted as a two-character hexadecimal
  Return Checksum.ToString("X2")
 End Function
End Class</pre><p>As you can see, a whole lot of information is packed into a singleNMEA sentence. Now that the $GPRMC sentence has been fully interpreted,the interpreter can be expanded to support a second sentence: $GPGSV.This sentence describes the configuration of satellites overhead, inreal-time.</p>
</p>
网 站: www.52xoo.com (3S,信息融合,数字图像处理,模式识别与人工智能等专业电子书、学术文章及源代码共享) E-mail: Jianhong72@163.com QQ: 88128745 (55359982用了近10年,最近被盗了,郁闷!!!)
举报 回复(0) 喜欢(0)     评分
上一页
游客

返回顶部