我使用串口从GPS读取一条线。一段时间后,似乎消息正在排队。为了进行测试,我做了一个事件,将GPS时间与计算机时间进行比较。起初他们是平等的,但几分钟后,他们跳了长达4秒钟。好像消息正在缓冲。
在下面输入我的代码。
Imports System.IO.Ports
Imports System.Threading
Public Class gps_serial
Public _serialPort As SerialPort
Public coordN As String
Public coordE As String
Public gpsString As String
Public time As String
Public Event newTime()
Sub newTimeRaise()
RaiseEvent newTime()
End Sub
Public Property open() As Boolean
Get
Return m_Open
End Get
Set(value As Boolean)
m_Open = value
End Set
End Property
Private m_Open As Boolean
Public Function connect(com As String, baud As Integer) As Boolean
Try
_serialPort = New SerialPort()
_serialPort.PortName = com
_serialPort.BaudRate = baud
_serialPort.Encoding = System.Text.Encoding.GetEncoding(1252)
AddHandler _serialPort.DataReceived, AddressOf DataReceivedHandler
_serialPort.Open()
If _serialPort.IsOpen Then
open = True
Return True
Else
open = False
Return False
End If
Catch
Return False
End Try
End Function
Public Function disconnect()
open = False
_serialPort.Close()
Return True
End Function
Private Sub DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs)
If _serialPort.IsOpen = True Then
Dim sp As SerialPort = CType(sender, SerialPort)
Try
Dim GPSdata As String = sp.ReadLine()
Dim split_GPSdata As String() = GPSdata.Split(New Char() {","c})
If split_GPSdata(0) = "$GPGGA" Then
gpsString = GPSdata
coordN = split_GPSdata(2)
coordE = split_GPSdata(4)
If split_GPSdata(1) <> time Then
time = split_GPSdata(1)
newTimeRaise()
End If
End If
Catch
End Try
End If
End Sub
End Class