mirror of
https://github.com/DarthAffe/OBD.NET.git
synced 2025-12-12 16:58:30 +00:00
refactoring
This commit is contained in:
parent
02bd8630a2
commit
34ca0d5a4f
@ -20,19 +20,26 @@ namespace OBD.NET.Communication
|
||||
/// </value>
|
||||
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>
|
||||
/// Occurs when a full line was received
|
||||
/// </summary>
|
||||
event EventHandler<DataReceivedEventArgs> DataReceived;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Connects the serial port.
|
||||
/// </summary>
|
||||
void Connect();
|
||||
|
||||
/// <summary>
|
||||
/// Connects the serial port async
|
||||
/// Connects the serial port asynchronous
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task ConnectAsync();
|
||||
@ -45,7 +52,7 @@ namespace OBD.NET.Communication
|
||||
void Write(byte[] data);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified data to the serial connection async
|
||||
/// Writes the specified data to the serial connection asynchronous
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
Task WriteAsync(byte[] data);
|
||||
|
||||
@ -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
|
||||
/// <summary>
|
||||
/// Initializes the device
|
||||
/// </summary>
|
||||
public virtual void Initialize()
|
||||
public virtual async void Initialize()
|
||||
{
|
||||
Connection.Connect();
|
||||
CheckConnectionAndStartWorker();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the device asynchronously
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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()
|
||||
|
||||
@ -29,6 +29,8 @@ namespace OBD.NET.Communication
|
||||
/// </value>
|
||||
public bool IsOpen { get; private set; }
|
||||
|
||||
public bool IsAsync => true;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a full line was received
|
||||
/// </summary>
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user