diff --git a/OBD.NET/OBD.NET.Common/Communication/EventArgs/DataReceivedEventArgs.cs b/OBD.NET/OBD.NET.Common/Communication/EventArgs/DataReceivedEventArgs.cs new file mode 100644 index 0000000..533b034 --- /dev/null +++ b/OBD.NET/OBD.NET.Common/Communication/EventArgs/DataReceivedEventArgs.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OBD.NET.Common.Communication.EventArgs +{ + /// + /// Event args on receiving serial data + /// + public class DataReceivedEventArgs:System.EventArgs + { + public DataReceivedEventArgs(int count, byte[] data) + { + Count = count; + Data = data; + } + + public int Count { get; private set; } + public byte[] Data { get; private set; } + } +} diff --git a/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs b/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs index 952bc0d..c25571f 100644 --- a/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs +++ b/OBD.NET/OBD.NET.Common/Communication/SerialConnection.cs @@ -1,4 +1,5 @@ -using System; +using OBD.NET.Common.Communication.EventArgs; +using System; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -22,32 +23,32 @@ namespace OBD.NET.Communication /// /// Occurs when a full line was received /// - event EventHandler MessageReceived; + event EventHandler DataReceived; /// /// Connects the serial port. /// - void Connect(); + bool Connect(); /// /// Connects the serial port async /// /// - Task ConnectAsync(); + Task ConnectAsync(); /// - /// Writes the specified text to the serial connection + /// Writes the specified data to the serial connection /// /// The text. - void Write(string text); + void Write(byte[] data); /// - /// Writes the specified text to the serial connection async + /// Writes the specified data to the serial connection async /// /// The text. - Task WriteAsync(string text); + Task WriteAsync(byte[] data); } } diff --git a/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs b/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs index fb6d9eb..eb1c656 100644 --- a/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs +++ b/OBD.NET/OBD.NET.Common/Devices/SerialDevice.cs @@ -3,54 +3,83 @@ using OBD.NET.Communication; using OBD.NET.Exceptions; using OBD.NET.Logging; using System.Threading.Tasks; +using OBD.NET.Common.Communication.EventArgs; namespace OBD.NET.Devices -{ +{ + /// + /// Base class used for communicating with the device + /// public abstract class SerialDevice : IDisposable { - #region Properties & Fields + private System.Collections.Generic.Queue commandQueue; + /// + /// Logger instance + /// protected IOBDLogger Logger { get; } + /// + /// Low level connection + /// protected ISerialConnection Connection { get; } + + /// + /// Terminator of the protocol message + /// protected char Terminator { get; set; } - - #endregion - + + #region Constructors + private SerialDevice() + { + commandQueue = new System.Collections.Generic.Queue(); + } + protected SerialDevice(ISerialConnection connection, char terminator = '\r', IOBDLogger logger = null) + :this() { Connection = connection; Terminator = terminator; Logger = logger; - connection.MessageReceived += SerialMessageReceived; + connection.DataReceived += OnDataReceived; } + #endregion + private void OnDataReceived(object sender, DataReceivedEventArgs e) + { + + } + #region Methods + + /// + /// Initializes the device + /// public virtual void Initialize() { - Logger?.WriteLine("Opening Serial-Connection ...", OBDLogLevel.Debug); - Connection.Connect(); - 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); + Connection.Connect(); + CheckConnection(); } + /// + /// Initializes the device async + /// + /// public virtual async Task InitializeAsync() { - Logger?.WriteLine("Opening Serial-Connection ...", OBDLogLevel.Debug); await Connection.ConnectAsync(); + CheckConnection(); + } + private void CheckConnection() + { if (!Connection.IsOpen) { Logger?.WriteLine("Failed to open Serial-Connection.", OBDLogLevel.Error); @@ -60,26 +89,27 @@ namespace OBD.NET.Devices Logger?.WriteLine("Opened Serial-Connection!", OBDLogLevel.Debug); } + + protected virtual void SendCommand(string command) { - if (!Connection.IsOpen) return; + if (!Connection.IsOpen) + { + throw new InvalidOperationException("Not connected"); + } command = PrepareCommand(command); - - Logger?.WriteLine("Writing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose); - Connection.Write(command); + Logger?.WriteLine("Queuing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose); + commandQueue.Enqueue(command); +// Logger?.WriteLine("Writing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose); + // 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); - } - + + /// + /// Prepares the command + /// + /// + /// protected virtual string PrepareCommand(string command) { if (command == null) throw new ArgumentNullException(nameof(command)); diff --git a/OBD.NET/OBD.NET.Common/OBD.NET.Common.csproj b/OBD.NET/OBD.NET.Common/OBD.NET.Common.csproj index 856ad40..36184f0 100644 --- a/OBD.NET/OBD.NET.Common/OBD.NET.Common.csproj +++ b/OBD.NET/OBD.NET.Common/OBD.NET.Common.csproj @@ -9,7 +9,6 @@ -