From 34ca0d5a4f70695bb3b079c4eb4cc3176eae0fb5 Mon Sep 17 00:00:00 2001 From: Roman Lumetsberger Date: Mon, 8 May 2017 20:33:22 +0200 Subject: [PATCH] refactoring --- .../Communication/SerialConnection.cs | 13 +++-- .../OBD.NET.Common/Devices/SerialDevice.cs | 53 ++++++++++--------- .../BluetoothSerialConnection.cs | 2 + .../Communication/SerialConnection.cs | 1 + 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs b/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs index d5934cf..00b84d8 100644 --- a/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs +++ b/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs @@ -20,19 +20,26 @@ namespace OBD.NET.Communication /// bool IsOpen { get; } + /// + /// Gets a value indicating whether this instance uses asynchronous IO + /// + /// + /// true if this instance is asynchronous; otherwise, false. + /// + bool IsAsync { get; } + /// /// Occurs when a full line was received /// event EventHandler DataReceived; - /// /// Connects the serial port. /// void Connect(); /// - /// Connects the serial port async + /// Connects the serial port asynchronous /// /// Task ConnectAsync(); @@ -45,7 +52,7 @@ namespace OBD.NET.Communication void Write(byte[] data); /// - /// Writes the specified data to the serial connection async + /// Writes the specified data to the serial connection asynchronous /// /// The text. Task WriteAsync(byte[] data); diff --git a/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs b/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs index efeffc5..620c01d 100644 --- a/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs +++ b/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs @@ -19,7 +19,7 @@ namespace OBD.NET.Devices private readonly StringBuilder _lineBuffer = new StringBuilder(); - private readonly AutoResetEvent commandFinishedEvent = new AutoResetEvent(true); + private readonly AutoResetEvent commandFinishedEvent = new AutoResetEvent(false); private Task commandWorkerTask; private CancellationTokenSource commandCancellationToken; @@ -66,19 +66,16 @@ namespace OBD.NET.Devices /// /// Initializes the device /// - public virtual void Initialize() + public virtual async void Initialize() { - Connection.Connect(); - CheckConnectionAndStartWorker(); - } - - /// - /// Initializes the device asynchronously - /// - /// - public virtual async Task InitializeAsync() - { - await Connection.ConnectAsync(); + if (Connection.IsAsync) + { + await Connection.ConnectAsync(); + } + else + { + Connection.Connect(); + } CheckConnectionAndStartWorker(); } @@ -94,20 +91,30 @@ namespace OBD.NET.Devices Logger?.WriteLine("Opened Serial-Connection!", OBDLogLevel.Debug); } + commandCancellationToken = new CancellationTokenSource(); commandWorkerTask = Task.Factory.StartNew(CommandWorker); } - private void 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); - Connection.Write(Encoding.ASCII.GetBytes(command)); + if (Connection.IsAsync) + { + await Connection.WriteAsync(Encoding.ASCII.GetBytes(command)); + } + else + { + Connection.Write(Encoding.ASCII.GetBytes(command)); + + } //wait for command to finish commandFinishedEvent.WaitOne(); + } } @@ -166,20 +173,16 @@ namespace OBD.NET.Devices private void FinishLine() { - string line = _lineBuffer.ToString(); + string line = _lineBuffer.ToString().Trim(); _lineBuffer.Clear(); + + if (string.IsNullOrWhiteSpace(line)) return; + Logger?.WriteLine("Response: '" + line + "'", OBDLogLevel.Verbose); + ProcessMessage(line); } - - private void SerialMessageReceived(object sender, string message) - { - if (string.IsNullOrWhiteSpace(message)) return; - - Logger?.WriteLine("Response: '" + message + "'", OBDLogLevel.Verbose); - ProcessMessage(message.Trim()); - } - + protected abstract void ProcessMessage(string message); public virtual void Dispose() diff --git a/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs b/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs index 72b826d..164c5d4 100644 --- a/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs +++ b/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs @@ -29,6 +29,8 @@ namespace OBD.NET.Communication /// public bool IsOpen { get; private set; } + public bool IsAsync => true; + /// /// Occurs when a full line was received /// diff --git a/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs b/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs index 76941f1..6f8b161 100644 --- a/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs +++ b/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs @@ -15,6 +15,7 @@ namespace OBD.NET.Communication private readonly int _timeout; public bool IsOpen => _serialPort?.IsOpen ?? false; + public bool IsAsync => false; private readonly byte[] _readBuffer = new byte[1024]; private readonly StringBuilder _lineBuffer = new StringBuilder();