西安GPS全球定位系统 : GPS功能必须具备GPS终端 、传输网络和监控平台三个要素;这三个要素缺一不可;通过这三个要素,可以提供车辆定位、防盗、反劫、行驶路线监控及呼叫指挥等功能。 西安天伯电子科技是一家集研发,生产,运营为一体的综合型GPS定位系统 公司。公司前身以车载GPS,车载影音 的研发、销售为主。2009年,公司在西安成立了运营中心, 正式向西北市场进军,整个产品销售网络辐射陕西、甘肃、宁夏、青海、新疆、内蒙、山东、河北、四川、西藏地区。公司提供:陕西GPS车载 设备、甘肃GPS车载 设备、宁夏GPS车载 设备、青海GPS车载 设备、 新疆GPS车载 设备、内蒙GPS车载 设备、山东GPS车载 设备、河北GPS车载 设备、四川GPS车载 设备、西藏GPS车载 设备的销售和安装服务工作,公司还提供个人定位仪,GPS定位软件的销售,所有安装我们公司GPS车载的大客户我们还同步提供GPS监控软件 平台的稳定运行以及平台使用培训等工作。
西安GPS定位系统|陕西GPS定位系统|GPS定位系统 GPS安装|西北GPS|内蒙GPS|甘肃GPS|新疆GPS GPS安装|西北GPS|内蒙GPS|甘肃GPS|新疆GPS|GPS定位系统
首页   |   企业邮箱   |   招聘
搜索 : 陕西GPS代理|西安GPS代理
GPS车载定位|GPS定位
Gps新闻中心 

 

 

天伯科技步步同程

GPS常见问题|GPS入门 专家教你怎样写一个GPS应用程序
监控平台入口
帐号:  
密码:  
看不清?点击更换    
服务热线
电话:029-88888776
电话:029-88881880
传真:029-88278689
邮箱:
tianboo@tianboo.net
地址:
陕西省西安市莲湖区南二环西段202号九座花园东区10F

专家教你怎样写一个GPS应用程序

