如何:读出通过USB连接的GPS(VB.NET) [英] HOW TO: Read out GPS attached via USB (VB.NET)

查看:201
本文介绍了如何:读出通过USB连接的GPS(VB.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何通过VB.NET从GPS-USB设备读取GPS数据?

我收集所有信息和代码片段,通过USB连接GPS来读取纬度/经度坐标。



所以这里是结果类clsGpsLocation。它包括一个函数,用于检查通讯端口是否连接GPS(FindComPort)和返回纬度/经度(GetPos)的子网。一个公共var _SatellitesInView包含最后一次GetPos调用的卫星数量。

用法:

  Dim GpsLocation as new clsGpsLocation 
Dim Latitude Double,Doubleitude Longitude
GpsLocation.GetPos(Latitude,Longitude)

以下是类:

 导入System.IO。端口
'公共SUBs和函数
'Sub New()
'Sub New(port $)'如果端口已知
'Function FindComPort()As String'返回例如'COM4'如果COM4读出返回GPS消息
'公共函数OpenGpsPort()作为布尔'返回TRUE,如果_SerPort打开或可以打开
'Public Sub GetPos(ByRef lat As Double,ByRef lon As Double)'在成功的情况下返回经度/纬度。 0,如果不是

Public Class clsGpsLocation

Dim _SerPort As New SerialPort()
Public _Port $
Public _SatellitesInView&

Dim GpsLogFile =Gps.Log

Sub New()
End Sub

Sub New(port $)
_Port $ = port $
End Sub

Public Function FindComPort()As String
FindComPort =

For i As Integer = 1 To 9
Try
_SerPort.Close()
Catch ex例外
结束尝试

_SerPort.PortName =COM&我
尝试
_SerPort.Open()
赶上例外
结束尝试

如果_SerPort.IsOpen然后
'5 Sekunden einlesen
Dim tmStart As Date = Now
while tmStart.AddSeconds(5)>现在
Application.DoEvents()
Dim msg $ = _SerPort.ReadExisting
如果msg.Contains($ GPRMC)然后
'Gefunden
_Port = _SerPort。 PortName
FindComPort = _SerPort.PortName
Exit Function
End If
Application.DoEvents()
End While
End If
Next

尝试
_SerPort.Close()
Catch ex例外
结束尝试

结束函数

公共函数OpenGpsPort()作为布尔
'Offen:OK
如果_SerPort.IsOpen然后返回True

'Port bereits ermittelt?
如果_Port<> 然后
_SerPort.PortName = _Port
尝试
_SerPort.Open()
抓住ex例外
结束尝试

如果_SerPort .IsOpen然后返回True
End If

'Port ermitteln
_Port = FindComPort()
返回_SerPort.IsOpen

End Function

公共函数IsOpen()作为布尔
返回_SerPort.IsOpen
结束函数

私有函数GetMsg()作为字符串
如果不是OpenGpsPort ()然后
返回
结束如果

'5 Sekunden einlesen
Dim tmStart As Date = Now
while tmStart.AddSeconds(5)> ;现在
Application.DoEvents()
Dim msg $ = _SerPort.ReadExisting
如果msg.Contains($ GPRMC)然后
'Gelesen
返回msg
End If
Application.DoEvents()
End While

'Nix
返回
End Function

Private Function toDecimal(ByVal Pos As String)As Double
'Pos =5601.0318
'度数:56,分钟数:010318
'Berechnung:十进制度数=度数+分钟数/ 60

'PosDb:56.010318
Dim PosDb As Double = CType(Replace(Pos,。,,),Double)'Replace。 (用于德语双打)
'Deg:56
Dim Deg As Double = Math.Floor(PosDb / 100)

Dim DecPos As Double = Math.Round( Deg +((PosDb - (Deg * 100))/ 60),5)
返回DecPos'= 56.0172

End Function

Public Sub GetPos(ByRef lat As Double,ByRef lon As Double)
lat = 0
lon = 0

如果不是OpenGpsPort()然后
Exit Sub
End If

Dim msg $
Dim sentence $
Dim LogSentence $

虽然True
msg $ = GetMsg()

Dim Sentences()As String = Split(msg $,$)
Dim bPosRead As Boolean = False

For I As Integer = 0 To Sentences.Count - 2'Den Letzten Satz nicht verarbeiten da der meistensverstümmeltist。 Es wird immer nur der Buffergefülltauch wenn der letzte Satz nicht mehr komplett passt。
sentence =句子(i)
Dim words()As String = Split(句子,,)
选择案例词(0)
案例GPGGA
lat = toDecimal(单词(2))
lon = toDecimal(单词(4))
_SatellitesInView& = CLng(单词(7))
LogSentence $ = Now& :&句子
bPosRead = True

CaseGPRMC
lat = toDecimal(words(3))
lon = toDecimal(words(5))
LogSentence $ = Now& :&句子
bPosRead = True

CaseGPGLL
lat = toDecimal(words(1))
lon = toDecimal(words(3))
LogSentence $ = Now& :&句子
bPosRead = True

CaseGPRMA
lat = toDecimal(words(2))
lon = toDecimal(words(4))
LogSentence $ = Now& :&句子
bPosRead = True

结束选择
Application.DoEvents()
下一个
如果bPosRead = True然后退出而
Application.DoEvents ()
End While

'GpsLogFile
'调用一些函数将LogSentence $写入GpsLogFile

End Sub

End Class




Blockquote



How can i read out GPS-Data from a GPS-USB-Device via VB.NET?

解决方案

it took me a while to gather all information and code-snippets to read latitude/longitude coordinates from a GPS attached via USB.

so here is the resulting class clsGpsLocation. it includes a function that checks com ports to determin wether a GPS is connected (FindComPort) and a sub that returns latitude/longitude (GetPos). a public var _SatellitesInView holds the number of satellites of the last GetPos-call.

usage:

Dim GpsLocation as new clsGpsLocation
Dim Latitude as Double, Longitude as Double
GpsLocation.GetPos(Latitude, Longitude)

and here is the class:

Imports System.IO.Ports
'Public SUBs and FUNCTIONS
'Sub New()
'Sub New(port$) 'if port is known
'Function FindComPort() As String 'returns for example 'COM4' if COM4 read out returns GPS-Messages
'Public Function OpenGpsPort() As Boolean 'returns TRUE if _SerPort is open or could be opened
'Public Sub GetPos(ByRef lat As Double, ByRef lon As Double) 'returns latitude / longitude in case of success. 0, 0 if not

Public Class clsGpsLocation

Dim _SerPort As New SerialPort()
Public _Port$
Public _SatellitesInView&

Dim GpsLogFile = "Gps.Log"

Sub New()
End Sub

Sub New(port$)
    _Port$ = port$
End Sub

Public Function FindComPort() As String
    FindComPort = ""

    For i As Integer = 1 To 9
        Try
            _SerPort.Close()
        Catch ex As Exception
        End Try

        _SerPort.PortName = "COM" & i
        Try
            _SerPort.Open()
        Catch ex As Exception
        End Try

        If _SerPort.IsOpen Then
            '5 Sekunden einlesen
            Dim tmStart As Date = Now
            While tmStart.AddSeconds(5) > Now
                Application.DoEvents()
                Dim msg$ = _SerPort.ReadExisting
                If msg.Contains("$GPRMC") Then
                    'Gefunden
                    _Port = _SerPort.PortName
                    FindComPort = _SerPort.PortName
                    Exit Function
                End If
                Application.DoEvents()
            End While
        End If
    Next

    Try
        _SerPort.Close()
    Catch ex As Exception
    End Try

End Function

Public Function OpenGpsPort() As Boolean
    'Offen: OK
    If _SerPort.IsOpen Then Return True

    'Port bereits ermittelt?
    If _Port <> "" Then
        _SerPort.PortName = _Port
        Try
            _SerPort.Open()
        Catch ex As Exception
        End Try

        If _SerPort.IsOpen Then Return True
    End If

    'Port ermitteln
    _Port = FindComPort()
    Return _SerPort.IsOpen

End Function

Public Function IsOpen() As Boolean
    Return _SerPort.IsOpen
End Function

Private Function GetMsg() As String
    If Not OpenGpsPort() Then
        Return ""
    End If

    '5 Sekunden einlesen
    Dim tmStart As Date = Now
    While tmStart.AddSeconds(5) > Now
        Application.DoEvents()
        Dim msg$ = _SerPort.ReadExisting
        If msg.Contains("$GPRMC") Then
            'Gelesen
            Return msg
        End If
        Application.DoEvents()
    End While

    'Nix
    Return ""
End Function

Private Function toDecimal(ByVal Pos As String) As Double
    'Pos="5601.0318"
    'Degrees: 56, Minutes: 010318
    'Berechnung: Decimal Degrees = Degrees + Minutes/60

    'PosDb: 56.010318
    Dim PosDb As Double = CType(Replace(Pos, ".", ","), Double) 'Replace . with , (Used in german doubles)
    'Deg: 56
    Dim Deg As Double = Math.Floor(PosDb / 100)

    Dim DecPos As Double = Math.Round(Deg + ((PosDb - (Deg * 100)) / 60), 5)
    Return DecPos '=56.0172

End Function

Public Sub GetPos(ByRef lat As Double, ByRef lon As Double)
    lat = 0
    lon = 0

    If Not OpenGpsPort() Then
        Exit Sub
    End If

    Dim msg$
    Dim sentence$
    Dim LogSentence$

    While True
        msg$ = GetMsg()

        Dim Sentences() As String = Split(msg$, "$")
        Dim bPosRead As Boolean = False

        For i As Integer = 0 To Sentences.Count - 2 'Den letzten Satz nicht verarbeiten da der meistens verstümmelt ist. Es wird immer nur der Buffer gefüllt auch wenn der letzte Satz nicht mehr komplett passt.
            sentence = Sentences(i)
            Dim words() As String = Split(sentence, ",")
            Select Case words(0)
                Case "GPGGA"
                    lat = toDecimal(words(2))
                    lon = toDecimal(words(4))
                    _SatellitesInView& = CLng(words(7))
                    LogSentence$ = Now & ":" & sentence
                    bPosRead = True

                Case "GPRMC"
                    lat = toDecimal(words(3))
                    lon = toDecimal(words(5))
                    LogSentence$ = Now & ":" & sentence
                    bPosRead = True

                Case "GPGLL"
                    lat = toDecimal(words(1))
                    lon = toDecimal(words(3))
                    LogSentence$ = Now & ":" & sentence
                    bPosRead = True

                Case "GPRMA"
                    lat = toDecimal(words(2))
                    lon = toDecimal(words(4))
                    LogSentence$ = Now & ":" & sentence
                    bPosRead = True

            End Select
            Application.DoEvents()
        Next
        If bPosRead = True Then Exit While
        Application.DoEvents()
    End While

    'GpsLogFile 
    'call some function that writes LogSentence$ into GpsLogFile

End Sub

End Class

Blockquote

这篇关于如何:读出通过USB连接的GPS(VB.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