mirror of
https://github.com/DarthAffe/OBD.NET.git
synced 2025-12-13 09:18:31 +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.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -22,32 +23,32 @@ namespace OBD.NET.Communication
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs when a full line was received
|
/// Occurs when a full line was received
|
||||||
/// </summary>
|
/// </summary>
|
||||||
event EventHandler<string> MessageReceived;
|
event EventHandler<DataReceivedEventArgs> DataReceived;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Connects the serial port.
|
/// Connects the serial port.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void Connect();
|
bool Connect();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Connects the serial port async
|
/// Connects the serial port async
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task ConnectAsync();
|
Task<bool> ConnectAsync();
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes the specified text to the serial connection
|
/// Writes the specified data to the serial connection
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="text">The text.</param>
|
/// <param name="text">The text.</param>
|
||||||
void Write(string text);
|
void Write(byte[] data);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes the specified text to the serial connection async
|
/// Writes the specified data to the serial connection async
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="text">The text.</param>
|
/// <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.Exceptions;
|
||||||
using OBD.NET.Logging;
|
using OBD.NET.Logging;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using OBD.NET.Common.Communication.EventArgs;
|
||||||
|
|
||||||
namespace OBD.NET.Devices
|
namespace OBD.NET.Devices
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Base class used for communicating with the device
|
||||||
|
/// </summary>
|
||||||
public abstract class SerialDevice : IDisposable
|
public abstract class SerialDevice : IDisposable
|
||||||
{
|
{
|
||||||
#region Properties & Fields
|
private System.Collections.Generic.Queue<string> commandQueue;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Logger instance
|
||||||
|
/// </summary>
|
||||||
protected IOBDLogger Logger { get; }
|
protected IOBDLogger Logger { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Low level connection
|
||||||
|
/// </summary>
|
||||||
protected ISerialConnection Connection { get; }
|
protected ISerialConnection Connection { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Terminator of the protocol message
|
||||||
|
/// </summary>
|
||||||
protected char Terminator { get; set; }
|
protected char Terminator { get; set; }
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
|
private SerialDevice()
|
||||||
|
{
|
||||||
|
commandQueue = new System.Collections.Generic.Queue<string>();
|
||||||
|
}
|
||||||
|
|
||||||
protected SerialDevice(ISerialConnection connection, char terminator = '\r', IOBDLogger logger = null)
|
protected SerialDevice(ISerialConnection connection, char terminator = '\r', IOBDLogger logger = null)
|
||||||
|
:this()
|
||||||
{
|
{
|
||||||
Connection = connection;
|
Connection = connection;
|
||||||
Terminator = terminator;
|
Terminator = terminator;
|
||||||
Logger = logger;
|
Logger = logger;
|
||||||
|
|
||||||
connection.MessageReceived += SerialMessageReceived;
|
connection.DataReceived += OnDataReceived;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
private void OnDataReceived(object sender, DataReceivedEventArgs e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes the device
|
||||||
|
/// </summary>
|
||||||
public virtual void Initialize()
|
public virtual void Initialize()
|
||||||
{
|
{
|
||||||
Logger?.WriteLine("Opening Serial-Connection ...", OBDLogLevel.Debug);
|
|
||||||
Connection.Connect();
|
Connection.Connect();
|
||||||
|
CheckConnection();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes the device async
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
public virtual async Task InitializeAsync()
|
public virtual async Task InitializeAsync()
|
||||||
{
|
{
|
||||||
Logger?.WriteLine("Opening Serial-Connection ...", OBDLogLevel.Debug);
|
|
||||||
await Connection.ConnectAsync();
|
await Connection.ConnectAsync();
|
||||||
|
CheckConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckConnection()
|
||||||
|
{
|
||||||
if (!Connection.IsOpen)
|
if (!Connection.IsOpen)
|
||||||
{
|
{
|
||||||
Logger?.WriteLine("Failed to open Serial-Connection.", OBDLogLevel.Error);
|
Logger?.WriteLine("Failed to open Serial-Connection.", OBDLogLevel.Error);
|
||||||
@ -60,26 +89,27 @@ namespace OBD.NET.Devices
|
|||||||
Logger?.WriteLine("Opened Serial-Connection!", OBDLogLevel.Debug);
|
Logger?.WriteLine("Opened Serial-Connection!", OBDLogLevel.Debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected virtual void SendCommand(string command)
|
protected virtual void SendCommand(string command)
|
||||||
{
|
{
|
||||||
if (!Connection.IsOpen) return;
|
if (!Connection.IsOpen)
|
||||||
|
|
||||||
command = PrepareCommand(command);
|
|
||||||
|
|
||||||
Logger?.WriteLine("Writing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose);
|
|
||||||
Connection.Write(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual async Task SendCommandAsync(string command)
|
|
||||||
{
|
{
|
||||||
if (!Connection.IsOpen) return;
|
throw new InvalidOperationException("Not connected");
|
||||||
|
|
||||||
command = PrepareCommand(command);
|
|
||||||
|
|
||||||
Logger?.WriteLine("Writing Command: '" + command.Replace('\r', '\'') + "'", OBDLogLevel.Verbose);
|
|
||||||
await Connection.WriteAsync(command);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
command = PrepareCommand(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prepares the command
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command"></param>
|
||||||
|
/// <returns></returns>
|
||||||
protected virtual string PrepareCommand(string command)
|
protected virtual string PrepareCommand(string command)
|
||||||
{
|
{
|
||||||
if (command == null) throw new ArgumentNullException(nameof(command));
|
if (command == null) throw new ArgumentNullException(nameof(command));
|
||||||
|
|||||||
@ -9,7 +9,6 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Communication\" />
|
|
||||||
<Folder Include="Properties\" />
|
<Folder Include="Properties\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user