⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classgps.vb

📁 Chuong trinh tren PDA cho phep ket noi ve server va cap nhat du lieu ve PDA
💻 VB
字号:
Imports System.IO.Ports
Public Class ClassGPS
    Private Port As SerialPort
    Public Property vPort()
        Get
            Return Port
        End Get
        Set(ByVal value)
            Port = value
        End Set
    End Property
    Public Function FindPort() As SerialPort
        Dim portNames As String() = SerialPort.GetPortNames()
        ' USB devices (more likely?) will probably be at a higher-numbered COM port
        Array.Reverse(portNames)
        Dim kq As SerialPort = Nothing
        Dim i As Integer = 0
        For Each portName As String In portNames
            Try
                'Console.WriteLine("Checking: {0}", portName)
                Dim arrboud() As Integer = {2400, 4800, 9600, 19200, 38400, 57600, 115200}
                For i = 0 To 6
                    kq = New SerialPort(portName, arrboud(i))
                    If IsGPSPort(Port) Then
                        Exit Try
                    Else
                        kq = Nothing
                    End If
                Next
            Catch
                Exit For
            End Try
            If i < 6 Then
                Exit For
            End If
        Next
        Return kq
    End Function
    Private Function IsGPSPort(ByVal p As SerialPort) As Boolean 'kiem tra port :port nay co phai la cong GPRS
        Dim kq As Boolean = False
        Dim line As String
        Try
            p.Open()
            line = ReadNextGoodLine(p)
            ' Do some check for NMEA data...
            If line IsNot Nothing Then
                kq = True
            End If
        Catch
        Finally
            If Not kq Then
                p.Close()
                p.Dispose()
            End If
        End Try
        Return kq
    End Function
    Private Function ReadNextGoodLine(ByVal p As SerialPort) As String
        ' Must read the next line.  This may not be a complete NMEA line
        ' (or even not an NMEA line at all!)
        Dim line As String = Nothing
        For i As Integer = 0 To 5
            ' Up to ten tries...
            line = p.ReadLine() 'doc du lieu cua port dang co 
            If Not IsSentenceValid(line) Then
                line = Nothing
            Else
                Exit For
            End If
        Next
        Return line
    End Function
    'Kiem tra chuoi du lieu cua mot port co phai co dung dinh dang NMEA
    Private Function IsSentenceValid(ByVal sentence As String) As Boolean
        ' Compare the characters after the asterisk to the calculation
        ' "X2" is the format string for hexadecimal
        Dim computedChecksum As String = GetChecksum(sentence).ToString("X2")
        Dim checksum As String = sentence.Substring(sentence.IndexOf("*") + 1, 2)
        Return checksum = computedChecksum
    End Function
    Private Function GetChecksum(ByVal sentence As String) As Integer
        ' A sentence can't be shorter than 9 characters ($XXXXX*0x)
        If sentence.Length < 9 Then
            Return -1
        End If
        ' Seed with the second character (remember, skip the $)
        Dim checksum As Integer = Convert.ToByte(sentence(1))
        Dim loc As Integer = 0, [end] As Integer = sentence.LastIndexOf("*"c)
        ' Loop through the characters
        For Each c As Char In sentence
            ' XOR every character between the dollar sign and the asterisk
            If loc > 1 AndAlso loc < [end] Then
                checksum = checksum Xor Convert.ToByte(c)
            End If
            loc += 1
        Next
        Return checksum
    End Function
    Public Sub CloseGPS()
        If Port IsNot Nothing Then
            If Port.IsOpen Then
                Port.Close()
            End If
            Port.Dispose()
            Port = Nothing
        End If
    End Sub
    Public Function InitGPS() As Boolean 'be use when GPS com be change

        If String.IsNullOrEmpty(Port.PortName) Then
            Port = FindPort()
        Else

            Port = New SerialPort(Port.PortName)
            If Not IsGPSPort(Port) Then
                Port = Nothing
            End If
        End If
        If Port IsNot Nothing Then
            Return True
        Else

            Return False
        End If
    End Function

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -