如何编写GPS应用程序:清除数据中的垃圾

如何编写GPS应用程序:清除GPS DATA垃圾
通过对字节做异或运算得出校验和(但不包括美元符号和星号)。然后,此校验
相对于从语句中返回的校验和。如果校验和不匹配,这条语句通常被丢弃。这是可以的,因为GPS设备往往每隔几秒钟重复着同样的信息。通过
比较校验和,解释器能够抛弃任何
带有无效的校验和的语句。

清单1-3:解释器现在可以检测错误,并解析只没有差错的NMEA数据。
'***************************************** **************
'**清单1-3。检测和处理NMEA错误
http://www.beikei.net
'************************************************* ******
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) & "�"' 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) & "�"' 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

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

glling

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值