diff --git a/OBD.NET/OBD.NET.Common/Communication/EventArgs/DataReceivedEventArgs.cs b/OBD.NET/OBD.NET.Common/Communication/EventArgs/DataReceivedEventArgs.cs index 533b034..bff6c57 100644 --- a/OBD.NET/OBD.NET.Common/Communication/EventArgs/DataReceivedEventArgs.cs +++ b/OBD.NET/OBD.NET.Common/Communication/EventArgs/DataReceivedEventArgs.cs @@ -1,21 +1,29 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace OBD.NET.Common.Communication.EventArgs +namespace OBD.NET.Common.Communication.EventArgs { /// - /// Event args on receiving serial data + /// Event args for receiving serial data /// public class DataReceivedEventArgs:System.EventArgs { + /// + /// Initializes a new instance of the class. + /// + /// The count. + /// The data. public DataReceivedEventArgs(int count, byte[] data) { Count = count; Data = data; } + /// + /// Count of valid data bytes in the buffer + /// public int Count { get; private set; } + + /// + /// Data buffer holding the bytes + /// public byte[] Data { get; private set; } } } diff --git a/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs b/OBD.NET/OBD.NET.Common/Communication/ISerialConnection.cs similarity index 89% rename from OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs rename to OBD.NET/OBD.NET.Common/Communication/ISerialConnection.cs index 00b84d8..6b2ea8e 100644 --- a/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs +++ b/OBD.NET/OBD.NET.Common/Communication/ISerialConnection.cs @@ -1,7 +1,5 @@ using OBD.NET.Common.Communication.EventArgs; using System; -using System.Text; -using System.Threading; using System.Threading.Tasks; namespace OBD.NET.Communication @@ -23,9 +21,10 @@ namespace OBD.NET.Communication /// /// Gets a value indicating whether this instance uses asynchronous IO /// - /// - /// true if this instance is asynchronous; otherwise, false. - /// + /// + /// Has to be set to true if asynchronous IO is supported. + /// If true async methods have to be implemented + /// bool IsAsync { get; } /// @@ -43,8 +42,7 @@ namespace OBD.NET.Communication /// /// Task ConnectAsync(); - - + /// /// Writes the specified data to the serial connection /// diff --git a/OBD.NET/OBD.NET.Common/Devices/ELM327.cs b/OBD.NET/OBD.NET.Common/Devices/ELM327.cs index 2fe0f18..b08d6ac 100644 --- a/OBD.NET/OBD.NET.Common/Devices/ELM327.cs +++ b/OBD.NET/OBD.NET.Common/Devices/ELM327.cs @@ -45,12 +45,22 @@ namespace OBD.NET.Devices #region Methods + public override async Task InitializeAsync() + { + await base.InitializeAsync(); + InternalInitialize(); + } + public override void Initialize() { base.Initialize(); + InternalInitialize(); + } + private void InternalInitialize() + { Logger?.WriteLine("Initializing ...", OBDLogLevel.Debug); - + try { Logger?.WriteLine("Resetting Device ...", OBDLogLevel.Debug); @@ -58,7 +68,7 @@ namespace OBD.NET.Devices Logger?.WriteLine("Turning Echo Off ...", OBDLogLevel.Debug); SendCommand(ATCommand.EchoOff); - + Logger?.WriteLine("Turning Linefeeds Off ...", OBDLogLevel.Debug); SendCommand(ATCommand.LinefeedsOff); diff --git a/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs b/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs index 620c01d..1d2f2f9 100644 --- a/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs +++ b/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs @@ -22,6 +22,7 @@ namespace OBD.NET.Devices private readonly AutoResetEvent commandFinishedEvent = new AutoResetEvent(false); private Task commandWorkerTask; + private CancellationTokenSource commandCancellationToken; /// @@ -38,15 +39,24 @@ namespace OBD.NET.Devices /// Terminator of the protocol message /// protected char Terminator { get; set; } - - + + #region Constructors + /// + /// Prevents a default instance of the class from being created. + /// private SerialDevice() { commandQueue = new BlockingCollection(); } + /// + /// Initializes a new instance of the class. + /// + /// connection. + /// terminator used for terminating the command message + /// logger instance protected SerialDevice(ISerialConnection connection, char terminator = '\r', IOBDLogger logger = null) :this() { @@ -66,19 +76,25 @@ namespace OBD.NET.Devices /// /// Initializes the device /// - public virtual async void Initialize() + public virtual void Initialize() { - if (Connection.IsAsync) - { - await Connection.ConnectAsync(); - } - else - { - Connection.Connect(); - } + Connection.Connect(); CheckConnectionAndStartWorker(); } + /// + /// Initializes the device + /// + public virtual async Task InitializeAsync() + { + await Connection.ConnectAsync(); + CheckConnectionAndStartWorker(); + } + + /// + /// Checks the connection and starts background worker which is sending the commands + /// + /// Failed to open Serial-Connection. private void CheckConnectionAndStartWorker() { if (!Connection.IsOpen) @@ -95,29 +111,12 @@ namespace OBD.NET.Devices commandWorkerTask = Task.Factory.StartNew(CommandWorker); } - private async void CommandWorker() - { - while (!commandCancellationToken.IsCancellationRequested) - { - string command = null; - commandQueue.TryTake(out command, Timeout.Infinite, commandCancellationToken.Token); - Logger?.WriteLine("Writing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose); - - if (Connection.IsAsync) - { - await Connection.WriteAsync(Encoding.ASCII.GetBytes(command)); - } - else - { - Connection.Write(Encoding.ASCII.GetBytes(command)); - - } - //wait for command to finish - commandFinishedEvent.WaitOne(); - - } - } + /// + /// Sends the command. + /// + /// command string + /// Not connected protected virtual void SendCommand(string command) { if (!Connection.IsOpen) @@ -145,6 +144,11 @@ namespace OBD.NET.Devices return command; } + /// + /// Called when data is received from the serial device + /// + /// The sender. + /// The instance containing the event data. private void OnDataReceived(object sender, DataReceivedEventArgs e) { for (int i = 0; i < e.Count; i++) @@ -171,6 +175,9 @@ namespace OBD.NET.Devices } } + /// + /// Signals a final message + /// private void FinishLine() { string line = _lineBuffer.ToString().Trim(); @@ -182,11 +189,45 @@ namespace OBD.NET.Devices ProcessMessage(line); } - + /// + /// Processes the message. + /// + /// message received protected abstract void ProcessMessage(string message); - + + /// + /// Worker method for sending commands + /// + private async void CommandWorker() + { + while (!commandCancellationToken.IsCancellationRequested) + { + string command = null; + commandQueue.TryTake(out command, Timeout.Infinite, commandCancellationToken.Token); + Logger?.WriteLine("Writing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose); + + if (Connection.IsAsync) + { + await Connection.WriteAsync(Encoding.ASCII.GetBytes(command)); + } + else + { + Connection.Write(Encoding.ASCII.GetBytes(command)); + + } + //wait for command to finish + commandFinishedEvent.WaitOne(); + + } + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// public virtual void Dispose() { + commandCancellationToken?.Cancel(); + commandWorkerTask?.Wait(); Connection?.Dispose(); } diff --git a/OBD.NET/OBD.NET.Common/Logging/TraceLogger.cs b/OBD.NET/OBD.NET.Common/Logging/OBDDebugLogger.cs similarity index 85% rename from OBD.NET/OBD.NET.Common/Logging/TraceLogger.cs rename to OBD.NET/OBD.NET.Common/Logging/OBDDebugLogger.cs index 7ee9adf..949c490 100644 --- a/OBD.NET/OBD.NET.Common/Logging/TraceLogger.cs +++ b/OBD.NET/OBD.NET.Common/Logging/OBDDebugLogger.cs @@ -1,8 +1,5 @@ using OBD.NET.Logging; -using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Text; namespace OBD.NET.Common.Logging { diff --git a/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs b/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs index 164c5d4..f4d1352 100644 --- a/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs +++ b/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.InteropServices.WindowsRuntime; -using System.Text; using System.Threading; using System.Threading.Tasks; using OBD.NET.Common.Communication.EventArgs; @@ -10,6 +9,10 @@ using Windows.Storage.Streams; namespace OBD.NET.Communication { + /// + /// Bluetooth OBD serial implementation + /// + /// public class BluetoothSerialConnection : ISerialConnection { @@ -20,15 +23,43 @@ namespace OBD.NET.Communication private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); private Task _readerTask; - + + private string device; + /// - /// Gets a value indicating whether this connection is open. + /// 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 connection is open; otherwise, false. + /// 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; /// @@ -42,7 +73,7 @@ namespace OBD.NET.Communication /// Synchronous operations not supported public void Connect() { - throw new NotSupportedException("Synchronous operations not supported"); + throw new NotSupportedException("Synchronous operations not supported on UWP platform"); } /// @@ -78,7 +109,7 @@ namespace OBD.NET.Communication /// Synchronous operations not supported public void Write(byte[] data) { - throw new NotImplementedException("Synchronous operations not supported"); + throw new NotImplementedException("Synchronous operations not supported on UWP platform"); } ///