Added support for WinDesktop

This commit is contained in:
mort5161 2014-07-25 11:25:18 -07:00
parent 254da8a14c
commit c5150bba41
26 changed files with 410 additions and 188 deletions

View file

@ -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<bool> 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<Stream> OpenStreamAsync();
public async Task CloseAsync()
{
if (tcs != null)
{
closeTask = new TaskCompletionSource<bool>();
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<NmeaDevice, Nmea.NmeaMessage> 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;
}
}
}
}

View file

@ -6,19 +6,19 @@
<SharedGUID>e15edbd9-0356-422b-8c29-18833787356e</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>NmeaParser.Shared</Import_RootNamespace>
<Import_RootNamespace>NmeaParser</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Nmea\Device.cs" />
<Compile Include="$(MSBuildThisFileDirectory)NmeaDevice.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Nmea\Gps\GPGGA.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Nmea\Gps\GPRMC.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Nmea\LaserRangeMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Nmea\LaserTech\LaserRange\PLTIT.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Nmea\NmeaDevice.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Nmea\NmeaMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Nmea\Trimble\LaserRange\PTNLA.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Nmea\Trimble\LaserRange\PTNLB.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Nmea\UnknownMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)StreamDevice.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Nmea\Gps\Garmin\" />

View file

@ -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<Stream> 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;
}
}
}

View file

@ -136,9 +136,9 @@
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NmeaParser\NmeaParser.WinStore.csproj">
<ProjectReference Include="..\NmeaParser.WinStore\NmeaParser.WinStore.csproj">
<Project>{62a55887-10f5-40d2-9352-96246d1b11d3}</Project>
<Name>BTDevices.WinStore</Name>
<Name>NmeaParser.WinStore</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '12.0' ">

View file

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NmeaParser</RootNamespace>
<AssemblyName>NmeaParser.WinDesktop</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\Bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\Bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="SerialPortDevice.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TypedEventHandler.cs" />
</ItemGroup>
<Import Project="..\NmeaParser.Shared\NmeaParser.Shared.projitems" Label="Shared" Condition="Exists('..\NmeaParser.Shared\NmeaParser.Shared.projitems')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View file

@ -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")]

View file

@ -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<System.IO.Stream> OpenStreamAsync()
{
m_port.Open();
return Task.FromResult<System.IO.Stream>(m_port.BaseStream);
}
protected override Task CloseStreamAsync(System.IO.Stream stream)
{
m_port.Close();
return Task.FromResult(true);
}
}
}

View file

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Windows.Foundation
{
/// <summary>
/// Represents a method that handles general events.
/// </summary>
/// <param name="sender">The object type.</param>
/// <param name="args">The type of event data generated by the event.</param>
public delegate void TypedEventHandler<TSender, TResult>(TSender sender, TResult args);
}

View file

@ -23,7 +23,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>BinWP\Debug\</OutputPath>
<OutputPath>..\Bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;SILVERLIGHT;WINDOWS_PHONE</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
@ -33,7 +33,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>BinWP\Release\</OutputPath>
<OutputPath>..\Bin\Release\</OutputPath>
<DefineConstants>TRACE;SILVERLIGHT;WINDOWS_PHONE</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
@ -41,10 +41,13 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\NmeaParser.WinStore\BluetoothDevice.cs">
<Link>BluetoothDevice.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="NmeaParser.Shared\NmeaParser.Shared.projitems" Label="Shared" Condition="Exists('NmeaParser.Shared\NmeaParser.Shared.projitems')" />
<Import Project="..\NmeaParser.Shared\NmeaParser.Shared.projitems" Label="Shared" Condition="Exists('..\NmeaParser.Shared\NmeaParser.Shared.projitems')" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).$(TargetFrameworkVersion).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).CSharp.targets" />
<ProjectExtensions />

View file

@ -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<System.IO.Stream> 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);
}
}
}

View file