原文:How to Write a GPS Application - Introduction
作者:Jon Person 翻译:YuL
需要什么样的GPS应用程序才能满足商业级的要求呢,像安装在车里的导航器一样?而且,GPS数据是怎么被解释,从而应用于实际工作中的呢?在这两部分里,我将和大家一起探讨这两个问题,并且给你编写商业级GPS应用程序必要的技巧,让专业的GPS设备能够更好的为你所用。
一句重要的语句
第一部分将探讨描述GPS原始数据的工作。实际上这个工作已经被国家舰船电子协会(下面简称NMEA,www.nmea.org)给简化了许多,该网站介 绍了一个正广泛应用于GPS主流设备的工业标准。为了给广大的开发人员一个良好的开端,我选择使用的一些VS.NET的源代码来自我的“GPS.NET Global Position SDK”组件。(为了简短这些代码,我去掉了诸如多线程和错误处理的部分。)
NMEA数据通过一个“逗号分隔的语句”来传递,这个语句包含的信息都基于语句的第一个单词。这里有五十种以上类型的语句,不过真正的一个描述仅仅需要处理少量的采集数据。最终常用的NMEA语句是“推荐最小”语句,它以“$GPRMC.”开头。这里有个一例子:
$GPRMC,040302.663,A,3939.7,N,10506.6,W,0.27,358.86,200804,,*1A
这条语句基本上包含了GPS应用程序所需的全部数据:纬度、经度、速度、方向、卫星时间、状态以及磁场变量。
第一步就是要写一个方法来解释NMEA数据,这个方法需要完成两件事:将每条语句分解为独立的单词,和检查首单词是否有效。清单 1-1 就是这个描述类的开始部分。
清单 1-1:一个NMEA描述的核心功能是将NMEA语句分解成单个的单词。
'** Listing 1-1. The core of an NMEA interpreter
'*******************************************************
Public Class NmeaInterpreter
' 处理来自GPS接收器的信息
Public Function Parse(ByVal sentence As String) As Boolean
' 将语句分解为单词
Dim Words() As String = GetWords(sentence)
' 通过匹配首单词来决定下一步的工作
Select Case Words(0)
Case "$GPRMC" ' 一条“推荐最小”的语句被找到!
' 标示这条语句为可用
Return True
Case Else
' 标示这条语句为不可用
Return False
End Select
End Function
' 将语句分解为单词
Public Function GetWords(ByVal sentence As String) As String()
Return sentence.Split(","c)
End Function
End Class
接下来一步就是要分析提取出来的信息了,让我们从纬度和经度开始吧。纬度和经度存储格式为“DDD°MM’SS.S,”,其中D表示时(也可以叫 “度”),M表示分,S表示秒。坐标可以简单表示为“DD°MM.M’”或者直接就表示为“DD°.”语句中第四个单词(“3939.7,”)以度和分的 形式表示当前的纬度为39°39.7’。头两个字符(39)表示度,余下的部分(39.7)表示分。经度和纬度是相同的结构,从这个语句来看要注意,头三 个字符表示经度的度(105°06.6’)。第五和第七个单词标示“半球”,其中“N”代表“北半球”,“W”代表“西经”。半球信息放在数字部分后面组 成完整的测量信息。
我还发现NMEA描述在事件驱动下使工作更容易完成。这是因为并不是按照特定的顺序来获取数据。一个事件驱动类为一应用程序给出了它最具灵活性和响应性的描述。因此,我也将使用事件来设计描述,从而获取信息。PositionReceived是第一个事件,它将记录当前获得的纬度和经度。清单 1-2 扩展这个描述器,来报告当前位置。
清单 1-2:这个描述器可以报告当前的纬度和经度。
'*******************************************************
'************* 清单 1-2. 从语句中提取信息 *************
'*******************************************************
Public Class NmeaInterpreter
' 当前位置变化时,记录新位置
Public Event PositionReceived(ByVal latitude As String, _
ByVal longitude As String)
' 处理GPS接收器的信息
Public Function Parse(ByVal sentence As String) As Boolean
' 通过匹配首单词来决定下一步的工作
Select Case GetWords(sentence)(0)
Case "$GPRMC" ' 一条“推荐最小”的语句被找到!
Return ParseGPRMC(sentence)
Case Else
' 标示这条语句为不可用
Return False
End Select
End Function
' 将语句分解为单词
Public Function GetWords(ByVal sentence As String) As String()
Return sentence.Split(","c)
End Function
' 描述 $GPRMC 消息
Public Function ParseGPRMC(ByVal sentence As String) As Boolean
' 将语句分解为单词
Dim Words() As String = GetWords(sentence)
' 我们是否有足够的数据来描述当前位置?
If Words(3) <> "" And Words(4) <> "" And Words(5) <> "" And _
Words(6) <> "" Then
' 是,则提取纬度和经度
Dim Latitude As String = Words(3).Substring(0, 2) & "°" ' 获取度
Latitude = Latitude & Words(3).Substring(2) & """" ' 获取分
Latitude = Latitude & Words(4) ' 获取半球
Dim Longitude As String = Words(5).Substring(0, 3) & "°" ' 获取度
Longitude = Longitude & Words(5).Substring(3) & """" ' 获取分
Longitude = Longitude & Words(6) ' 获取半球
' 将该变化通知应用程序
RaiseEvent PositionReceived(Latitude, Longitude)
End If
' 表示该语句可用
Return True
End Function
End Class
当然,我们肯定注意到了当没有接受到任何信息时,有些GPS设备将报告空值。因此,在解意描述前先判断每个单词是一个很不错的想法。如果你需要打度的符号(°),那么按Alt健并且用数字键盘输入“0176”就OK了。
检验和(Checksum)就是采用XOR的方法来计算美元符($)和星号(*)之间的字节。这个检验和是用来与语句的检验和比较的。如果检验和不匹配,语句将被弃用。这个做法是很有效的,因为GPS设备每隔很短的时间就返回一批相同的信息。利用检验和的比较能力,描述就可以剔除每一无效的语句。清单 1-3 扩展描述器类来完成这个功能。
清单 1-3:这个描述器可以检测错误并且仅仅解意释放错误NMEA数据
'** 清单 1-3. 检测并处理NMEA错误
'*******************************
Public Class NmeaInterpreter
' 当前位置变化时,记录新位置
Public Event PositionReceived(ByVal latitude As String, _
ByVal longitude As String)
' 处理GPS接收器的信息
Public Function Parse(ByVal sentence As String) As Boolean
' 如果检验和不能和我们计算的检验和匹配,那么丢弃它
If Not IsValid(sentence) Then Return False
' 通过匹配首单词来决定下一步的工作
Select Case GetWords(sentence)(0)
Case "$GPRMC" ' 一条“推荐最小”的语句被找到!
Return ParseGPRMC(sentence)
Case Else
' 标示这条语句为不可用
Return False
End Select
End Function
' 将语句分解为单词
Public Function GetWords(ByVal sentence As String) As String()
Return sentence.Split(","c)
End Function
' 描述 $GPRMC 消息
Public Function ParseGPRMC(ByVal sentence As String) As Boolean
' 将语句分解为单词
Dim Words() As String = GetWords(sentence)
' 我们是否有足够的数据来描述当前位置?
If Words(3) <> "" And Words(4) <> "" And Words(5) <> "" And _
Words(6) <> "" Then
' 是,则提取纬度和经度
Dim Latitude As String = Words(3).Substring(0, 2) & "°" ' 获取度
Latitude = Latitude & Words(3).Substring(2) & """" ' 获取分
Latitude = Latitude & Words(4) ' 获取半球
Dim Longitude As String = Words(5).Substring(0, 3) & "°" ' 获取度
Longitude = Longitude & Words(5).Substring(3) & """" ' 获取分
Longitude = Longitude & Words(6) ' 获取半球
' 将该变化通知应用程序
RaiseEvent PositionReceived(Latitude, Longitude)
End If
' 表示该语句可用
Return True
End Function
' 如果检验和匹配,则返回TRUE
Public Function IsValid(ByVal sentence As String) As Boolean
' 星号计算后比较字符
Return sentence.Substring(sentence.IndexOf("*") + 1) = GetChecksum(sentence)
End Function
' 计算语句的检验和
Public Function GetChecksum(ByVal sentence As String) As String
' 通过循环获得检验和
Dim Character As Char
Dim Checksum As Integer
For Each Character In sentence
Select Case Character
Case "$"c
' 忽略美元符号
Case "*"c
' 遇到星号则停止计算
Exit For
Case Else
' 是否为检验和的第一个值?
If Checksum = 0 Then
' 是。将该值赋给检验和
Checksum = Convert.ToByte(Character)
Else
' 否。采用XOR算法和当前检验和值计算,并将结构赋给检验和
Checksum = Checksum Xor Convert.ToByte(Character)
End If
End Select
Next
' 返回一个十六进制的检验和
Return Checksum.ToString("X2")
End Function
End Class
时间是GPS技术的基础,因为距离通过光速来测量得到的。每个GPS卫星包含四个原子钟,它将在一段以十亿分之一秒为单位的时间内计量它的射线传播。一个 强大的特性是仅用很少的代码,这些原子钟可以和精确到毫秒的电脑钟同步。$GPRMC语句的第二个单词(040302.663)包含了卫星时间,这个时间是用压缩格式表示的。头两个字符表示小时,接下来两个字符表示分钟,再下来两个字符表示秒,最后小数部分表示毫秒。因此,这个时间是4:03: 02.663 AM。然而,卫星报告的时间是国际时间(GMT+0),因此,这个时间必须被调整到区域的当地时间。清单 1-4 添加支持将卫星时间利用DateTime.ToLocalTime方法转换到当地时间的功能。
清单 1-4:这个类可以无线地即时将原子钟同步到你的电脑钟
'********************************************************
'** 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) & "°" ' 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
' 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)
End If
' 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
GPS设备分析你的位置根据时间计算速度和方向。本文开头部分的$GPRMC语句也包含这几个部分。速度总是被报告为哩/小时,并且方向被报告为方位角(azimuth,以水平顺时针测量,范围为0°到360°,其中0°表示正北,90°表示正东,其它的依次类推)。这里要用到一个简单的数学计算,将哩 /小时转换为公里/小时。在清单 1-5 中你将再次看到GPS数据被一行代码描述出来,这就如同一个汽车超速所展示出的那样。
清单 1-5:这个类现在可以告诉你要去哪里,而且可以帮你少领超速罚单
'** 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) & "°" ' 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
' 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
这个$GPRMC语句包含一个用“fix”表示的值。它表示你现在获得的信号强度至少来自三颗卫星,这样就足够用来计算你当前的位置了。如果有四颗卫星的话,还可以获得高程信息。$GPRMC语句的第三个单词“A”表示“活动的”的意思,那么我们就知道当前的信号是可用的,如果是“V”,则表示不可用。清 单 1-6 包含的代码可以用来检查这个字符并报告是否可用。
清单 1-6:这个描述起现在可以让我们获知设备的数据是否是“fix”的
'*******************************************************
'** 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) & "°" ' 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
' 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
' 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

