diff --git a/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs b/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs new file mode 100644 index 0000000..971e10d --- /dev/null +++ b/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs @@ -0,0 +1,170 @@ +using System; +using System.Runtime.InteropServices.WindowsRuntime; +using System.Threading; +using System.Threading.Tasks; +using OBD.NET.Communication.EventArgs; +using Windows.Devices.Bluetooth.Rfcomm; +using Windows.Networking.Sockets; +using Windows.Storage.Streams; +using System.Linq; + +namespace OBD.NET.Communication +{ + /// + /// Bluetooth OBD serial implementation + /// + /// + public class BluetoothSerialConnection : ISerialConnection + { + + private StreamSocket _socket; + private DataWriter _writer; + + private readonly byte[] _readBuffer = new byte[1024]; + + private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); + private Task _readerTask; + + private string device; + + /// + /// Initializes a new instance of the class. + /// + public BluetoothSerialConnection() + { + device = null; + } + + /// + /// Initializes a new instance of the class. + /// + /// Name of the device. + /// The logger. + public BluetoothSerialConnection(string deviceName) + { + device = deviceName; + } + + + /// + /// Gets a value indicating whether this instance is open. + /// + /// + /// true if this instance is open; otherwise, false. + /// + public bool IsOpen { get; private set; } + + /// + /// Gets a value indicating whether this instance uses asynchronous IO + /// + /// + /// Has to be set to true if asynchronous IO is supported. + /// If true async methods have to be implemented + /// + public bool IsAsync => true; + + /// + /// Occurs when a full line was received + /// + public event EventHandler DataReceived; + + /// + /// Connects the serial port. + /// + /// Synchronous operations not supported + public void Connect() + { + throw new NotSupportedException("Synchronous operations not supported on UWP platform"); + } + + /// + /// Connects the serial port asynchronously + /// + /// + public async Task ConnectAsync() + { + var services = await Windows.Devices.Enumeration.DeviceInformation + .FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort)); + + //use first serial service + if (services.Count > 0) + { + var id = services[0].Id; + + //use predefined device from constructor + if (!string.IsNullOrWhiteSpace(device)) + { + id = services.Where(x => x.Name.Equals(device, StringComparison.OrdinalIgnoreCase)) + .Select(x => x.Id).FirstOrDefault(); + + if (id == null) + { + throw new InvalidOperationException($"Device {device} not found"); + } + } + + // Initialize the target Bluetooth device + var service = await RfcommDeviceService.FromIdAsync(id); + + // Check that the service meets this App's minimum requirement + + _socket = new StreamSocket(); + await _socket.ConnectAsync(service.ConnectionHostName, + service.ConnectionServiceName); + _writer = new DataWriter(_socket.OutputStream); + _readerTask = StartReader(); + IsOpen = true; + } + } + + /// + /// Writes the specified text to the serial connection + /// + /// The text. + /// Synchronous operations not supported + public void Write(byte[] data) + { + throw new NotImplementedException("Synchronous operations not supported on UWP platform"); + } + + /// + /// Writes the specified text to the serial connection asynchronously + /// + /// The text. + /// + public async Task WriteAsync(byte[] data) + { + _writer.WriteBytes(data); + await _writer.StoreAsync(); + await _writer.FlushAsync(); + } + + private Task StartReader() + { + return Task.Factory.StartNew(async () => + { + + var buffer = _readBuffer.AsBuffer(); + while (!_cancellationTokenSource.IsCancellationRequested) + { + var readData = await _socket.InputStream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.Partial); + SerialPortOnDataReceived(readData); + } + }, _cancellationTokenSource.Token); + } + + private void SerialPortOnDataReceived(IBuffer buffer) + { + DataReceived?.Invoke(this, new DataReceivedEventArgs((int)buffer.Length, _readBuffer)); + } + + + public void Dispose() + { + _cancellationTokenSource?.Cancel(); + _readerTask?.Wait(); + _socket?.Dispose(); + } + + } +} diff --git a/OBD.NET/OBD.NET.Universal/OBD.NET.Universal.csproj b/OBD.NET/OBD.NET.Universal/OBD.NET.Universal.csproj new file mode 100644 index 0000000..cc044c5 --- /dev/null +++ b/OBD.NET/OBD.NET.Universal/OBD.NET.Universal.csproj @@ -0,0 +1,138 @@ + + + + + Debug + AnyCPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73} + Library + Properties + OBD.NET.Universal + OBD.NET.Universal + en-US + UAP + 10.0.22000.0 + 10.0.19041.0 + 14 + 512 + {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + prompt + 4 + + + x86 + true + bin\x86\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x86 + false + prompt + + + x86 + bin\x86\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x86 + false + prompt + + + ARM + true + bin\ARM\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM + false + prompt + + + ARM + bin\ARM\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM + false + prompt + + + x64 + true + bin\x64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x64 + false + prompt + + + x64 + bin\x64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x64 + false + prompt + + + PackageReference + + + + + + + + + 6.2.13 + + + + + {d985b70e-cdf3-4cf1-ab5d-8d19c7fe7b31} + OBD.NET + + + + + + + 14.0 + + + + \ No newline at end of file diff --git a/OBD.NET/OBD.NET.Universal/OBD.NET.Universal.nuspec b/OBD.NET/OBD.NET.Universal/OBD.NET.Universal.nuspec new file mode 100644 index 0000000..5fcc230 --- /dev/null +++ b/OBD.NET/OBD.NET.Universal/OBD.NET.Universal.nuspec @@ -0,0 +1,15 @@ + + + + $id$ + $version$ + $title$ + $author$ + $author$ + https://github.com/romanlum/OBD.NET + false + $description$ + Initial release. + Copyright 2017 + + \ No newline at end of file diff --git a/OBD.NET/OBD.NET.Universal/Properties/AssemblyInfo.cs b/OBD.NET/OBD.NET.Universal/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..483f8d8 --- /dev/null +++ b/OBD.NET/OBD.NET.Universal/Properties/AssemblyInfo.cs @@ -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("OBD.NET.Universal")] +[assembly: AssemblyDescription("C#-Library to read/write data from/to a car through an ELM327-/STN1170-Adapter on UWP")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Roman Lumetsberger")] +[assembly: AssemblyProduct("OBD.NET")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[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)] \ No newline at end of file diff --git a/OBD.NET/OBD.NET.Universal/Properties/OBD.NET.Universal.rd.xml b/OBD.NET/OBD.NET.Universal/Properties/OBD.NET.Universal.rd.xml new file mode 100644 index 0000000..874b02e --- /dev/null +++ b/OBD.NET/OBD.NET.Universal/Properties/OBD.NET.Universal.rd.xml @@ -0,0 +1,33 @@ + + + + + + + + + diff --git a/OBD.NET/OBD.NET.sln b/OBD.NET/OBD.NET.sln index 0d1d446..b5c2464 100644 --- a/OBD.NET/OBD.NET.sln +++ b/OBD.NET/OBD.NET.sln @@ -1,22 +1,93 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OBD.NET", "OBD.NET\OBD.NET.csproj", "{F60052E8-1201-4A5A-ADD7-6367C5424F74}" +# Visual Studio Version 17 +VisualStudioVersion = 17.0.32014.148 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OBD.NET", "OBD.NET\OBD.NET.csproj", "{F60052E8-1201-4A5A-ADD7-6367C5424F74}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleClient", "ConsoleClient\ConsoleClient.csproj", "{8AC58110-3925-481F-9D85-4B809D7D18B9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleClient", "ConsoleClient\ConsoleClient.csproj", "{8AC58110-3925-481F-9D85-4B809D7D18B9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OBD.NET.Universal", "OBD.NET.Universal\OBD.NET.Universal.csproj", "{E0EAFF82-C514-4827-8F49-F1928EBA8E73}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|ARM = Debug|ARM + Debug|ARM64 = Debug|ARM64 + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU + Release|ARM = Release|ARM + Release|ARM64 = Release|ARM64 + Release|x64 = Release|x64 + Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Debug|ARM.ActiveCfg = Debug|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Debug|ARM.Build.0 = Debug|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Debug|ARM64.Build.0 = Debug|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Debug|x64.ActiveCfg = Debug|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Debug|x64.Build.0 = Debug|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Debug|x86.ActiveCfg = Debug|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Debug|x86.Build.0 = Debug|Any CPU {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Release|Any CPU.ActiveCfg = Release|Any CPU {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Release|Any CPU.Build.0 = Release|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Release|ARM.ActiveCfg = Release|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Release|ARM.Build.0 = Release|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Release|ARM64.ActiveCfg = Release|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Release|ARM64.Build.0 = Release|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Release|x64.ActiveCfg = Release|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Release|x64.Build.0 = Release|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Release|x86.ActiveCfg = Release|Any CPU + {F60052E8-1201-4A5A-ADD7-6367C5424F74}.Release|x86.Build.0 = Release|Any CPU {8AC58110-3925-481F-9D85-4B809D7D18B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8AC58110-3925-481F-9D85-4B809D7D18B9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Debug|ARM.ActiveCfg = Debug|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Debug|ARM.Build.0 = Debug|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Debug|ARM64.Build.0 = Debug|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Debug|x64.ActiveCfg = Debug|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Debug|x64.Build.0 = Debug|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Debug|x86.ActiveCfg = Debug|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Debug|x86.Build.0 = Debug|Any CPU {8AC58110-3925-481F-9D85-4B809D7D18B9}.Release|Any CPU.ActiveCfg = Release|Any CPU {8AC58110-3925-481F-9D85-4B809D7D18B9}.Release|Any CPU.Build.0 = Release|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Release|ARM.ActiveCfg = Release|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Release|ARM.Build.0 = Release|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Release|ARM64.ActiveCfg = Release|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Release|ARM64.Build.0 = Release|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Release|x64.ActiveCfg = Release|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Release|x64.Build.0 = Release|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Release|x86.ActiveCfg = Release|Any CPU + {8AC58110-3925-481F-9D85-4B809D7D18B9}.Release|x86.Build.0 = Release|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|ARM.ActiveCfg = Debug|ARM + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|ARM.Build.0 = Debug|ARM + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|ARM64.Build.0 = Debug|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x64.ActiveCfg = Debug|x64 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x64.Build.0 = Debug|x64 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x86.ActiveCfg = Debug|x86 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x86.Build.0 = Debug|x86 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|Any CPU.Build.0 = Release|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|ARM.ActiveCfg = Release|ARM + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|ARM.Build.0 = Release|ARM + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|ARM64.ActiveCfg = Release|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|ARM64.Build.0 = Release|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x64.ActiveCfg = Release|x64 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x64.Build.0 = Release|x64 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x86.ActiveCfg = Release|x86 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {949E7C1D-8E95-46CF-B659-C6A15678A0D5} EndGlobalSection EndGlobal diff --git a/OBD.NET/OBD.NET/Communication/SerialConnection.cs b/OBD.NET/OBD.NET/Communication/SerialConnection.cs index 833d70c..1578222 100644 --- a/OBD.NET/OBD.NET/Communication/SerialConnection.cs +++ b/OBD.NET/OBD.NET/Communication/SerialConnection.cs @@ -1,3 +1,5 @@ +#if NET5_0_OR_GREATER + using System.IO.Ports; using System.Text; using OBD.NET.Communication.EventArgs; @@ -68,4 +70,6 @@ public class SerialConnection : ISerialConnection public void Write(byte[] data) => _serialPort.Write(data, 0, data.Length); #endregion -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/OBD.NET/OBD.NET/OBD.NET.csproj b/OBD.NET/OBD.NET/OBD.NET.csproj index a35313e..d8788c4 100644 --- a/OBD.NET/OBD.NET/OBD.NET.csproj +++ b/OBD.NET/OBD.NET/OBD.NET.csproj @@ -1,9 +1,9 @@ - + enable enable - net6.0;net5.0;netstandard2.0 + net6.0;net5.0;netstandard1.4 10 Darth Affe / Roman Lumetsberger @@ -27,7 +27,20 @@ OBD.NET OBD.NET + + + NET5_0;NETFULL + + + + NET6_0;NETFULL + + + + NETCORE;NETSTANDARD;NETSTANDARD1_4 + + $(DefineConstants);TRACE;DEBUG true @@ -42,7 +55,7 @@ $(DefineConstants);RELEASE - + diff --git a/OBD.NET/OBD.NET/Properties/launchSettings.json b/OBD.NET/OBD.NET/Properties/launchSettings.json new file mode 100644 index 0000000..6a6f2e0 --- /dev/null +++ b/OBD.NET/OBD.NET/Properties/launchSettings.json @@ -0,0 +1,7 @@ +{ + "profiles": { + "OBD.NET": { + "commandName": "Project" + } + } +} \ No newline at end of file