diff --git a/OBD.NET/OBD.NET.Common/Devices/ELM327.cs b/OBD.NET/OBD.NET.Common/Devices/ELM327.cs index fc43562..4921712 100644 --- a/OBD.NET/OBD.NET.Common/Devices/ELM327.cs +++ b/OBD.NET/OBD.NET.Common/Devices/ELM327.cs @@ -23,7 +23,7 @@ namespace OBD.NET.Devices protected static Dictionary DataTypeCache { get; } = new Dictionary(); protected Mode Mode { get; set; } = Mode.ShowCurrentData; //TODO DarthAffe 26.06.2016: Implement different modes - + #endregion #region Events @@ -50,16 +50,15 @@ namespace OBD.NET.Devices base.Initialize(); Logger?.WriteLine("Initializing ...", OBDLogLevel.Debug); - + try { Logger?.WriteLine("Resetting Device ...", OBDLogLevel.Debug); SendCommand(ATCommand.ResetDevice); - Thread.Sleep(1000); Logger?.WriteLine("Turning Echo Off ...", OBDLogLevel.Debug); SendCommand(ATCommand.EchoOff); - + Logger?.WriteLine("Turning Linefeeds Off ...", OBDLogLevel.Debug); SendCommand(ATCommand.LinefeedsOff); @@ -72,7 +71,6 @@ namespace OBD.NET.Devices Logger?.WriteLine("Setting the Protocol to 'Auto' ...", OBDLogLevel.Debug); SendCommand(ATCommand.SetProtocolAuto); - Thread.Sleep(1000); } // DarthAffe 21.02.2017: This seems to happen sometimes, i don't know why - just retry. catch @@ -87,29 +85,27 @@ namespace OBD.NET.Devices await base.InitializeAsync(); Logger?.WriteLine("Initializing ...", OBDLogLevel.Debug); - + try { Logger?.WriteLine("Resetting Device ...", OBDLogLevel.Debug); await SendCommandAsync(ATCommand.ResetDevice); - Thread.Sleep(1000); - Logger?.WriteLine("Turning Echo Off ...", OBDLogLevel.Debug); await SendCommandAsync(ATCommand.EchoOff); Logger?.WriteLine("Turning Linefeeds Off ...", OBDLogLevel.Debug); await SendCommandAsync(ATCommand.LinefeedsOff); - + Logger?.WriteLine("Turning Headers Off ...", OBDLogLevel.Debug); await SendCommandAsync(ATCommand.HeadersOff); - + Logger?.WriteLine("Turning Spaced Off ...", OBDLogLevel.Debug); await SendCommandAsync(ATCommand.PrintSpacesOff); - + Logger?.WriteLine("Setting the Protocol to 'Auto' ...", OBDLogLevel.Debug); - await SendCommandAsync(ATCommand.SetProtocolAuto); + await SendCommandAsync(ATCommand.SetProtocolAuto); //TODO rewrite - await Task.Delay(1000); //TODO CHECK + } // DarthAffe 21.02.2017: This seems to happen sometimes, i don't know why - just retry. catch @@ -213,7 +209,7 @@ namespace OBD.NET.Devices if (sendCloseProtocol) { SendCommand(ATCommand.CloseProtocol); - Thread.Sleep(500); + //Thread.Sleep(500); } } catch { } diff --git a/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs b/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs index af3ed4c..cd48f14 100644 --- a/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs +++ b/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.InteropServices.WindowsRuntime; using System.Text; +using System.Threading; using System.Threading.Tasks; using Windows.Devices.Bluetooth.Rfcomm; using Windows.Networking.Sockets; @@ -16,6 +17,9 @@ namespace OBD.NET.Communication private readonly byte[] _readBuffer = new byte[1024]; private readonly StringBuilder _lineBuffer = new StringBuilder(); + + private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); + private Task _readerTask; /// /// Gets a value indicating whether this connection is open. @@ -25,19 +29,30 @@ namespace OBD.NET.Communication /// public bool IsOpen { get; private set; } - + /// + /// Occurs when a full line was received + /// public event EventHandler MessageReceived; + /// + /// Connects the serial port. + /// + /// Synchronous operations not supported public void Connect() { - throw new NotSupportedException(); + throw new NotSupportedException("Synchronous operations not supported"); } + /// + /// 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) { // Initialize the target Bluetooth BR device @@ -49,27 +64,45 @@ namespace OBD.NET.Communication await _socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName); _writer = new DataWriter(_socket.OutputStream); - StartReader(); + _readerTask = StartReader(); IsOpen = true; - - } } - private void StartReader() + /// + /// Writes the specified text to the serial connection + /// + /// The text. + /// Synchronous operations not supported + public void Write(string text) { - Task.Factory.StartNew(async () => - { + throw new NotImplementedException("Synchronous operations not supported"); + } - var buffer = _readBuffer.AsBuffer(); - while (true) - { - var readData = await _socket.InputStream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.Partial); - SerialPortOnDataReceived(readData); - } - + /// + /// Writes the specified text to the serial connection asynchronously + /// + /// The text. + /// + public async Task WriteAsync(string text) + { + _writer.WriteString(text); + 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) @@ -108,19 +141,11 @@ namespace OBD.NET.Communication public void Dispose() { + _cancellationTokenSource?.Cancel(); + _readerTask?.Wait(); _socket?.Dispose(); } - public void Write(string text) - { - throw new NotImplementedException(); - } - - public async Task WriteAsync(string text) - { - _writer.WriteString(text); - await _writer.StoreAsync(); - await _writer.FlushAsync(); - } + } } diff --git a/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs b/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs index 9ece5b9..23ad33d 100644 --- a/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs +++ b/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs @@ -106,12 +106,12 @@ namespace OBD.NET.Communication public Task ConnectAsync() { - throw new NotImplementedException(); + throw new NotSupportedException("Asynchronous operations not supported"); } public Task WriteAsync(string text) { - throw new NotImplementedException(); + throw new NotSupportedException("Asynchronous operations not supported"); } #endregion