如你所看到的,整个信息被打包在一条单一的NMEA语句中。现在这个$GPRMC语句已经完全被描述出来了,这个描述器可以扩展用来支持第二条语句:$ GPGSV,这个语句实时地表达了卫星的形态。
对于确定数据精确度和GPS稳定性,卫星的位置是非常重要的信息。既然GPS精确读将被详细地在这两部分介绍,那么这一部分将主要描述卫星位置和信号的强度。
卫星二十四小时在轨道上运转着,它们在任一时间、任一地点上都至少有六颗能被用户看到。卫星不断地监测地球,这样就避免出现一些盲点或者卫星无法看到的地方。就像在天空中找星星一样,卫星的位置被表示为一个方位角和高程。如前面所述,方位角为直接水平测量。高程测量则为一个与水平面的夹角,其中0°表示水 平,90°表示为“天顶”(或者说是头顶)。因此,如果设备说卫星的方位角为45°且高程为45°,那么卫星现在的位置就是处于水平的东北方向,高度为一 半的位置。另外对于卫星位置,设备报告每个卫星的“随机伪代码”(简称PRC),这个数值用来唯一标示一个卫星。
这里有一个关于 $GPGSV 的语句:
$GPGSV,3,1,10,24,82,023,40,05,62,285,32,01,62,123,00,17,59,229,28*70
每条语句包含四部分内容,例如:第一部分是“24,82,023,40”,第二部分是“05,62,285,32”等等。每部分的第一个词为PRC,第二个词为卫星高程,跟着为方位角和信号强度。如果这个卫星信息用图来显示,那么就如图 1-1。

