📄 testgpsreader.vb
字号:
' TestGPSReader.vb
'
' Copyright (C) 2003 JW Hedgehog, Inc. All rights reserved
'
' JW Hedgehog, Inc
' http://www.jwhh.com
'
' Direct questions to jimw@jwhh.com
'
' This code, comments and information are provided "AS IS" with
' no warrenty of any kind, either expressed or implied, including
' but not limited to the implied warranties of merchentability and/or
' fitness for a particular purpose
' -------------------------------------------------------------------
' GPSReader test program
Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents GPS As GPSReader
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnStartGPS As System.Windows.Forms.Button
Friend WithEvents lbMessageDisplay As System.Windows.Forms.ListBox
Friend WithEvents lblTime As System.Windows.Forms.Label
Friend WithEvents lblLat As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents lblLon As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents lblMessageType As System.Windows.Forms.Label
Friend WithEvents lblNumSats As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents lblLatDirection As System.Windows.Forms.Label
Friend WithEvents lblLonDirection As System.Windows.Forms.Label
Friend WithEvents lblFixType As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents lblQuality As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents lblBearing As System.Windows.Forms.Label
Friend WithEvents Label7 As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents btnStopGPS As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.btnStartGPS = New System.Windows.Forms.Button
Me.btnStopGPS = New System.Windows.Forms.Button
Me.lbMessageDisplay = New System.Windows.Forms.ListBox
Me.lblTime = New System.Windows.Forms.Label
Me.lblLat = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.lblLon = New System.Windows.Forms.Label
Me.Label5 = New System.Windows.Forms.Label
Me.lblMessageType = New System.Windows.Forms.Label
Me.lblNumSats = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.lblLatDirection = New System.Windows.Forms.Label
Me.lblLonDirection = New System.Windows.Forms.Label
Me.lblFixType = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.lblQuality = New System.Windows.Forms.Label
Me.Label6 = New System.Windows.Forms.Label
Me.lblBearing = New System.Windows.Forms.Label
Me.Label7 = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
'
'btnStartGPS
'
Me.btnStartGPS.Location = New System.Drawing.Point(32, 8)
Me.btnStartGPS.Text = "Start GPS"
'
'btnStopGPS
'
Me.btnStopGPS.Location = New System.Drawing.Point(112, 8)
Me.btnStopGPS.Text = "Stop GPS"
'
'lbMessageDisplay
'
Me.lbMessageDisplay.Location = New System.Drawing.Point(8, 152)
Me.lbMessageDisplay.Size = New System.Drawing.Size(216, 86)
'
'lblTime
'
Me.lblTime.Location = New System.Drawing.Point(80, 40)
Me.lblTime.Size = New System.Drawing.Size(104, 16)
'
'lblLat
'
Me.lblLat.Location = New System.Drawing.Point(80, 56)
Me.lblLat.Size = New System.Drawing.Size(64, 16)
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(16, 56)
Me.Label3.Size = New System.Drawing.Size(56, 16)
Me.Label3.Text = "Lat:"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'lblLon
'
Me.lblLon.Location = New System.Drawing.Point(80, 72)
Me.lblLon.Size = New System.Drawing.Size(64, 16)
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(16, 72)
Me.Label5.Size = New System.Drawing.Size(56, 16)
Me.Label5.Text = "Lon:"
Me.Label5.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'lblMessageType
'
Me.lblMessageType.Location = New System.Drawing.Point(8, 248)
Me.lblMessageType.Size = New System.Drawing.Size(224, 16)
'
'lblNumSats
'
Me.lblNumSats.Location = New System.Drawing.Point(80, 88)
Me.lblNumSats.Size = New System.Drawing.Size(64, 16)
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 88)
Me.Label2.Size = New System.Drawing.Size(56, 16)
Me.Label2.Text = "# Sats:"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'lblLatDirection
'
Me.lblLatDirection.Location = New System.Drawing.Point(160, 56)
Me.lblLatDirection.Size = New System.Drawing.Size(32, 16)
Me.lblLatDirection.TextAlign = System.Drawing.ContentAlignment.TopCenter
'
'lblLonDirection
'
Me.lblLonDirection.Location = New System.Drawing.Point(160, 72)
Me.lblLonDirection.Size = New System.Drawing.Size(32, 16)
Me.lblLonDirection.TextAlign = System.Drawing.ContentAlignment.TopCenter
'
'lblFixType
'
Me.lblFixType.Location = New System.Drawing.Point(80, 104)
Me.lblFixType.Size = New System.Drawing.Size(64, 16)
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(16, 104)
Me.Label4.Size = New System.Drawing.Size(56, 16)
Me.Label4.Text = "Fix Type:"
Me.Label4.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'lblQuality
'
Me.lblQuality.Location = New System.Drawing.Point(80, 120)
Me.lblQuality.Size = New System.Drawing.Size(64, 16)
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(16, 120)
Me.Label6.Size = New System.Drawing.Size(56, 16)
Me.Label6.Text = "Quality:"
Me.Label6.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'lblBearing
'
Me.lblBearing.Location = New System.Drawing.Point(80, 136)
Me.lblBearing.Size = New System.Drawing.Size(64, 16)
'
'Label7
'
Me.Label7.Location = New System.Drawing.Point(16, 136)
Me.Label7.Size = New System.Drawing.Size(56, 16)
Me.Label7.Text = "Bearing:"
Me.Label7.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 40)
Me.Label1.Size = New System.Drawing.Size(48, 16)
Me.Label1.Text = "Time:"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'Form1
'
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.lblBearing)
Me.Controls.Add(Me.Label7)
Me.Controls.Add(Me.lblQuality)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.lblFixType)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.lblLonDirection)
Me.Controls.Add(Me.lblLatDirection)
Me.Controls.Add(Me.lblNumSats)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.lblMessageType)
Me.Controls.Add(Me.lblLon)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.lblLat)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.lblTime)
Me.Controls.Add(Me.lbMessageDisplay)
Me.Controls.Add(Me.btnStopGPS)
Me.Controls.Add(Me.btnStartGPS)
Me.Menu = Me.MainMenu1
Me.MinimizeBox = False
Me.Text = "Test GPS (VB)"
End Sub
Public Shared Sub Main()
Application.Run(New Form1)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Construct the reader
' The two-parameter constructor uses default port settings. It is equivalent to the following
' GPS = New GPSReader("COM4:", 19200, ParitySetting.NoParity, 8, StopBitsSetting.OneStopBit)
GPS = New GPSReader("COM4:", 19200)
End Sub
' Initiate the GPS reading process
' StartRead launches the background thread and begins reading from the port
Private Sub btnStartGPS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartGPS.Click
lblMessageType.Text = "Read Starting..."
GPS.StartRead()
End Sub
' Terminate GPS reading process
Private Sub btnStopGPS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopGPS.Click
lblMessageType.Text = "Read Stopping..."
GPS.StopRead()
End Sub
' This event is called everytime a GPS sentence is received by the GPSReader
Private Sub GPS_OnGPSMessage(ByVal sender As Object, ByVal arg As GPSExample.Util.GPSEventArgs) Handles GPS.OnGPSMessage
' Add the complete sentence to the listbox
lbMessageDisplay.Items.Insert(0, arg.MessageText)
If lbMessageDisplay.Items.Count > 8 Then
lbMessageDisplay.Items.RemoveAt(8)
End If
' Update the appropriate screen fields based on the message type
Select Case arg.MessageType
Case "GPGGA"
lblTime.Text = CInt(arg.Time).ToString("D6") ' Show only whole time value
lblLat.Text = arg.Lat.ToString("F6") ' Show exactly 6 decimal places
lblLatDirection.Text = arg.LatDirection
lblLon.Text = arg.Lon.ToString("F6") ' Show exactly 6 decimal places
lblLonDirection.Text = arg.LonDirection
lblNumSats.Text = arg.NumSats.ToString
lblQuality.Text = arg.Quality.ToString
Case "GPGSA"
lblFixType.Text = arg.FixType.ToString
Case "GPRMC"
lblBearing.Text = arg.Bearing.ToString
Case "GPVTG"
lblBearing.Text = arg.Bearing.ToString
End Select
End Sub
' This method is called when the background thread enters the GPS Read loop
' It is not required that your code handle this event. It simply provides a confirmation
' that the reading process has actually begun
Private Sub GPS_OnGPSReadStart(ByVal sender As Object, ByVal e As System.EventArgs) Handles GPS.OnGPSReadStart
lblMessageType.Text = "Read Started - Mode: " + GPS.ActiveReadMode.ToString()
End Sub
' This method is called when the background thread exits the GPS Read loop
' It is not required that your code handle this event. It simply provides a notification
' that the read loop is shutting down. The event fires whether the thread terminated because
' of a call to StopRead or if an error occured.
Private Sub GPS_OnGPSReadStop(ByVal sender As Object, ByVal e As System.EventArgs) Handles GPS.OnGPSReadStop
lblMessageType.Text = "Read Stopped"
End Sub
Private Sub lblLon_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblLon.ParentChanged
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -