diff --git a/src/NmeaParser/NmeaParser.Shared/Nmea/Gps/GPGGA.cs b/src/NmeaParser.Shared/Nmea/Gps/GPGGA.cs similarity index 100% rename from src/NmeaParser/NmeaParser.Shared/Nmea/Gps/GPGGA.cs rename to src/NmeaParser.Shared/Nmea/Gps/GPGGA.cs diff --git a/src/NmeaParser/NmeaParser.Shared/Nmea/Gps/GPRMC.cs b/src/NmeaParser.Shared/Nmea/Gps/GPRMC.cs similarity index 100% rename from src/NmeaParser/NmeaParser.Shared/Nmea/Gps/GPRMC.cs rename to src/NmeaParser.Shared/Nmea/Gps/GPRMC.cs diff --git a/src/NmeaParser/NmeaParser.Shared/Nmea/Gps/Garmin/PGRME.cs b/src/NmeaParser.Shared/Nmea/Gps/Garmin/PGRME.cs similarity index 100% rename from src/NmeaParser/NmeaParser.Shared/Nmea/Gps/Garmin/PGRME.cs rename to src/NmeaParser.Shared/Nmea/Gps/Garmin/PGRME.cs diff --git a/src/NmeaParser/NmeaParser.Shared/Nmea/LaserRangeMessage.cs b/src/NmeaParser.Shared/Nmea/LaserRangeMessage.cs similarity index 100% rename from src/NmeaParser/NmeaParser.Shared/Nmea/LaserRangeMessage.cs rename to src/NmeaParser.Shared/Nmea/LaserRangeMessage.cs diff --git a/src/NmeaParser/NmeaParser.Shared/Nmea/LaserTech/LaserRange/PLTIT.cs b/src/NmeaParser.Shared/Nmea/LaserTech/LaserRange/PLTIT.cs similarity index 100% rename from src/NmeaParser/NmeaParser.Shared/Nmea/LaserTech/LaserRange/PLTIT.cs rename to src/NmeaParser.Shared/Nmea/LaserTech/LaserRange/PLTIT.cs diff --git a/src/NmeaParser/NmeaParser.Shared/Nmea/NmeaMessage.cs b/src/NmeaParser.Shared/Nmea/NmeaMessage.cs similarity index 100% rename from src/NmeaParser/NmeaParser.Shared/Nmea/NmeaMessage.cs rename to src/NmeaParser.Shared/Nmea/NmeaMessage.cs diff --git a/src/NmeaParser/NmeaParser.Shared/Nmea/Trimble/LaserRange/PTNLA.cs b/src/NmeaParser.Shared/Nmea/Trimble/LaserRange/PTNLA.cs similarity index 100% rename from src/NmeaParser/NmeaParser.Shared/Nmea/Trimble/LaserRange/PTNLA.cs rename to src/NmeaParser.Shared/Nmea/Trimble/LaserRange/PTNLA.cs diff --git a/src/NmeaParser/NmeaParser.Shared/Nmea/Trimble/LaserRange/PTNLB.cs b/src/NmeaParser.Shared/Nmea/Trimble/LaserRange/PTNLB.cs similarity index 100% rename from src/NmeaParser/NmeaParser.Shared/Nmea/Trimble/LaserRange/PTNLB.cs rename to src/NmeaParser.Shared/Nmea/Trimble/LaserRange/PTNLB.cs diff --git a/src/NmeaParser/NmeaParser.Shared/Nmea/UnknownMessage.cs b/src/NmeaParser.Shared/Nmea/UnknownMessage.cs similarity index 100% rename from src/NmeaParser/NmeaParser.Shared/Nmea/UnknownMessage.cs rename to src/NmeaParser.Shared/Nmea/UnknownMessage.cs diff --git a/src/NmeaParser.Shared/NmeaDevice.cs b/src/NmeaParser.Shared/NmeaDevice.cs new file mode 100644 index 0000000..d7e84cf --- /dev/null +++ b/src/NmeaParser.Shared/NmeaDevice.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using Windows.Foundation; + +namespace NmeaParser +{ + public abstract class NmeaDevice : IDisposable + { + private string message = ""; + private Stream m_stream; + System.Threading.CancellationTokenSource tcs; + TaskCompletionSource closeTask; + + protected NmeaDevice() + { + } + public async Task OpenAsync() + { + tcs = new System.Threading.CancellationTokenSource(); + m_stream = await OpenStreamAsync(); + StartParser(); + } + + private void StartParser() + { + var token = tcs.Token; + var _ = Task.Run(async () => + { + var stream = m_stream; + byte[] buffer = new byte[1024]; + while (!token.IsCancellationRequested) + { + int readCount = 0; + try + { + readCount = await stream.ReadAsync(buffer, 0, 1024, token).ConfigureAwait(false); + } + catch { } + if (token.IsCancellationRequested) + break; + if (readCount > 0) + { + OnData(buffer.Take(readCount).ToArray()); + } + await Task.Delay(10, token); + } + if (closeTask != null) + closeTask.SetResult(true); + }); + } + + protected abstract Task OpenStreamAsync(); + public async Task CloseAsync() + { + if (tcs != null) + { + closeTask = new TaskCompletionSource(); + if (tcs != null) + tcs.Cancel(); + tcs = null; + } + await closeTask.Task; + await CloseStreamAsync(m_stream); + m_stream = null; + } + protected abstract Task CloseStreamAsync(Stream stream); + + private void OnData(byte[] data) + { + var nmea = System.Text.Encoding.UTF8.GetString(data, 0, data.Length); + message += nmea; + var lineEnd = message.IndexOf("\n"); + if (lineEnd > -1) + { + string line = message.Substring(0, lineEnd); + message = message.Substring(lineEnd).Trim(); + ProcessMessage(line.Trim()); + } + } + + private void ProcessMessage(string p) + { + try + { + var msg = NmeaParser.Nmea.NmeaMessage.Parse(p); + if (msg != null) + OnMessageReceived(msg); + } + catch { } + } + + private void OnMessageReceived(Nmea.NmeaMessage msg) + { + if (MessageReceived != null) + MessageReceived(this, msg); + } + + public event TypedEventHandler MessageReceived; + + public void Dispose() + { + Dispose(true); + } + protected virtual void Dispose(bool force) + { + if (m_stream != null) + { + if (tcs != null) + { + tcs.Cancel(); + tcs = null; + } + CloseStreamAsync(m_stream); + m_stream = null; + } + } + } +} diff --git a/src/NmeaParser/NmeaParser.Shared/NmeaParser.Shared.projitems b/src/NmeaParser.Shared/NmeaParser.Shared.projitems similarity index 85% rename from src/NmeaParser/NmeaParser.Shared/NmeaParser.Shared.projitems rename to src/NmeaParser.Shared/NmeaParser.Shared.projitems index be8ea6a..5fd6b87 100644 --- a/src/NmeaParser/NmeaParser.Shared/NmeaParser.Shared.projitems +++ b/src/NmeaParser.Shared/NmeaParser.Shared.projitems @@ -6,19 +6,19 @@ e15edbd9-0356-422b-8c29-18833787356e - NmeaParser.Shared + NmeaParser - + - + diff --git a/src/NmeaParser/NmeaParser.Shared/NmeaParser.Shared.shproj b/src/NmeaParser.Shared/NmeaParser.Shared.shproj similarity index 100% rename from src/NmeaParser/NmeaParser.Shared/NmeaParser.Shared.shproj rename to src/NmeaParser.Shared/NmeaParser.Shared.shproj diff --git a/src/NmeaParser.Shared/StreamDevice.cs b/src/NmeaParser.Shared/StreamDevice.cs new file mode 100644 index 0000000..70fa586 --- /dev/null +++ b/src/NmeaParser.Shared/StreamDevice.cs @@ -0,0 +1,34 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace NmeaParser +{ + public class StreamDevice : NmeaDevice + { + System.IO.Stream m_stream; + public StreamDevice(Stream stream) : base() + { + m_stream = stream; + } + + protected override Task OpenStreamAsync() + { + return Task.FromResult(m_stream); + } + + protected override Task CloseStreamAsync(System.IO.Stream stream) + { + return Task.FromResult(true); //do nothing + } + + protected override void Dispose(bool force) + { + if (m_stream != null) + m_stream.Dispose(); + m_stream = null; + } + } +} diff --git a/src/NmeaParser.Tests/NmeaParser.Tests.csproj b/src/NmeaParser.Tests/NmeaParser.Tests.csproj index fc1c114..180dd32 100644 --- a/src/NmeaParser.Tests/NmeaParser.Tests.csproj +++ b/src/NmeaParser.Tests/NmeaParser.Tests.csproj @@ -136,9 +136,9 @@ - + {62a55887-10f5-40d2-9352-96246d1b11d3} - BTDevices.WinStore + NmeaParser.WinStore diff --git a/src/NmeaParser.WinDesktop/NmeaParser.WinDesktop.csproj b/src/NmeaParser.WinDesktop/NmeaParser.WinDesktop.csproj new file mode 100644 index 0000000..a1619bc --- /dev/null +++ b/src/NmeaParser.WinDesktop/NmeaParser.WinDesktop.csproj @@ -0,0 +1,55 @@ + + + + + Debug + AnyCPU + {DF711AB9-F14E-4F1F-B8F2-B6DDC4691554} + Library + Properties + NmeaParser + NmeaParser.WinDesktop + v4.5 + 512 + + + true + full + false + ..\Bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + ..\Bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/NmeaParser.WinDesktop/Properties/AssemblyInfo.cs b/src/NmeaParser.WinDesktop/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ae0c045 --- /dev/null +++ b/src/NmeaParser.WinDesktop/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("NmeaParser.WinDesktop")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("NmeaParser.WinDesktop")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("c96fd2bc-67e6-45fd-a57e-1bb8088bdc43")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/NmeaParser.WinDesktop/SerialPortDevice.cs b/src/NmeaParser.WinDesktop/SerialPortDevice.cs new file mode 100644 index 0000000..6d7074f --- /dev/null +++ b/src/NmeaParser.WinDesktop/SerialPortDevice.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using System.Threading.Tasks; + +namespace NmeaParser +{ + public class SerialPortDevice : NmeaDevice + { + private System.IO.Ports.SerialPort m_port; + + public SerialPortDevice(System.IO.Ports.SerialPort port) + { + m_port = port; + } + + protected override Task OpenStreamAsync() + { + m_port.Open(); + return Task.FromResult(m_port.BaseStream); + } + + protected override Task CloseStreamAsync(System.IO.Stream stream) + { + m_port.Close(); + return Task.FromResult(true); + } + } +} diff --git a/src/NmeaParser.WinDesktop/TypedEventHandler.cs b/src/NmeaParser.WinDesktop/TypedEventHandler.cs new file mode 100644 index 0000000..d7585d2 --- /dev/null +++ b/src/NmeaParser.WinDesktop/TypedEventHandler.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Windows.Foundation +{ + /// + /// Represents a method that handles general events. + /// + /// The object type. + /// The type of event data generated by the event. + public delegate void TypedEventHandler(TSender sender, TResult args); +} diff --git a/src/NmeaParser/NmeaParser.WinPhone.csproj b/src/NmeaParser.WinPhone/NmeaParser.WinPhone.csproj similarity index 88% rename from src/NmeaParser/NmeaParser.WinPhone.csproj rename to src/NmeaParser.WinPhone/NmeaParser.WinPhone.csproj index 42599ed..b20d139 100644 --- a/src/NmeaParser/NmeaParser.WinPhone.csproj +++ b/src/NmeaParser.WinPhone/NmeaParser.WinPhone.csproj @@ -23,7 +23,7 @@ true full false - BinWP\Debug\ + ..\Bin\Debug\ DEBUG;TRACE;SILVERLIGHT;WINDOWS_PHONE true true @@ -33,7 +33,7 @@ pdbonly true - BinWP\Release\ + ..\Bin\Release\ TRACE;SILVERLIGHT;WINDOWS_PHONE true true @@ -41,10 +41,13 @@ 4 + + BluetoothDevice.cs + - + diff --git a/src/NmeaParser/Properties/AssemblyInfo.cs b/src/NmeaParser.WinPhone/Properties/AssemblyInfo.cs similarity index 100% rename from src/NmeaParser/Properties/AssemblyInfo.cs rename to src/NmeaParser.WinPhone/Properties/AssemblyInfo.cs diff --git a/src/NmeaParser.WinStore/BluetoothDevice.cs b/src/NmeaParser.WinStore/BluetoothDevice.cs new file mode 100644 index 0000000..5d04710 --- /dev/null +++ b/src/NmeaParser.WinStore/BluetoothDevice.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using System.Threading.Tasks; +using Windows.Networking.Sockets; +#if NETFX_CORE +using BTDevice = Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceService; +using Windows.Devices.Bluetooth.Rfcomm; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Foundation; +#else +using BTDevice = Windows.Networking.Proximity.PeerInformation; +#endif + +namespace NmeaParser +{ + public class BluetoothDevice : NmeaDevice + { + private BTDevice m_device; + private StreamSocket m_socket; + + public BluetoothDevice(BTDevice device) + { + m_device = device; + } + + protected override async Task OpenStreamAsync() + { + var socket = new Windows.Networking.Sockets.StreamSocket(); + await socket.ConnectAsync( +#if NETFX_CORE + m_device.ConnectionHostName, + m_device.ConnectionServiceName); +#else + m_device.HostName, "1"); +#endif + m_socket = socket; + return socket.InputStream.AsStreamForRead(); + } + + protected override Task CloseStreamAsync(System.IO.Stream stream) + { + stream.Dispose(); + m_socket.Dispose(); + m_socket = null; + return Task.FromResult(true); + } + } +} diff --git a/src/NmeaParser/NmeaParser.WinStore.csproj b/src/NmeaParser.WinStore/NmeaParser.WinStore.csproj similarity index 89% rename from src/NmeaParser/NmeaParser.WinStore.csproj rename to src/NmeaParser.WinStore/NmeaParser.WinStore.csproj index f4c22e2..64e2d3e 100644 --- a/src/NmeaParser/NmeaParser.WinStore.csproj +++ b/src/NmeaParser.WinStore/NmeaParser.WinStore.csproj @@ -21,7 +21,7 @@ true full false - binWS\Debug\ + ..\bin\Debug\ DEBUG;TRACE;NETFX_CORE prompt 4 @@ -29,7 +29,7 @@ pdbonly true - binWS\Release\ + ..\bin\Release\ TRACE;NETFX_CORE prompt 4 @@ -38,12 +38,13 @@ + 12.0 - +