【问题标题】:HOW TO: Read out GPS attached via USB (VB.NET) [closed]如何:读出通过 USB (VB.NET) 连接的 GPS [关闭]
【发布时间】:2015-10-06 01:54:30
【问题描述】:

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

【问题讨论】:

  • 你应该把它分成一个问题和一个答案,否则它可能最终会被关闭。
  • 如果可以的话,我会对此评论投赞成票。我正在为问题/答案组合投票。
  • 感谢提示,进行拆分。
  • 您可以自行回答,但您提出的问题必须由其他人回答。这个问题太多太宽泛了。
  • 好的,gunr2171,你说的太对了,这是毫无疑问的。我解决了这个问题。因为我经常通过 stackoverflow 找到有用的 code-sn-ps,所以我只想分享结果。马克告诉我把我的帖子分成问答。我这样做了。似乎每个人都有话要说......

标签: vb.net gps serial-port usb


【解决方案1】:

我花了一些时间收集所有信息和代码-sn-ps 以从通过 USB 连接的 GPS 读取纬度/经度坐标。

所以这是生成的类 clsGpsLocation。它包括一个检查 com 端口以确定 GPS 是否已连接的函数(FindComPort)和一个返回纬度/经度的子函数(GetPos)。一个公共变量 _SatellitesInView 保存最后一次 GetPos 调用的卫星数量。

用法:

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

这是课程:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    相关资源
    最近更新 更多