mirror of
https://github.com/DarthAffe/OBD.NET.git
synced 2025-12-12 16:58:30 +00:00
refactoring
This commit is contained in:
parent
1a80a8729f
commit
8e5a25ca85
@ -23,7 +23,7 @@ namespace OBD.NET.Devices
|
||||
protected static Dictionary<byte, Type> DataTypeCache { get; } = new Dictionary<byte, Type>();
|
||||
|
||||
protected Mode Mode { get; set; } = Mode.ShowCurrentData; //TODO DarthAffe 26.06.2016: Implement different modes
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
@ -50,16 +50,15 @@ namespace OBD.NET.Devices
|
||||
base.Initialize();
|
||||
|
||||
Logger?.WriteLine("Initializing ...", OBDLogLevel.Debug);
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
Logger?.WriteLine("Resetting Device ...", OBDLogLevel.Debug);
|
||||
SendCommand(ATCommand.ResetDevice);
|
||||
Thread.Sleep(1000);
|
||||
|
||||
Logger?.WriteLine("Turning Echo Off ...", OBDLogLevel.Debug);
|
||||
SendCommand(ATCommand.EchoOff);
|
||||
|
||||
|
||||
Logger?.WriteLine("Turning Linefeeds Off ...", OBDLogLevel.Debug);
|
||||
SendCommand(ATCommand.LinefeedsOff);
|
||||
|
||||
@ -72,7 +71,6 @@ namespace OBD.NET.Devices
|
||||
Logger?.WriteLine("Setting the Protocol to 'Auto' ...", OBDLogLevel.Debug);
|
||||
SendCommand(ATCommand.SetProtocolAuto);
|
||||
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
// DarthAffe 21.02.2017: This seems to happen sometimes, i don't know why - just retry.
|
||||
catch
|
||||
@ -87,29 +85,27 @@ namespace OBD.NET.Devices
|
||||
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 SendCommandAsync(ATCommand.SetProtocolAuto); //TODO rewrite
|
||||
|
||||
await Task.Delay(1000); //TODO CHECK
|
||||
|
||||
}
|
||||
// DarthAffe 21.02.2017: This seems to happen sometimes, i don't know why - just retry.
|
||||
catch
|
||||
@ -213,7 +209,7 @@ namespace OBD.NET.Devices
|
||||
if (sendCloseProtocol)
|
||||
{
|
||||
SendCommand(ATCommand.CloseProtocol);
|
||||
Thread.Sleep(500);
|
||||
//Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Devices.Bluetooth.Rfcomm;
|
||||
using Windows.Networking.Sockets;
|
||||
@ -16,6 +17,9 @@ namespace OBD.NET.Communication
|
||||
|
||||
private readonly byte[] _readBuffer = new byte[1024];
|
||||
private readonly StringBuilder _lineBuffer = new StringBuilder();
|
||||
|
||||
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
|
||||
private Task _readerTask;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this connection is open.
|
||||
@ -25,19 +29,30 @@ namespace OBD.NET.Communication
|
||||
/// </value>
|
||||
public bool IsOpen { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a full line was received
|
||||
/// </summary>
|
||||
public event EventHandler<string> MessageReceived;
|
||||
|
||||
/// <summary>
|
||||
/// Connects the serial port.
|
||||
/// </summary>
|
||||
/// <exception cref="System.NotSupportedException">Synchronous operations not supported</exception>
|
||||
public void Connect()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
throw new NotSupportedException("Synchronous operations not supported");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connects the serial port asynchronously
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task ConnectAsync()
|
||||
{
|
||||
var services = await Windows.Devices.Enumeration.DeviceInformation
|
||||
.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
|
||||
|
||||
//use first serial service
|
||||
if (services.Count > 0)
|
||||
{
|
||||
// Initialize the target Bluetooth BR device
|
||||
@ -49,27 +64,45 @@ namespace OBD.NET.Communication
|
||||
await _socket.ConnectAsync(service.ConnectionHostName,
|
||||
service.ConnectionServiceName);
|
||||
_writer = new DataWriter(_socket.OutputStream);
|
||||
StartReader();
|
||||
_readerTask = StartReader();
|
||||
IsOpen = true;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void StartReader()
|
||||
/// <summary>
|
||||
/// Writes the specified text to the serial connection
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
/// <exception cref="System.NotImplementedException">Synchronous operations not supported</exception>
|
||||
public void Write(string text)
|
||||
{
|
||||
Task.Factory.StartNew(async () =>
|
||||
{
|
||||
throw new NotImplementedException("Synchronous operations not supported");
|
||||
}
|
||||
|
||||
var buffer = _readBuffer.AsBuffer();
|
||||
while (true)
|
||||
{
|
||||
var readData = await _socket.InputStream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.Partial);
|
||||
SerialPortOnDataReceived(readData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified text to the serial connection asynchronously
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
/// <returns></returns>
|
||||
public async Task WriteAsync(string text)
|
||||
{
|
||||
_writer.WriteString(text);
|
||||
await _writer.StoreAsync();
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
});
|
||||
private Task StartReader()
|
||||
{
|
||||
return Task.Factory.StartNew(async () =>
|
||||
{
|
||||
|
||||
var buffer = _readBuffer.AsBuffer();
|
||||
while (!_cancellationTokenSource.IsCancellationRequested)
|
||||
{
|
||||
var readData = await _socket.InputStream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.Partial);
|
||||
SerialPortOnDataReceived(readData);
|
||||
}
|
||||
}, _cancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
private void SerialPortOnDataReceived(IBuffer buffer)
|
||||
@ -108,19 +141,11 @@ namespace OBD.NET.Communication
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cancellationTokenSource?.Cancel();
|
||||
_readerTask?.Wait();
|
||||
_socket?.Dispose();
|
||||
}
|
||||
|
||||
public void Write(string text)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task WriteAsync(string text)
|
||||
{
|
||||
_writer.WriteString(text);
|
||||
await _writer.StoreAsync();
|
||||
await _writer.FlushAsync();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -106,12 +106,12 @@ namespace OBD.NET.Communication
|
||||
|
||||
public Task ConnectAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
throw new NotSupportedException("Asynchronous operations not supported");
|
||||
}
|
||||
|
||||
public Task WriteAsync(string text)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
throw new NotSupportedException("Asynchronous operations not supported");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user