mirror of
https://github.com/DarthAffe/OBD.NET.git
synced 2025-12-12 16:58:30 +00:00
work
This commit is contained in:
parent
8e5a25ca85
commit
9d6d1cd36b
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace OBD.NET.Common.Communication.EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Event args on receiving serial data
|
||||
/// </summary>
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
/// <summary>
|
||||
/// Occurs when a full line was received
|
||||
/// </summary>
|
||||
event EventHandler<string> MessageReceived;
|
||||
event EventHandler<DataReceivedEventArgs> DataReceived;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Connects the serial port.
|
||||
/// </summary>
|
||||
void Connect();
|
||||
bool Connect();
|
||||
|
||||
/// <summary>
|
||||
/// Connects the serial port async
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task ConnectAsync();
|
||||
Task<bool> ConnectAsync();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified text to the serial connection
|
||||
/// Writes the specified data to the serial connection
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
void Write(string text);
|
||||
void Write(byte[] data);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified text to the serial connection async
|
||||
/// Writes the specified data to the serial connection async
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
Task WriteAsync(string text);
|
||||
Task WriteAsync(byte[] data);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
{
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class used for communicating with the device
|
||||
/// </summary>
|
||||
public abstract class SerialDevice : IDisposable
|
||||
{
|
||||
#region Properties & Fields
|
||||
private System.Collections.Generic.Queue<string> commandQueue;
|
||||
|
||||
/// <summary>
|
||||
/// Logger instance
|
||||
/// </summary>
|
||||
protected IOBDLogger Logger { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Low level connection
|
||||
/// </summary>
|
||||
protected ISerialConnection Connection { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Terminator of the protocol message
|
||||
/// </summary>
|
||||
protected char Terminator { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region Constructors
|
||||
|
||||
private SerialDevice()
|
||||
{
|
||||
commandQueue = new System.Collections.Generic.Queue<string>();
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the device
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the device async
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Prepares the command
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
/// <returns></returns>
|
||||
protected virtual string PrepareCommand(string command)
|
||||
{
|
||||
if (command == null) throw new ArgumentNullException(nameof(command));
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Communication\" />
|
||||
<Folder Include="Properties\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user