@ -21,7 +21,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>binWS\Debug\</OutputPath>
<OutputPath>..\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
@ -29,7 +29,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>binWS\Release\</OutputPath>
<OutputPath>..\bin\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
@ -38,12 +38,13 @@
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
</ItemGroup>
<ItemGroup>
<Compile Include="BluetoothDevice.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '12.0' ">
<VisualStudioVersion>12.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="NmeaParser.Shared\NmeaParser.Shared.projitems" Label="Shared" Condition="Exists('NmeaParser.Shared\NmeaParser.Shared.projitems')" />
<Import Project="..\NmeaParser.Shared\NmeaParser.Shared.projitems" Label="Shared" Condition="Exists('..\NmeaParser.Shared\NmeaParser.Shared.projitems')" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View file

@ -0,0 +1,29 @@
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")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NmeaParser")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 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")]
[assembly: ComVisible(false)]

View file

@ -7,19 +7,24 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WinPhone", "WinPhone", "{26
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WinStore", "WinStore", "{07131E3E-1C4E-41CB-BD14-7950AA858A23}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NmeaParser.WinStore", "NmeaParser\NmeaParser.WinStore.csproj", "{62A55887-10F5-40D2-9352-96246D1B11D3}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NmeaParser.WinStore", "NmeaParser.WinStore\NmeaParser.WinStore.csproj", "{62A55887-10F5-40D2-9352-96246D1B11D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NmeaParser.Tests", "NmeaParser.Tests\NmeaParser.Tests.csproj", "{5B5BAF9D-3FB9-47F9-AE07-B8CC43EC887C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NmeaParser.WinPhone", "NmeaParser\NmeaParser.WinPhone.csproj", "{EA42A713-BC6E-4914-B54B-47C0891B7421}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NmeaParser.WinPhone", "NmeaParser.WinPhone\NmeaParser.WinPhone.csproj", "{EA42A713-BC6E-4914-B54B-47C0891B7421}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "NmeaParser.Shared", "NmeaParser\NmeaParser.Shared\NmeaParser.Shared.shproj", "{E15EDBD9-0356-422B-8C29-18833787356E}"
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "NmeaParser.Shared", "NmeaParser.Shared\NmeaParser.Shared.shproj", "{E15EDBD9-0356-422B-8C29-18833787356E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WinDesktop", "WinDesktop", "{AFA7C5B0-1A9C-4C85-880D-C6AF2C061961}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NmeaParser.WinDesktop", "NmeaParser.WinDesktop\NmeaParser.WinDesktop.csproj", "{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
NmeaParser\NmeaParser.Shared\NmeaParser.Shared.projitems*{62a55887-10f5-40d2-9352-96246d1b11d3}*SharedItemsImports = 4
NmeaParser\NmeaParser.Shared\NmeaParser.Shared.projitems*{e15edbd9-0356-422b-8c29-18833787356e}*SharedItemsImports = 13
NmeaParser\NmeaParser.Shared\NmeaParser.Shared.projitems*{ea42a713-bc6e-4914-b54b-47c0891b7421}*SharedItemsImports = 4
NmeaParser.Shared\NmeaParser.Shared.projitems*{df711ab9-f14e-4f1f-b8f2-b6ddc4691554}*SharedItemsImports = 4
NmeaParser.Shared\NmeaParser.Shared.projitems*{62a55887-10f5-40d2-9352-96246d1b11d3}*SharedItemsImports = 4
NmeaParser.Shared\NmeaParser.Shared.projitems*{e15edbd9-0356-422b-8c29-18833787356e}*SharedItemsImports = 13
NmeaParser.Shared\NmeaParser.Shared.projitems*{ea42a713-bc6e-4914-b54b-47c0891b7421}*SharedItemsImports = 4
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -86,6 +91,16 @@ Global
{EA42A713-BC6E-4914-B54B-47C0891B7421}.Release|x64.ActiveCfg = Release|Any CPU
{EA42A713-BC6E-4914-B54B-47C0891B7421}.Release|x86.ActiveCfg = Release|Any CPU
{EA42A713-BC6E-4914-B54B-47C0891B7421}.Release|x86.Build.0 = Release|Any CPU
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}.Debug|ARM.ActiveCfg = Debug|Any CPU
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}.Debug|x64.ActiveCfg = Debug|Any CPU
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}.Debug|x86.ActiveCfg = Debug|Any CPU
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}.Release|Any CPU.Build.0 = Release|Any CPU
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}.Release|ARM.ActiveCfg = Release|Any CPU
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}.Release|x64.ActiveCfg = Release|Any CPU
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -94,5 +109,6 @@ Global
{62A55887-10F5-40D2-9352-96246D1B11D3} = {07131E3E-1C4E-41CB-BD14-7950AA858A23}
{5B5BAF9D-3FB9-47F9-AE07-B8CC43EC887C} = {07131E3E-1C4E-41CB-BD14-7950AA858A23}
{EA42A713-BC6E-4914-B54B-47C0891B7421} = {26A0F6A9-4B11-46F4-BB01-50D37D1C3CB4}
{DF711AB9-F14E-4F1F-B8F2-B6DDC4691554} = {AFA7C5B0-1A9C-4C85-880D-C6AF2C061961}
EndGlobalSection
EndGlobal

