📄 testgpsreader.cs
字号:
// TestGPSReader.cs
//
// Copyright (C) 2003 JW Hedgehog, Inc. All rights reserved
//
// JW Hedgehog, Inc
// http://www.jwhh.com
//
// Direct questions to mailto: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
// ---------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using GPSExample.Util ;
namespace TestGPSReader
{
/// <summary>
/// GPSReader test program.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnStartGPS;
private System.Windows.Forms.Button btnStopGPS;
private System.Windows.Forms.ListBox lbMessageDisplay;
internal System.Windows.Forms.Label Label1;
internal System.Windows.Forms.Label lblBearing;
internal System.Windows.Forms.Label Label7;
internal System.Windows.Forms.Label lblQuality;
internal System.Windows.Forms.Label Label6;
internal System.Windows.Forms.Label lblFixType;
internal System.Windows.Forms.Label Label4;
internal System.Windows.Forms.Label lblLonDirection;
internal System.Windows.Forms.Label lblLatDirection;
internal System.Windows.Forms.Label lblNumSats;
internal System.Windows.Forms.Label Label2;
internal System.Windows.Forms.Label lblLon;
internal System.Windows.Forms.Label Label5;
internal System.Windows.Forms.Label lblLat;
internal System.Windows.Forms.Label Label3;
internal System.Windows.Forms.Label lblTime;
internal System.Windows.Forms.Label lblMessageType;
private System.Windows.Forms.MainMenu mainMenu1;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// Construct the reader
// The two-parameter constructor uses default port settings. It is equivalent to the following
// _gpsReader = new GPSReader("COM4:", 19200, ParitySetting.NoParity, 8, StopBitsSetting.OneStopBit)
_gpsReader = new GPSReader("COM4:", 19200) ;
// Attach to GPSReader Events
_gpsReader.OnGPSMessage += new GPSEventHandler(_gpsReader_OnGPSMessage);
_gpsReader.OnGPSReadStart += new EventHandler(_gpsReader_OnGPSReadStart);
_gpsReader.OnGPSReadStop +=new EventHandler(_gpsReader_OnGPSReadStop);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.btnStartGPS = new System.Windows.Forms.Button();
this.btnStopGPS = new System.Windows.Forms.Button();
this.lbMessageDisplay = new System.Windows.Forms.ListBox();
this.Label1 = new System.Windows.Forms.Label();
this.lblBearing = new System.Windows.Forms.Label();
this.Label7 = new System.Windows.Forms.Label();
this.lblQuality = new System.Windows.Forms.Label();
this.Label6 = new System.Windows.Forms.Label();
this.lblFixType = new System.Windows.Forms.Label();
this.Label4 = new System.Windows.Forms.Label();
this.lblLonDirection = new System.Windows.Forms.Label();
this.lblLatDirection = new System.Windows.Forms.Label();
this.lblNumSats = new System.Windows.Forms.Label();
this.Label2 = new System.Windows.Forms.Label();
this.lblLon = new System.Windows.Forms.Label();
this.Label5 = new System.Windows.Forms.Label();
this.lblLat = new System.Windows.Forms.Label();
this.Label3 = new System.Windows.Forms.Label();
this.lblTime = new System.Windows.Forms.Label();
this.lblMessageType = new System.Windows.Forms.Label();
//
// btnStartGPS
//
this.btnStartGPS.Location = new System.Drawing.Point(32, 8);
this.btnStartGPS.Text = "Start GPS";
this.btnStartGPS.Click += new System.EventHandler(this.btnStartGPS_Click);
//
// btnStopGPS
//
this.btnStopGPS.Location = new System.Drawing.Point(128, 8);
this.btnStopGPS.Text = "Stop GPS";
this.btnStopGPS.Click += new System.EventHandler(this.btnStopGPS_Click);
//
// lbMessageDisplay
//
this.lbMessageDisplay.Location = new System.Drawing.Point(16, 160);
this.lbMessageDisplay.Size = new System.Drawing.Size(216, 86);
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(40, 40);
this.Label1.Size = new System.Drawing.Size(48, 16);
this.Label1.Text = "Time:";
this.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// lblBearing
//
this.lblBearing.Location = new System.Drawing.Point(96, 136);
this.lblBearing.Size = new System.Drawing.Size(64, 16);
//
// Label7
//
this.Label7.Location = new System.Drawing.Point(32, 136);
this.Label7.Size = new System.Drawing.Size(56, 16);
this.Label7.Text = "Bearing:";
this.Label7.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// lblQuality
//
this.lblQuality.Location = new System.Drawing.Point(96, 120);
this.lblQuality.Size = new System.Drawing.Size(64, 16);
//
// Label6
//
this.Label6.Location = new System.Drawing.Point(32, 120);
this.Label6.Size = new System.Drawing.Size(56, 16);
this.Label6.Text = "Quality:";
this.Label6.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// lblFixType
//
this.lblFixType.Location = new System.Drawing.Point(96, 104);
this.lblFixType.Size = new System.Drawing.Size(64, 16);
//
// Label4
//
this.Label4.Location = new System.Drawing.Point(32, 104);
this.Label4.Size = new System.Drawing.Size(56, 16);
this.Label4.Text = "Fix Type:";
this.Label4.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// lblLonDirection
//
this.lblLonDirection.Location = new System.Drawing.Point(176, 72);
this.lblLonDirection.Size = new System.Drawing.Size(32, 16);
this.lblLonDirection.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// lblLatDirection
//
this.lblLatDirection.Location = new System.Drawing.Point(176, 56);
this.lblLatDirection.Size = new System.Drawing.Size(32, 16);
this.lblLatDirection.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// lblNumSats
//
this.lblNumSats.Location = new System.Drawing.Point(96, 88);
this.lblNumSats.Size = new System.Drawing.Size(64, 16);
//
// Label2
//
this.Label2.Location = new System.Drawing.Point(32, 88);
this.Label2.Size = new System.Drawing.Size(56, 16);
this.Label2.Text = "# Sats:";
this.Label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// lblLon
//
this.lblLon.Location = new System.Drawing.Point(96, 72);
this.lblLon.Size = new System.Drawing.Size(64, 16);
//
// Label5
//
this.Label5.Location = new System.Drawing.Point(32, 72);
this.Label5.Size = new System.Drawing.Size(56, 16);
this.Label5.Text = "Lon:";
this.Label5.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// lblLat
//
this.lblLat.Location = new System.Drawing.Point(96, 56);
this.lblLat.Size = new System.Drawing.Size(64, 16);
//
// Label3
//
this.Label3.Location = new System.Drawing.Point(32, 56);
this.Label3.Size = new System.Drawing.Size(56, 16);
this.Label3.Text = "Lat:";
this.Label3.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// lblTime
//
this.lblTime.Location = new System.Drawing.Point(96, 40);
this.lblTime.Size = new System.Drawing.Size(104, 16);
//
// lblMessageType
//
this.lblMessageType.Location = new System.Drawing.Point(8, 248);
this.lblMessageType.Size = new System.Drawing.Size(224, 16);
//
// Form1
//
this.Controls.Add(this.lblMessageType);
this.Controls.Add(this.Label1);
this.Controls.Add(this.lblBearing);
this.Controls.Add(this.Label7);
this.Controls.Add(this.lblQuality);
this.Controls.Add(this.Label6);
this.Controls.Add(this.lblFixType);
this.Controls.Add(this.Label4);
this.Controls.Add(this.lblLonDirection);
this.Controls.Add(this.lblLatDirection);
this.Controls.Add(this.lblNumSats);
this.Controls.Add(this.Label2);
this.Controls.Add(this.lblLon);
this.Controls.Add(this.Label5);
this.Controls.Add(this.lblLat);
this.Controls.Add(this.Label3);
this.Controls.Add(this.lblTime);
this.Controls.Add(this.lbMessageDisplay);
this.Controls.Add(this.btnStopGPS);
this.Controls.Add(this.btnStartGPS);
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Text = "Test GPS (C#)";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private GPSReader _gpsReader ;
/// <summary>
/// Initiate the GPS reading process
/// StartRead launches the background thread and begins reading from the port
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStartGPS_Click(object sender, System.EventArgs e)
{
lblMessageType.Text = "Read Starting..." ;
_gpsReader.StartRead() ;
}
/// <summary>
/// Terminate GPS reading process
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStopGPS_Click(object sender, System.EventArgs e)
{
lblMessageType.Text = "Read Stopping..." ;
_gpsReader.StopRead() ;
}
/// <summary>
/// This event is called everytime a GPS sentence is received by the GPSReader
/// </summary>
/// <param name="sender"></param>
/// <param name="arg"></param>
private void _gpsReader_OnGPSMessage(object sender, GPSEventArgs arg)
{
// Add the complete sentence to the listbox
lbMessageDisplay.Items.Insert(0,arg.MessageText) ;
if (lbMessageDisplay.Items.Count > 8)
lbMessageDisplay.Items.RemoveAt(8) ;
// Update the appropriate screen fields based on the message type
switch(arg.MessageType)
{
case "GPGGA":
lblTime.Text = ((int)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() ;
break;
case "GPGSA":
lblFixType.Text = arg.FixType.ToString() ;
break;
case "GPRMC":
lblBearing.Text = arg.Bearing.ToString() ;
break;
case "GPVTG":
lblBearing.Text = arg.Bearing.ToString() ;
break;
}
}
/// <summary>
/// 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
/// </summary>
/// <param name="sender"></param>
/// <param name="arg"></param>
private void _gpsReader_OnGPSReadStart(object sender, EventArgs arg)
{
lblMessageType.Text = "Read Started - Mode: " + _gpsReader.ActiveReadMode.ToString() ;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="sender"></param>
/// <param name="arg"></param>
private void _gpsReader_OnGPSReadStop(object sender, EventArgs arg)
{
lblMessageType.Text = "Read Stopped" ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -