1
0
mirror of https://github.com/DarthAffe/OBD.NET.git synced 2025-12-13 09:18:31 +00:00

refactoring

This commit is contained in:
Roman Lumetsberger 2017-05-07 21:46:55 +02:00
parent 1a80a8729f
commit 8e5a25ca85
3 changed files with 64 additions and 43 deletions

View File

@ -23,7 +23,7 @@ namespace OBD.NET.Devices
protected static Dictionary<byte, Type> DataTypeCache { get; } = new Dictionary<byte, Type>(); protected static Dictionary<byte, Type> DataTypeCache { get; } = new Dictionary<byte, Type>();
protected Mode Mode { get; set; } = Mode.ShowCurrentData; //TODO DarthAffe 26.06.2016: Implement different modes protected Mode Mode { get; set; } = Mode.ShowCurrentData; //TODO DarthAffe 26.06.2016: Implement different modes
#endregion #endregion
#region Events #region Events
@ -50,16 +50,15 @@ namespace OBD.NET.Devices
base.Initialize(); base.Initialize();
Logger?.WriteLine("Initializing ...", OBDLogLevel.Debug); Logger?.WriteLine("Initializing ...", OBDLogLevel.Debug);
try try
{ {
Logger?.WriteLine("Resetting Device ...", OBDLogLevel.Debug); Logger?.WriteLine("Resetting Device ...", OBDLogLevel.Debug);
SendCommand(ATCommand.ResetDevice); SendCommand(ATCommand.ResetDevice);
Thread.Sleep(1000);
Logger?.WriteLine("Turning Echo Off ...", OBDLogLevel.Debug); Logger?.WriteLine("Turning Echo Off ...", OBDLogLevel.Debug);
SendCommand(ATCommand.EchoOff); SendCommand(ATCommand.EchoOff);
Logger?.WriteLine("Turning Linefeeds Off ...", OBDLogLevel.Debug); Logger?.WriteLine("Turning Linefeeds Off ...", OBDLogLevel.Debug);
SendCommand(ATCommand.LinefeedsOff); SendCommand(ATCommand.LinefeedsOff);
@ -72,7 +71,6 @@ namespace OBD.NET.Devices
Logger?.WriteLine("Setting the Protocol to 'Auto' ...", OBDLogLevel.Debug); Logger?.WriteLine("Setting the Protocol to 'Auto' ...", OBDLogLevel.Debug);
SendCommand(ATCommand.SetProtocolAuto); SendCommand(ATCommand.SetProtocolAuto);
Thread.Sleep(1000);
} }
// DarthAffe 21.02.2017: This seems to happen sometimes, i don't know why - just retry. // DarthAffe 21.02.2017: This seems to happen sometimes, i don't know why - just retry.
catch catch
@ -87,29 +85,27 @@ namespace OBD.NET.Devices
await base.InitializeAsync(); await base.InitializeAsync();
Logger?.WriteLine("Initializing ...", OBDLogLevel.Debug); Logger?.WriteLine("Initializing ...", OBDLogLevel.Debug);
try try
{ {
Logger?.WriteLine("Resetting Device ...", OBDLogLevel.Debug); Logger?.WriteLine("Resetting Device ...", OBDLogLevel.Debug);
await SendCommandAsync(ATCommand.ResetDevice); await SendCommandAsync(ATCommand.ResetDevice);
Thread.Sleep(1000);
Logger?.WriteLine("Turning Echo Off ...", OBDLogLevel.Debug); Logger?.WriteLine("Turning Echo Off ...", OBDLogLevel.Debug);
await SendCommandAsync(ATCommand.EchoOff); await SendCommandAsync(ATCommand.EchoOff);
Logger?.WriteLine("Turning Linefeeds Off ...", OBDLogLevel.Debug); Logger?.WriteLine("Turning Linefeeds Off ...", OBDLogLevel.Debug);
await SendCommandAsync(ATCommand.LinefeedsOff); await SendCommandAsync(ATCommand.LinefeedsOff);
Logger?.WriteLine("Turning Headers Off ...", OBDLogLevel.Debug); Logger?.WriteLine("Turning Headers Off ...", OBDLogLevel.Debug);
await SendCommandAsync(ATCommand.HeadersOff); await SendCommandAsync(ATCommand.HeadersOff);
Logger?.WriteLine("Turning Spaced Off ...", OBDLogLevel.Debug); Logger?.WriteLine("Turning Spaced Off ...", OBDLogLevel.Debug);
await SendCommandAsync(ATCommand.PrintSpacesOff); await SendCommandAsync(ATCommand.PrintSpacesOff);
Logger?.WriteLine("Setting the Protocol to 'Auto' ...", OBDLogLevel.Debug); 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. // DarthAffe 21.02.2017: This seems to happen sometimes, i don't know why - just retry.
catch catch
@ -213,7 +209,7 @@ namespace OBD.NET.Devices
if (sendCloseProtocol) if (sendCloseProtocol)
{ {
SendCommand(ATCommand.CloseProtocol); SendCommand(ATCommand.CloseProtocol);
Thread.Sleep(500); //Thread.Sleep(500);
} }
} }
catch { } catch { }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Runtime.InteropServices.WindowsRuntime; using System.Runtime.InteropServices.WindowsRuntime;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.Devices.Bluetooth.Rfcomm; using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Networking.Sockets; using Windows.Networking.Sockets;
@ -16,6 +17,9 @@ namespace OBD.NET.Communication
private readonly byte[] _readBuffer = new byte[1024]; private readonly byte[] _readBuffer = new byte[1024];
private readonly StringBuilder _lineBuffer = new StringBuilder(); private readonly StringBuilder _lineBuffer = new StringBuilder();
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private Task _readerTask;
/// <summary> /// <summary>
/// Gets a value indicating whether this connection is open. /// Gets a value indicating whether this connection is open.
@ -25,19 +29,30 @@ namespace OBD.NET.Communication
/// </value> /// </value>
public bool IsOpen { get; private set; } public bool IsOpen { get; private set; }
/// <summary>
/// Occurs when a full line was received
/// </summary>
public event EventHandler<string> MessageReceived; public event EventHandler<string> MessageReceived;
/// <summary>
/// Connects the serial port.
/// </summary>
/// <exception cref="System.NotSupportedException">Synchronous operations not supported</exception>
public void Connect() public void Connect()
{ {
throw new NotSupportedException(); throw new NotSupportedException("Synchronous operations not supported");
} }
/// <summary>
/// Connects the serial port asynchronously
/// </summary>
/// <returns></returns>
public async Task ConnectAsync() public async Task ConnectAsync()
{ {
var services = await Windows.Devices.Enumeration.DeviceInformation var services = await Windows.Devices.Enumeration.DeviceInformation
.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort)); .FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
//use first serial service
if (services.Count > 0) if (services.Count > 0)
{ {
// Initialize the target Bluetooth BR device // Initialize the target Bluetooth BR device
@ -49,27 +64,45 @@ namespace OBD.NET.Communication
await _socket.ConnectAsync(service.ConnectionHostName, await _socket.ConnectAsync(service.ConnectionHostName,
service.ConnectionServiceName); service.ConnectionServiceName);
_writer = new DataWriter(_socket.OutputStream); _writer = new DataWriter(_socket.OutputStream);
StartReader(); _readerTask = StartReader();
IsOpen = true; IsOpen = true;
} }
} }
private void StartReader() /// <summary>
/// Writes the specified text to the serial connection
/// </summary>
/// <param name="text">The text.</param>
/// <exception cref="System.NotImplementedException">Synchronous operations not supported</exception>
public void Write(string text)
{ {
Task.Factory.StartNew(async () => throw new NotImplementedException("Synchronous operations not supported");
{ }
var buffer = _readBuffer.AsBuffer(); /// <summary>
while (true) /// Writes the specified text to the serial connection asynchronously
{ /// </summary>
var readData = await _socket.InputStream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.Partial); /// <param name="text">The text.</param>
SerialPortOnDataReceived(readData); /// <returns></returns>
} 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) private void SerialPortOnDataReceived(IBuffer buffer)
@ -108,19 +141,11 @@ namespace OBD.NET.Communication
public void Dispose() public void Dispose()
{ {
_cancellationTokenSource?.Cancel();
_readerTask?.Wait();
_socket?.Dispose(); _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();
}
} }
} }

View File

@ -106,12 +106,12 @@ namespace OBD.NET.Communication
public Task ConnectAsync() public Task ConnectAsync()
{ {
throw new NotImplementedException(); throw new NotSupportedException("Asynchronous operations not supported");
} }
public Task WriteAsync(string text) public Task WriteAsync(string text)
{ {
throw new NotImplementedException(); throw new NotSupportedException("Asynchronous operations not supported");
} }
#endregion #endregion