diff --git a/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs b/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs
index e4a2112..952bc0d 100644
--- a/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs
+++ b/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs
@@ -1,6 +1,7 @@
using System;
using System.Text;
using System.Threading;
+using System.Threading.Tasks;
namespace OBD.NET.Communication
{
@@ -29,12 +30,24 @@ namespace OBD.NET.Communication
///
void Connect();
+ ///
+ /// Connects the serial port async
+ ///
+ ///
+ Task ConnectAsync();
+
///
/// Writes the specified text to the serial connection
///
/// The text.
void Write(string text);
-
+
+ ///
+ /// Writes the specified text to the serial connection async
+ ///
+ /// The text.
+ Task WriteAsync(string text);
+
}
}
diff --git a/OBD.NET/OBD.NET.Common/Devices/ELM327.cs b/OBD.NET/OBD.NET.Common/Devices/ELM327.cs
index 53dbd5b..fc43562 100644
--- a/OBD.NET/OBD.NET.Common/Devices/ELM327.cs
+++ b/OBD.NET/OBD.NET.Common/Devices/ELM327.cs
@@ -9,6 +9,7 @@ using OBD.NET.Events.EventArgs;
using OBD.NET.Extensions;
using OBD.NET.Logging;
using OBD.NET.OBDData;
+using System.Threading.Tasks;
namespace OBD.NET.Devices
{
@@ -81,11 +82,53 @@ namespace OBD.NET.Devices
}
}
+ public override async Task InitializeAsync()
+ {
+ 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 Task.Delay(1000); //TODO CHECK
+ }
+ // DarthAffe 21.02.2017: This seems to happen sometimes, i don't know why - just retry.
+ catch
+ {
+ Logger?.WriteLine("Failed to initialize the device!", OBDLogLevel.Error);
+ throw;
+ }
+ }
+
public virtual void SendCommand(ATCommand command)
{
SendCommand(command.Command);
}
+ public virtual async Task SendCommandAsync(ATCommand command)
+ {
+ await SendCommandAsync(command.Command);
+ }
+
public virtual void RequestData()
where T : class, IOBDData, new()
{
@@ -95,12 +138,27 @@ namespace OBD.NET.Devices
RequestData(pid);
}
+ public virtual async Task RequestDataAsync()
+ where T : class, IOBDData, new()
+ {
+ Logger?.WriteLine("Requesting Type " + typeof(T).Name + " ...", OBDLogLevel.Debug);
+
+ byte pid = ResolvePid();
+ await RequestDataAsync(pid);
+ }
+
protected virtual void RequestData(byte pid)
{
Logger?.WriteLine("Requesting PID " + pid.ToString("X2") + " ...", OBDLogLevel.Debug);
SendCommand(((byte)Mode).ToString("X2") + pid.ToString("X2"));
}
+ protected virtual async Task RequestDataAsync(byte pid)
+ {
+ Logger?.WriteLine("Requesting PID " + pid.ToString("X2") + " ...", OBDLogLevel.Debug);
+ await SendCommandAsync(((byte)Mode).ToString("X2") + pid.ToString("X2"));
+ }
+
protected override void ProcessMessage(string message)
{
DateTime timestamp = DateTime.Now;
diff --git a/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs b/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs
index c92dd8c..fb6d9eb 100644
--- a/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs
+++ b/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs
@@ -2,6 +2,7 @@
using OBD.NET.Communication;
using OBD.NET.Exceptions;
using OBD.NET.Logging;
+using System.Threading.Tasks;
namespace OBD.NET.Devices
{
@@ -20,9 +21,9 @@ namespace OBD.NET.Devices
protected SerialDevice(ISerialConnection connection, char terminator = '\r', IOBDLogger logger = null)
{
- this.Connection = connection;
- this.Terminator = terminator;
- this.Logger = logger;
+ Connection = connection;
+ Terminator = terminator;
+ Logger = logger;
connection.MessageReceived += SerialMessageReceived;
}
@@ -45,6 +46,20 @@ namespace OBD.NET.Devices
Logger?.WriteLine("Opened Serial-Connection!", OBDLogLevel.Debug);
}
+ public virtual async Task InitializeAsync()
+ {
+ Logger?.WriteLine("Opening Serial-Connection ...", OBDLogLevel.Debug);
+ await Connection.ConnectAsync();
+
+ if (!Connection.IsOpen)
+ {
+ Logger?.WriteLine("Failed to open Serial-Connection.", OBDLogLevel.Error);
+ throw new SerialException("Failed to open Serial-Connection.");
+ }
+ else
+ Logger?.WriteLine("Opened Serial-Connection!", OBDLogLevel.Debug);
+ }
+
protected virtual void SendCommand(string command)
{
if (!Connection.IsOpen) return;
@@ -55,6 +70,16 @@ namespace OBD.NET.Devices
Connection.Write(command);
}
+ protected virtual async Task SendCommandAsync(string command)
+ {
+ if (!Connection.IsOpen) return;
+
+ command = PrepareCommand(command);
+
+ Logger?.WriteLine("Writing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose);
+ await Connection.WriteAsync(command);
+ }
+
protected virtual string PrepareCommand(string command)
{
if (command == null) throw new ArgumentNullException(nameof(command));
diff --git a/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs b/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs
index f580314..9ece5b9 100644
--- a/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs
+++ b/OBD.NET/ODB.NET.Desktop/Communication/SerialConnection.cs
@@ -2,6 +2,7 @@
using System.IO.Ports;
using System.Text;
using System.Threading;
+using System.Threading.Tasks;
namespace OBD.NET.Communication
{
@@ -103,6 +104,16 @@ namespace OBD.NET.Communication
_serialPort?.Dispose();
}
+ public Task ConnectAsync()
+ {
+ throw new NotImplementedException();
+ }
+
+ public Task WriteAsync(string text)
+ {
+ throw new NotImplementedException();
+ }
+
#endregion
}
}