Files
GpsClient/GpsTestApp/Form1.cs

75 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using KollNet.Lib;
using KollNet.Lib.EventArguments;
using KollNet.Lib.Models;
namespace GpsTestApp {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void ConnectButtonClicked(object sender, EventArgs e) {
if (string.IsNullOrEmpty(ComPort.Text)) {
string comPort = GpsClient.FindGps();
if (!string.IsNullOrEmpty(comPort)) {
string[] parts = comPort.Split(':');
ComPort.Text = parts[0];
BaudRate.Text = parts[1];
} else {
XtraMessageBox.Show("No COM Port was specified and an active GPS COM Port was not found");
return;
}
}
if (!GpsClient.Connect(ComPort.Text, Convert.ToInt32(BaudRate.Text))) {
XtraMessageBox.Show("Unable to connect to " + ComPort.Text + " with Baud " + BaudRate.Text);
return;
}
GpsClient.PositionChanged += GpsClient_PositionChanged;
GpsClient.NmeaReceived += GpsClient_NmeaReceived;
GpsClient.SatellitesUpdated += GpsClient_SatellitesUpdated;
}
private void GpsClient_SatellitesUpdated(object sender, SatellitesUpdatedEventArgs e) {
this.InvokeIfRequired(() => {
memoEdit1.AppendText("Received Satellite Data for " + e.ActiveSatellites.Count + " satellites" + Environment.NewLine);
});
}
private void GpsClient_NmeaReceived(object sender, NmeaReceivedEventArgs e) {
this.InvokeIfRequired(() => {
if (e.NmeaSentence.StartsWith("$GPGSV")) {
Clipboard.SetText(e.NmeaSentence);
}
});
}
private void GpsClient_PositionChanged(object sender, GpsCoordinatesChangedEventArgs e) {
string data = "LL[" + e.Coordinates.Latitude.ToString("0.000000") + "," + e.Coordinates.Longitude.ToString("0.000000") +
"] SC[" + e.Coordinates.Speed + "," + e.Coordinates.Course + "]";
Console.WriteLine(data);
this.InvokeIfRequired(() => {
memoEdit1.AppendText(data + Environment.NewLine);
TimeOffset.Text = GpsClient.GpsTimeOffset.ToString();
UtcTime.Text = DateTime.UtcNow.Add(GpsClient.GpsTimeOffset).ToString();
LocalTime.Text = DateTime.UtcNow.Add(GpsClient.GpsTimeOffset).ToLocalTime().ToString();
});
}
}
}