View file

@ -1,116 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Windows.Networking.Sockets;
#if NETFX_CORE
using BTDevice = Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceService;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Networking.Sockets;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
#else
using BTDevice = Windows.Networking.Proximity.PeerInformation;
#endif
namespace NmeaParser
{
public abstract class Device : IDisposable
{
private BTDevice m_device;
private StreamSocket socket;
System.Threading.CancellationTokenSource tcs;
public Device(BTDevice device)
{
if (device == null)
throw new ArgumentNullException("device");
#if NETFX_CORE
if (device.ServiceId.Uuid != RfcommServiceId.SerialPort.Uuid)
throw new NotSupportedException("Only SerialPort devices supported");
#endif
m_device = device;
}
TaskCompletionSource<bool> closeTask;
public async Task StartAsync()
{
if (tcs != null)
return;
if (m_device == null)
throw new ObjectDisposedException("Device");
tcs = new System.Threading.CancellationTokenSource();
socket = new StreamSocket();
await socket.ConnectAsync(
#if NETFX_CORE
m_device.ConnectionHostName,
m_device.ConnectionServiceName);
#else
m_device.HostName, "1");
//socket = await Windows.Networking.Proximity.PeerFinder.ConnectAsync(m_device.HostName;
#endif
if (tcs.IsCancellationRequested) //Stop was called while opening device
{
socket.Dispose();
socket = null;
throw new TaskCanceledException();
} var token = tcs.Token;
var _ = Task.Run(async () =>
{
var stream = socket.InputStream.AsStreamForRead();
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(socket != null)
socket.Dispose();
if (closeTask != null)
closeTask.SetResult(true);
});
}
public Task StopAsync()
{
if (tcs != null)
{
socket.Dispose();
socket = null;
closeTask = new TaskCompletionSource<bool>();
if(tcs != null)
tcs.Cancel();
tcs = null;
return closeTask.Task;
}
return Task.FromResult(false);
}
protected abstract void OnData(byte[] data);
public void Dispose()
{
if (tcs != null)
{
tcs.Cancel();
tcs = null;
}
m_device = null;
if(socket != null)
socket.Dispose();
socket = null;
}
}
}

View file

@ -1,55 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if NETFX_CORE
using BTDevice = Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceService;
using Windows.Devices.Bluetooth.Rfcomm;
#else
using BTDevice = Windows.Networking.Proximity.PeerInformation;
#endif
using Windows.Foundation;
namespace NmeaParser
{
public class NmeaDevice : Device
{
private string message = "";
public NmeaDevice(BTDevice device)
: base(device)
{
}
protected override 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<NmeaDevice, Nmea.NmeaMessage> MessageReceived;
}
}