如何:读取通过USB连接的GPS(VB.NET)

时间:2015-07-16 14:35:29

标签: vb.net gps serial-port usb

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

1 个答案:

答案 0 :(得分:1)

我花了一段时间收集所有信息和代码片段,以便通过USB连接GPS读取纬度/经度坐标。

所以这里是结果类clsGpsLocation。它包括一个检查com端口以确定GPS连接的功能(FindComPort)和返回纬度/经度(GetPos)的子功能。公共var _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