1
0
mirror of https://github.com/DarthAffe/OBD.NET.git synced 2025-12-12 16:58:30 +00:00

refactoring

This commit is contained in:
Roman Lumetsberger 2017-05-08 20:33:22 +02:00
parent 02bd8630a2
commit 34ca0d5a4f
4 changed files with 41 additions and 28 deletions

View File

@ -20,19 +20,26 @@ namespace OBD.NET.Communication
/// </value> /// </value>
bool IsOpen { get; } bool IsOpen { get; }
/// <summary>
/// Gets a value indicating whether this instance uses asynchronous IO
/// </summary>
/// <value>
/// <c>true</c> if this instance is asynchronous; otherwise, <c>false</c>.
/// </value>
bool IsAsync { get; }
/// <summary> /// <summary>
/// Occurs when a full line was received /// Occurs when a full line was received
/// </summary> /// </summary>
event EventHandler<DataReceivedEventArgs> DataReceived; event EventHandler<DataReceivedEventArgs> DataReceived;
/// <summary> /// <summary>
/// Connects the serial port. /// Connects the serial port.
/// </summary> /// </summary>
void Connect(); void Connect();
/// <summary> /// <summary>
/// Connects the serial port async /// Connects the serial port asynchronous
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
Task ConnectAsync(); Task ConnectAsync();
@ -45,7 +52,7 @@ namespace OBD.NET.Communication
void Write(byte[] data); void Write(byte[] data);
/// <summary> /// <summary>
/// Writes the specified data to the serial connection async /// Writes the specified data to the serial connection asynchronous
/// </summary> /// </summary>
/// <param name="text">The text.</param> /// <param name="text">The text.</param>
Task WriteAsync(byte[] data); Task WriteAsync(byte[] data);

View File

@ -19,7 +19,7 @@ namespace OBD.NET.Devices
private readonly StringBuilder _lineBuffer = new StringBuilder(); private readonly StringBuilder _lineBuffer = new StringBuilder();
private readonly AutoResetEvent commandFinishedEvent = new AutoResetEvent(true); private readonly AutoResetEvent commandFinishedEvent = new AutoResetEvent(false);
private Task commandWorkerTask; private Task commandWorkerTask;
private CancellationTokenSource commandCancellationToken; private CancellationTokenSource commandCancellationToken;
@ -66,19 +66,16 @@ namespace OBD.NET.Devices
/// <summary> /// <summary>
/// Initializes the device /// Initializes the device
/// </summary> /// </summary>
public virtual void Initialize() public virtual async void Initialize()
{ {
Connection.Connect(); if (Connection.IsAsync)
CheckConnectionAndStartWorker(); {
} await Connection.ConnectAsync();
}
/// <summary> else
/// Initializes the device asynchronously {
/// </summary> Connection.Connect();
/// <returns></returns> }
public virtual async Task InitializeAsync()
{
await Connection.ConnectAsync();
CheckConnectionAndStartWorker(); CheckConnectionAndStartWorker();
} }
@ -94,20 +91,30 @@ namespace OBD.NET.Devices
Logger?.WriteLine("Opened Serial-Connection!", OBDLogLevel.Debug); Logger?.WriteLine("Opened Serial-Connection!", OBDLogLevel.Debug);
} }
commandCancellationToken = new CancellationTokenSource();
commandWorkerTask = Task.Factory.StartNew(CommandWorker); commandWorkerTask = Task.Factory.StartNew(CommandWorker);
} }
private void CommandWorker() private async void CommandWorker()
{ {
while (!commandCancellationToken.IsCancellationRequested) while (!commandCancellationToken.IsCancellationRequested)
{ {
string command = null; string command = null;
commandQueue.TryTake(out command, Timeout.Infinite, commandCancellationToken.Token); commandQueue.TryTake(out command, Timeout.Infinite, commandCancellationToken.Token);
Logger?.WriteLine("Writing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose); 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 //wait for command to finish
commandFinishedEvent.WaitOne(); commandFinishedEvent.WaitOne();
} }
} }
@ -166,20 +173,16 @@ namespace OBD.NET.Devices
private void FinishLine() private void FinishLine()
{ {
string line = _lineBuffer.ToString(); string line = _lineBuffer.ToString().Trim();
_lineBuffer.Clear(); _lineBuffer.Clear();
if (string.IsNullOrWhiteSpace(line)) return;
Logger?.WriteLine("Response: '" + line + "'", OBDLogLevel.Verbose);
ProcessMessage(line); 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); protected abstract void ProcessMessage(string message);
public virtual void Dispose() public virtual void Dispose()

View File

@ -29,6 +29,8 @@ namespace OBD.NET.Communication
/// </value> /// </value>
public bool IsOpen { get; private set; } public bool IsOpen { get; private set; }
public bool IsAsync => true;
/// <summary> /// <summary>
/// Occurs when a full line was received /// Occurs when a full line was received
/// </summary> /// </summary>

View File

@ -15,6 +15,7 @@ namespace OBD.NET.Communication
private readonly int _timeout; private readonly int _timeout;
public bool IsOpen => _serialPort?.IsOpen ?? false; public bool IsOpen => _serialPort?.IsOpen ?? false;
public bool IsAsync => false;
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();