mirror of
https://github.com/DarthAffe/OBD.NET.git
synced 2025-12-12 16:58:30 +00:00
added async implementations (not supported for serial connection at the moment)
will be used for uwp implementation
This commit is contained in:
parent
f9b25b4965
commit
771ade2054
@ -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
|
||||
/// </summary>
|
||||
void Connect();
|
||||
|
||||
/// <summary>
|
||||
/// Connects the serial port async
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task ConnectAsync();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified text to the serial connection
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
void Write(string text);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified text to the serial connection async
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
Task WriteAsync(string text);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<T>()
|
||||
where T : class, IOBDData, new()
|
||||
{
|
||||
@ -95,12 +138,27 @@ namespace OBD.NET.Devices
|
||||
RequestData(pid);
|
||||
}
|
||||
|
||||
public virtual async Task RequestDataAsync<T>()
|
||||
where T : class, IOBDData, new()
|
||||
{
|
||||
Logger?.WriteLine("Requesting Type " + typeof(T).Name + " ...", OBDLogLevel.Debug);
|
||||
|
||||
byte pid = ResolvePid<T>();
|
||||
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;
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user