Compare commits

..

4 Commits

25 changed files with 161 additions and 166 deletions

View File

@@ -1,5 +1,6 @@
using System; using GpsClient2.EventArguments;
using GpsClient2.Model; using GpsClient2.Model;
using System;
namespace GpsClient2 { namespace GpsClient2 {
public abstract class BaseGpsClient { public abstract class BaseGpsClient {
@@ -40,16 +41,6 @@ namespace GpsClient2 {
#region Events Triggers #region Events Triggers
protected virtual void OnGpsDataReceived(GpsDataEventArgs e) { protected virtual void OnGpsDataReceived(GpsDataEventArgs e) {
if (GpsInfo.CoordinateSystem == GpsCoordinateSystem.Lambert72) {
var x = 0.0d;
var y = 0.0d;
var h = 0.0d;
CoordinateConverterUtilities.GeoETRS89ToLambert72(e.Latitude, e.Longitude, 0, ref x, ref y, ref h);
e.CoordinateSystem = GpsCoordinateSystem.Lambert72;
e.Latitude = x;
e.Longitude = y;
}
GpsCallbackEvent?.Invoke(this, e); GpsCallbackEvent?.Invoke(this, e);
} }

View File

@@ -1,13 +1,10 @@
using GpsClient2.Model; using GpsClient2.EventArguments;
using GpsClient2.NmeaMessages;
using GpsClient2.Exceptions; using GpsClient2.Exceptions;
using GpsClient2.Model;
using GpsClient2.NmeaMessages;
using System; using System;
using System.Collections.Generic; using System.ComponentModel;
using System.Globalization;
using System.IO.Ports; using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace GpsClient2 { namespace GpsClient2 {
@@ -18,7 +15,8 @@ namespace GpsClient2 {
private SerialPort _serialPort; private SerialPort _serialPort;
private DateTime? _previousReadTime; private DateTime? _previousReadTime;
private readonly BackgroundWorker worker = new BackgroundWorker();
private ComPortInfo data = null;
#endregion #endregion
#region Constructors #region Constructors
@@ -34,30 +32,36 @@ namespace GpsClient2 {
#region Connect and Disconnect #region Connect and Disconnect
public override bool Connect() { public override bool Connect() {
var data = (ComPortInfo)GpsInfo; data = (ComPortInfo)GpsInfo;
IsRunning = true; IsRunning = true;
OnGpsStatusChanged(GpsStatus.Connecting); OnGpsStatusChanged(GpsStatus.Connecting);
_serialPort = new SerialPort(data.ComPort, 9600, Parity.None, 8, StopBits.One); _serialPort = new SerialPort(data.ComPort, data.BaudRate, Parity.None, 8, StopBits.One);
// Attach a method to be called when there // Attach a method to be called when there
// is data waiting in the port's buffer // is data waiting in the port's buffer
_serialPort.DataReceived += port_DataReceived; //_serialPort.DataReceived += port_DataReceived;
try { try {
// Begin communications // Begin communications
_serialPort.Open(); _serialPort.Open();
OnGpsStatusChanged(GpsStatus.Connected); OnGpsStatusChanged(GpsStatus.Connected);
// Enter an application loop to keep this thread alive // Enter an application loop to keep this thread alive
while (_serialPort.IsOpen) { worker.DoWork += Worker_DoWork;
Thread.Sleep(data.ReadFrequenty); worker.RunWorkerAsync();
} return true;
} catch { } catch {
Disconnect(); Disconnect();
throw; throw;
} }
}
return true; private async void Worker_DoWork(object sender, DoWorkEventArgs e) {
while (_serialPort.IsOpen) {
await Task.Delay(data.ReadFrequency);
//Thread.Sleep(data.ReadFrequenty);
processData();
}
} }
public override bool Disconnect() { public override bool Disconnect() {
@@ -66,24 +70,103 @@ namespace GpsClient2 {
OnGpsStatusChanged(GpsStatus.Disabled); OnGpsStatusChanged(GpsStatus.Disabled);
return true; return true;
} }
#endregion #endregion
#region Location Callbacks #region Location Callbacks
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) {
processData();
}
private void processData() {
try { try {
var readString = _serialPort.ReadExisting(); var readString = _serialPort.ReadExisting().Trim();
OnRawGpsDataReceived(readString);
var result = _parser.Parse(readString); foreach (string sentence in readString.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)) {
if (typeof(GprmcMessage) != result.GetType()) return; if (sentence.Length > 0 && sentence.StartsWith("$") && sentence.Contains("*")) {
if (_previousReadTime != null && GpsInfo.ReadFrequenty != 0 && ((GprmcMessage)result).UpdateDate.Subtract(new TimeSpan(0, 0, 0, 0, GpsInfo.ReadFrequenty)) <= _previousReadTime) return; parseSentence(sentence.Trim());
OnGpsDataReceived(new GpsDataEventArgs((GprmcMessage)result)); }
} catch (UnknownTypeException ex) { }
Console.WriteLine(ex.Message); } catch (Exception ex) {
Console.WriteLine(" ==> " + ex.Message);
} }
} }
private void parseSentence(string sentence) {
if (sentence.StartsWith("$GPGSV")) { return; } // Ignore all $GPGSV messages
//Console.WriteLine(sentence);
try {
OnRawGpsDataReceived(sentence);
var result = _parser.Parse(sentence);
if (result == null) { return; }
if (typeof(GprmcMessage) != result.GetType()) return;
if (_previousReadTime != null && GpsInfo.ReadFrequency != 0 && ((GprmcMessage)result).UpdateDate.Subtract(new TimeSpan(0, 0, 0, 0, GpsInfo.ReadFrequency)) <= _previousReadTime) return;
OnGpsDataReceived(new GpsDataEventArgs((GprmcMessage)result));
} catch (UnknownTypeException ex) {
Console.WriteLine(" ==> " + ex.Message + " : " + sentence);
} catch (ArgumentException ex) {
Console.WriteLine(" ==> " + ex.Message + " : " + sentence);
}
}
#endregion
#region "Find My GPS Device Methods"
public async Task<bool> FindGps() {
int[] bauds = { 9600, 4800 };
foreach (string comPort in SerialPort.GetPortNames()) {
foreach (int baud in bauds) {
if (await IsGpsPort(comPort, baud)) {
GpsInfo = new ComPortInfo(comPort, baud);
return true;
}
}
}
return false;
}
private static async Task<bool> IsGpsPort(string comPort, int baudRate) {
bool result = await Task.Run(() => {
Console.Write("Attempting to locate GPS on " + comPort + ":" + baudRate + "...");
SerialPort serial = new SerialPort(comPort, baudRate, Parity.None, 8, StopBits.One);
serial.ReadTimeout = 3000;
try {
serial.Open();
Console.Write(" Connected. Listening for NMEA Sentences...");
} catch (Exception ex) {
Console.WriteLine(" Error: " + ex.Message);
return false;
}
string sData = "";
while (true) {
string s;
try {
s = serial.ReadLine();
} catch (Exception ex) {
serial.Close();
Console.WriteLine(" Error: " + ex.Message);
return false;
}
if (string.IsNullOrEmpty(s)) { return false; }
sData += s + Environment.NewLine;
if (sData.Length > 1500) {
if (sData.Contains("$GPRMC") || sData.Contains("$GPGGA") || sData.Contains("$GPGSA")) {
Console.WriteLine(" This is a GPS device!");
serial.Close();
return true;
}
}
}
});
return result;
}
#endregion #endregion
} }
} }

View File

@@ -1,30 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2 {
internal class CoordinateConverterUtilities {
#if WIN64
private const string DllImport = @"plugins/ETRS89_LAMBERT_UTM_64bits.dll";
#else
private const string DllImport = @"plugins/ETRS89_LAMBERT_UTM_32bits.dll";
#endif
#region Coordinate conversion functions using NGI DLL
//Import the dll with the functions to calculate lambert coordinates
[DllImport(DllImport, SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GeoETRS89ToLambert72(double Xi, double Yi, double Zi, ref double xo, ref double yo, ref double Ho);
[DllImport(DllImport, SetLastError = true, CharSet = CharSet.Auto)]
public static extern int Lambert72ToLambert08(double Xi, double Yi, double Zi, ref double xo, ref double yo, ref double Ho);
[DllImport(DllImport, SetLastError = true, CharSet = CharSet.Auto)]
public static extern int Lambert72ToGeoETRS89(double Xi, double Yi, double Zi, ref double xo, ref double yo, ref double Ho);
#endregion
}
}

View File

@@ -1,10 +1,4 @@
using System; namespace GpsClient2 {
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2 {
public enum CoordinateType { public enum CoordinateType {
Latitude, Latitude,
Longitude Longitude

View File

@@ -1,13 +1,9 @@
using System; using GpsClient2.Model;
using GpsClient2.Model;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Device.Location;
using GpsClient2.NmeaMessages; using GpsClient2.NmeaMessages;
using System;
using System.Device.Location;
namespace GpsClient2 { namespace GpsClient2.EventArguments {
public class GpsDataEventArgs : EventArgs { public class GpsDataEventArgs : EventArgs {
public GpsCoordinateSystem CoordinateSystem { get; set; } = GpsCoordinateSystem.GeoEtrs89; public GpsCoordinateSystem CoordinateSystem { get; set; } = GpsCoordinateSystem.GeoEtrs89;
@@ -15,6 +11,7 @@ namespace GpsClient2 {
public double Longitude { get; set; } public double Longitude { get; set; }
public double Speed { get; set; } public double Speed { get; set; }
public double Course { get; set; }
public GpsDataEventArgs(GpsLocation gpsLocation) { public GpsDataEventArgs(GpsLocation gpsLocation) {
Latitude = gpsLocation.Latitude; Latitude = gpsLocation.Latitude;
@@ -26,6 +23,7 @@ namespace GpsClient2 {
Latitude = gpsLocation.Latitude; Latitude = gpsLocation.Latitude;
Longitude = gpsLocation.Longitude; Longitude = gpsLocation.Longitude;
Speed = gpsLocation.Speed; Speed = gpsLocation.Speed;
Course = gpsLocation.Course;
} }
public GpsDataEventArgs(GeoCoordinate gpsLocation) { public GpsDataEventArgs(GeoCoordinate gpsLocation) {

View File

@@ -0,0 +1,15 @@
using System;
namespace GpsClient2.EventArguments {
public class GpsStatusEventArgs : EventArgs {
public GpsStatus Status { get; set; }
public GpsStatusEventArgs() {
}
public GpsStatusEventArgs(GpsStatus status) {
Status = status;
}
}
}

View File

@@ -1,8 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2.Exceptions { namespace GpsClient2.Exceptions {
public class UnknownTypeException : Exception { public class UnknownTypeException : Exception {

View File

@@ -44,8 +44,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="BaseGpsClient.cs" /> <Compile Include="BaseGpsClient.cs" />
<Compile Include="CoordinateConverterUtilities.cs" />
<Compile Include="Enums.cs" /> <Compile Include="Enums.cs" />
<Compile Include="EventArguments\GpsStatusEventArgs.cs" />
<Compile Include="Exceptions\UnknownTypeException.cs" /> <Compile Include="Exceptions\UnknownTypeException.cs" />
<Compile Include="Model\BaseGpsInfo.cs" /> <Compile Include="Model\BaseGpsInfo.cs" />
<Compile Include="Model\ComPortInfo.cs" /> <Compile Include="Model\ComPortInfo.cs" />
@@ -58,19 +58,11 @@
<Compile Include="NmeaParser.cs" /> <Compile Include="NmeaParser.cs" />
<Compile Include="StringExtensions.cs" /> <Compile Include="StringExtensions.cs" />
<Compile Include="ComPortGpsClient.cs" /> <Compile Include="ComPortGpsClient.cs" />
<Compile Include="GpsDataEventArgs.cs" /> <Compile Include="EventArguments\GpsDataEventArgs.cs" />
<Compile Include="Model\GpsLocation.cs" /> <Compile Include="Model\GpsLocation.cs" />
<Compile Include="NmeaMessages\GprmcMessage.cs" /> <Compile Include="NmeaMessages\GprmcMessage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WindowsLocationApiGpsClient.cs" /> <Compile Include="WindowsLocationApiGpsClient.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="plugins\ETRS89_LAMBERT_UTM_32bits.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="plugins\ETRS89_LAMBERT_UTM_64bits.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

14
GpsClient2.nuspec Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>GpsClient2</id>
<version>1.0.8</version>
<title>GpsClient2</title>
<authors>Russ Kollmansberger</authors>
<owners>Russ Kollmansberger</owners>
<description>Package that allows users to receive and process GPS NMEA sentences or utilize Windows Location Services to report the device's current location.</description>
<releaseNotes>Removed reference to Lambert Coordinate conversion</releaseNotes>
<tags>NuGet,Package,Helper</tags>
<license type="expression">MIT</license>
</metadata>
</package>

View File

@@ -1,13 +1,7 @@
using System; namespace GpsClient2.Model {
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2.Model {
public abstract class BaseGpsInfo { public abstract class BaseGpsInfo {
public GpsCoordinateSystem CoordinateSystem { get; set; } = GpsCoordinateSystem.GeoEtrs89; public GpsCoordinateSystem CoordinateSystem { get; set; } = GpsCoordinateSystem.GeoEtrs89;
public int ReadFrequenty { get; set; } public int ReadFrequency { get; set; }
} }
} }

View File

@@ -1,20 +1,16 @@
using System; namespace GpsClient2.Model {
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2.Model {
public class ComPortInfo : BaseGpsInfo { public class ComPortInfo : BaseGpsInfo {
public string ComPort { get; set; } = "ComPort1"; public string ComPort { get; set; } = "ComPort1";
public int BaudRate { get; set; } = 9600;
public ComPortInfo() { public ComPortInfo() {
ReadFrequenty = 1000; ReadFrequency = 1000;
} }
public ComPortInfo(string comPort, int readFrequenty = 1000) { public ComPortInfo(string comPort, int baudRate = 9600, int readFrequency = 1000) {
ComPort = comPort; ComPort = comPort;
ReadFrequenty = readFrequenty; BaudRate = baudRate;
ReadFrequency = readFrequency;
} }
} }
} }

View File

@@ -1,10 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace GpsClient2.Model { namespace GpsClient2.Model {
[DataContract] [DataContract]

View File

@@ -1,15 +1,9 @@
using System; namespace GpsClient2.Model {
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2.Model {
public class WindowsLocationApiInfo : BaseGpsInfo { public class WindowsLocationApiInfo : BaseGpsInfo {
public int Timeout { get; set; } = 1000; public int Timeout { get; set; } = 1000;
public WindowsLocationApiInfo() { public WindowsLocationApiInfo() {
ReadFrequenty = 0; ReadFrequency = 0;
} }
} }
} }

View File

@@ -2,9 +2,6 @@
using GpsClient2.NmeaMessages; using GpsClient2.NmeaMessages;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2 { namespace GpsClient2 {
public static class NmeaConstants { public static class NmeaConstants {

View File

@@ -1,8 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2.NmeaMessages { namespace GpsClient2.NmeaMessages {
public class GpggaMessage : NmeaMessage { public class GpggaMessage : NmeaMessage {

View File

@@ -1,8 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2.NmeaMessages { namespace GpsClient2.NmeaMessages {
public class GpgsaMessage : NmeaMessage { public class GpgsaMessage : NmeaMessage {

View File

@@ -1,9 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2.NmeaMessages { namespace GpsClient2.NmeaMessages {
public class GprmcMessage : NmeaMessage { public class GprmcMessage : NmeaMessage {

View File

@@ -1,8 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2.NmeaMessages { namespace GpsClient2.NmeaMessages {
public class GpvtgMessage : NmeaMessage { public class GpvtgMessage : NmeaMessage {

View File

@@ -1,10 +1,4 @@
using System; namespace GpsClient2.NmeaMessages {
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2.NmeaMessages {
public abstract class NmeaMessage { public abstract class NmeaMessage {
public abstract void Parse(string[] messageParts); public abstract void Parse(string[] messageParts);
} }

View File

@@ -1,9 +1,5 @@
using GpsClient2.NmeaMessages; using GpsClient2.NmeaMessages;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2 { namespace GpsClient2 {
public class NmeaParser { public class NmeaParser {

View File

@@ -1,5 +1,4 @@
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following // General Information about an assembly is controlled through the following

View File

@@ -1,10 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace GpsClient2 { namespace GpsClient2 {
public static class StringExtensions { public static class StringExtensions {

View File

@@ -1,10 +1,7 @@
using GpsClient2.Model; using GpsClient2.EventArguments;
using GpsClient2.Model;
using System; using System;
using System.Collections.Generic;
using System.Device.Location; using System.Device.Location;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsClient2 { namespace GpsClient2 {
public class WindowsLocationApiGpsClient : BaseGpsClient { public class WindowsLocationApiGpsClient : BaseGpsClient {
@@ -67,7 +64,7 @@ namespace GpsClient2 {
} }
private void WatcherOnPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e) { private void WatcherOnPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e) {
if (_previousReadTime != null && GpsInfo.ReadFrequenty != 0 && e.Position.Timestamp.Subtract(new TimeSpan(0, 0, 0, 0, GpsInfo.ReadFrequenty)) <= _previousReadTime) return; if (_previousReadTime != null && GpsInfo.ReadFrequency != 0 && e.Position.Timestamp.Subtract(new TimeSpan(0, 0, 0, 0, GpsInfo.ReadFrequency)) <= _previousReadTime) return;
OnGpsDataReceived(new GpsDataEventArgs(e.Position.Location)); OnGpsDataReceived(new GpsDataEventArgs(e.Position.Location));
_previousReadTime = e.Position.Timestamp; _previousReadTime = e.Position.Timestamp;
} }