(图 1-1:$GPGSV语句的图形表示,中心点为当前位置,周边的圆标示水平面。)

这个语句里最重要的指标应该算是“信号躁声比(signal-to-noise ratio)”(以下简称为SNR)。这个数值标示卫星信号的接收率。我们知道,卫星是以相同的强度发射信号,但是传播过程中难免会遇到诸如树和墙之类的 障碍物,这样就影响了信号的识别。典型的SNR值在0到50之间,其中50表示非常好的信号。(SNR可以达到99,但是我还从来没有见过50以上的数据 哦。)。在图 1-1里,绿色卫星表示强信号,然而黄色卫星则为中等(在第二部分,我将提供一个方法来实现信号强度的分类)。卫星#1的信号完全被阻挡了。清单 1-7 说明了扩展读取卫星信息后的描述器。
清单 1-7:描述器添加了描述GPS卫星当前位置的功能。
'** Listing 1-7. Extracting satellite information
'*******************************************************
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()
Public Event SatelliteReceived(ByVal pseudoRandomCode As Integer, _
ByVal azimuth As Integer, _
ByVal elevation As Integer, _
ByVal signalToNoiseRatio As Integer)
' 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 "$GPGSV" ' A "Satellites in View" message was found
Return ParseGPGSV(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
' 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
' 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
' Interprets a "Satellites in View" NMEA sentence
Public Function ParseGPGSV(ByVal sentence As String) As Boolean
Dim PseudoRandomCode As Integer
Dim Azimuth As Integer
Dim Elevation As Integer
Dim SignalToNoiseRatio As Integer
' Divide the sentence into words
Dim Words() As String = GetWords(sentence)
' Each sentence contains four blocks of satellite information.
' Read each block and report each satellite's information
Dim Count As Integer
For Count = 1 To 4
' Does the sentence have enough words to analyze?
If (Words.Length - 1) >= (Count * 4 + 3) Then
' Yes. Proceed with analyzing the block. Does it contain any
' information?
If Words(Count * 4) <> "" And Words(Count * 4 + 1) <> "" _
And Words(Count * 4 + 2) <> "" And Words(Count * 4 + 3) <> "" Then
' Yes. Extract satellite information and report it
PseudoRandomCode = CType(Words(Count * 4), Integer)
Elevation = CType(Words(Count * 4 + 1), Integer)
Azimuth = CType(Words(Count * 4 + 2), Integer)
SignalToNoiseRatio = CType(Words(Count * 4 + 2), Integer)
' Notify of this satellite's information
RaiseEvent SatelliteReceived(PseudoRandomCode, Azimuth, Elevation, _
SignalToNoiseRatio)
End If
End If
Next
' 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
国外的读者也许已经想到了一个敏感的问题,就是关于美国用了那种数据格式的问题,这些我们在清单里处理!有些国家(比如:比利时和瑞典)使用不同的数据格式,为了让应用程序工作那就不得不修改这个描述器了。还好,.NET框架里面提供了不同文化差异下数值转换的方法,因此我们直接修改描述器就可以了。在这个描述器里,只有速度是小数值,所以只要改一下它就行了。NmeaCultrueInfo变量表示NMEA语句中有关不同文化的数值。 Double.Parse方法可以用来将速度变量转换为机器当前文化对应的数值。清单 1-8 给出了描述器完整的清单,现在就可以把它用在其它地方了。

清单 1-8:完整描述器,适合世界各种文化的国家
'** Listing 1-8. Adding support for international cultures
'*************************************************************
Imports System.Globalization
Public Class NmeaInterpreter
' Represents the EN-US culture, used for numers in NMEA sentences
Private NmeaCultureInfo As New CultureInfo("en-US")
' Used to convert knots into miles per hour
Private MPHPerKnot As Double = Double.Parse("1.150779", NmeaCultureInfo)
' 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()
Public Event SatelliteReceived(ByVal pseudoRandomCode As Integer, _
ByVal azimuth As Integer, _
ByVal elevation As Integer, _
ByVal signalToNoiseRatio As Integer)
' 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 "$GPGSV"
Return ParseGPGSV(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
' 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)
End If
' 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. Parse the speed and convert it to MPH
Dim Speed As Double = Double.Parse(Words(7), NmeaCultureInfo) _
* MPHPerKnot
' Notify of the new speed
RaiseEvent SpeedReceived(Speed)
' Are we over the highway speed limit?
If Speed > 55 Then RaiseEvent SpeedLimitReached()
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
' 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
' Interprets a "Satellites in View" NMEA sentence
Public Function ParseGPGSV(ByVal sentence As String) As Boolean
Dim PseudoRandomCode As Integer
Dim Azimuth As Integer
Dim Elevation As Integer
Dim SignalToNoiseRatio As Integer
' Divide the sentence into words
Dim Words() As String = GetWords(sentence)
' Each sentence contains four blocks of satellite information.
' Read each block
' and report each satellite's information
Dim Count As Integer
For Count = 1 To 4
' Does the sentence have enough words to analyze?
If (Words.Length - 1) >= (Count * 4 + 3) Then
' Yes. Proceed with analyzing the block. Does it contain any information?
If Words(Count * 4) <> "" And Words(Count * 4 + 1) <> "" _
And Words(Count * 4 + 2) <> "" And Words(Count * 4 + 3) <> "" Then
' Yes. Extract satellite information and report it
PseudoRandomCode = CType(Words(Count * 4), Integer)
Elevation = CType(Words(Count * 4 + 1), Integer)
Azimuth = CType(Words(Count * 4 + 2), Integer)
SignalToNoiseRatio = CType(Words(Count * 4 + 2), Integer)
' Notify of this satellite's information
RaiseEvent SatelliteReceived(PseudoRandomCode, Azimuth, Elevation, _
SignalToNoiseRatio)
End If
End If
Next
' 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
总结
你现在应该已经很好的理解这个NMEA描述器是如何从语句里提取单词的了。你现在可以通过卫星来定位、同步你的电脑钟、找到你的方向、探测你的速度,并且 阴天也一样可以知道卫星在天上的位置。这个描述器无须任何改动就可以工作在.NET简装框架上。如果语句被存储在一个文件里,那么这个描述器可以用来回放 你的旅行过程。所有这些让人激动的特点,仅仅用了这么一个小小的类就完成,但是这个描述器能导航你的汽车或者帮你打高尔夫吗?肯定不行。这里保留了一个重 要的主题,关于如何是GPS应用程序很好的工作于现实世界:精确度的问题。GPS设备报告它们获取的一切信息,然而信息是不精确的。事实上,当前位置信息 可以有半个足球场那么大的误差,甚至设备采用了时下流行的DGPS和WAAS修正技术!不幸的是,不少开发人员并没有意识到这个问题。这里许多第三方的组 件,当然这些组件不适合用于商业应用程序,因为它们要加强精确度最小程度。还是建议你把这篇文章放在手边,下一篇文章还用得上。我会在那篇文章里详细介绍 加强精度,并让这个描述器可以更适用于专家级高精度应用!



陕西GPS销售|陕西GPS代理
友情链接>> 孙悟空 站长之家
网站导航>>网站地图天伯 | 车载GPS定位系统西安GPS定位系统 |  陕西GPS定位系统   | GPS全球定位系统GPS定位系统
西安GPS|陕西GPS 西安GPS定位|陕西GPS定位 西安GPS定位系统|陕西GPS定位系统
西安GPS安装|陕西GPS安装