omni for testing
This commit is contained in:
@@ -25,8 +25,6 @@ namespace OmniPlus
|
||||
|
||||
public Byte[] Send(params Byte[] request)
|
||||
{
|
||||
this.InvokeLogMessage(">>> PLAIN" + BitConverter.ToString(request));
|
||||
|
||||
if (!this.IsOpen)
|
||||
{
|
||||
this.OpenII();
|
||||
@@ -44,15 +42,14 @@ namespace OmniPlus
|
||||
var asyncState = new IRDAAsyncState
|
||||
{
|
||||
Request = request,
|
||||
Stream = this.Stream
|
||||
Stream = this.Stream,
|
||||
Logger = this.InvokeLogMessage
|
||||
};
|
||||
|
||||
var lock_object = new object();
|
||||
|
||||
lock (lock_object)
|
||||
{
|
||||
Task.Factory.StartNew(asyncState.BeginReadAsync, asyncState.CancellationToken);
|
||||
|
||||
asyncState.BeginWriteAsync(request);
|
||||
}
|
||||
|
||||
@@ -149,22 +146,24 @@ namespace OmniPlus
|
||||
public class IRDAAsyncState
|
||||
{
|
||||
private const int BUFFER_SIZE = 256;
|
||||
private const int MAX_REPEATS = 5;
|
||||
private const int MAX_REPEATS = 10;
|
||||
|
||||
private readonly ManualResetEventSlim manualResetEventSlim;
|
||||
private readonly CancellationTokenSource cancellationTokenSource;
|
||||
private readonly CancellationToken cancellationToken;
|
||||
|
||||
private Task readerTask;
|
||||
private int repeatsCount;
|
||||
|
||||
public IRDAAsyncState()
|
||||
{
|
||||
this.manualResetEventSlim = new ManualResetEventSlim();
|
||||
this.cancellationTokenSource = new CancellationTokenSource();
|
||||
this.CancellationToken = this.cancellationTokenSource.Token;
|
||||
this.Buffer = new Byte[BUFFER_SIZE];
|
||||
this.cancellationToken = this.cancellationTokenSource.Token;
|
||||
this.InputBuffer = new Byte[BUFFER_SIZE];
|
||||
}
|
||||
|
||||
public Int32 Repeats { get; private set; }
|
||||
|
||||
public Byte[] Buffer { get; private set; }
|
||||
public Byte[] InputBuffer { get; private set; }
|
||||
|
||||
public Byte[] Request { get; set; }
|
||||
|
||||
@@ -172,55 +171,51 @@ namespace OmniPlus
|
||||
|
||||
public Stream Stream { get; set; }
|
||||
|
||||
public CancellationToken CancellationToken { get; }
|
||||
public Action<String> Logger { get; set; }
|
||||
|
||||
internal void BeginReadAsync()
|
||||
{
|
||||
this.Repeats++;
|
||||
this.repeatsCount++;
|
||||
this.Response = null;
|
||||
|
||||
var asyncResult = this.Stream.BeginRead(this.Buffer, 0, this.Buffer.Length, this.EndReadAsync, this);
|
||||
var asyncResult = this.Stream.BeginRead(this.InputBuffer, 0, BUFFER_SIZE, this.EndReadAsync, this);
|
||||
|
||||
asyncResult.AsyncWaitHandle.WaitOne();
|
||||
|
||||
if (this.Response is null && this.Repeats <= MAX_REPEATS)
|
||||
if (this.Response is null && this.repeatsCount <= MAX_REPEATS)
|
||||
{
|
||||
this.BeginReadAsync();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.Response is null)
|
||||
else
|
||||
{
|
||||
this.Response = new Byte[] { };
|
||||
this.manualResetEventSlim.Set();
|
||||
}
|
||||
|
||||
this.manualResetEventSlim.Set();
|
||||
}
|
||||
|
||||
private void EndReadAsync(IAsyncResult asyncResult)
|
||||
{
|
||||
if (asyncResult.AsyncState is IRDAAsyncState asyncState)
|
||||
{
|
||||
lock (asyncState)
|
||||
{
|
||||
var bytesLength = asyncState.Stream.EndRead(asyncResult);
|
||||
var bytesLength = asyncState.Stream.EndRead(asyncResult);
|
||||
var lock_object = new object();
|
||||
|
||||
lock (lock_object)
|
||||
{
|
||||
if (this.Response is null)
|
||||
{
|
||||
var response = new Byte[bytesLength];
|
||||
|
||||
Array.Copy(this.Buffer, 0, response, 0, bytesLength);
|
||||
Buffer.BlockCopy(this.InputBuffer, 0, response, 0, bytesLength);
|
||||
|
||||
Console.WriteLine("<<< IRDA " + BitConverter.ToString(response));
|
||||
this.Logger?.Invoke("<<< IRDA: " + BitConverter.ToString(response));
|
||||
|
||||
response = response.FromIrdaMessage();
|
||||
|
||||
Console.WriteLine("<<< UI1236 " + BitConverter.ToString(response));
|
||||
this.Logger?.Invoke(" UI1236: " + BitConverter.ToString(response));
|
||||
|
||||
response = response.FromUI1236Message();
|
||||
|
||||
Console.WriteLine("<<< OMNI " + BitConverter.ToString(response));
|
||||
this.Logger?.Invoke(" OMNI: " + BitConverter.ToString(response));
|
||||
|
||||
if (response.Length != 0)
|
||||
{
|
||||
@@ -233,19 +228,17 @@ namespace OmniPlus
|
||||
|
||||
internal void BeginWriteAsync(Byte[] request)
|
||||
{
|
||||
Console.WriteLine(">>> OMNI " + BitConverter.ToString(request));
|
||||
this.Logger?.Invoke(">>> OMNI: " + BitConverter.ToString(request));
|
||||
|
||||
request = request.ToUI1236Message();
|
||||
|
||||
Console.WriteLine(">>> UI1236 " + BitConverter.ToString(request));
|
||||
this.Logger?.Invoke(" UI1236: " + BitConverter.ToString(request));
|
||||
|
||||
request = request.ToIrdaMessage();
|
||||
|
||||
Console.WriteLine(">>> IRDA " + BitConverter.ToString(request));
|
||||
this.Logger?.Invoke(" IRDA: " + BitConverter.ToString(request));
|
||||
|
||||
request = request
|
||||
.ToUI1236Message()
|
||||
.ToIrdaMessage();
|
||||
this.readerTask = Task.Factory.StartNew(this.BeginReadAsync, this.cancellationToken);
|
||||
|
||||
var asyncResult = this.Stream.BeginWrite(request, 0, request.Length, this.EndWriteAsync, this);
|
||||
|
||||
|
||||
@@ -45,16 +45,14 @@ namespace OmniPlus
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
this.LogMessage?.Invoke(e.Message);
|
||||
}
|
||||
|
||||
Console.WriteLine("... Port disposed");
|
||||
this.LogMessage?.Invoke($"Port {this.Options.PortName} disposed");
|
||||
}
|
||||
|
||||
protected virtual void OpenII()
|
||||
{
|
||||
this.Dispose();
|
||||
|
||||
try
|
||||
{
|
||||
this.serialPort = new SerialPort
|
||||
@@ -75,7 +73,7 @@ namespace OmniPlus
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
this.LogMessage?.Invoke(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,13 +140,13 @@ namespace OmniPlus
|
||||
{
|
||||
this.Dispose();
|
||||
|
||||
Console.WriteLine("... Port disposed internal");
|
||||
this.LogMessage?.Invoke("... Port disposed internal");
|
||||
}
|
||||
|
||||
private void Port_ErrorReceived(Object sender, SerialErrorReceivedEventArgs args)
|
||||
{
|
||||
Console.WriteLine("... Port error received");
|
||||
Console.WriteLine($"... {args.EventType}");
|
||||
this.LogMessage?.Invoke("... Port error received");
|
||||
this.LogMessage?.Invoke($"... {args.EventType}");
|
||||
|
||||
if (!this.serialPort.IsOpen)
|
||||
{
|
||||
@@ -242,7 +240,7 @@ namespace OmniPlus
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
this.LogMessage?.Invoke(e.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,7 +252,7 @@ namespace OmniPlus
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
this.LogMessage?.Invoke(e.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ namespace OmniPlus
|
||||
{
|
||||
public sealed class OmniPlusConnection
|
||||
{
|
||||
private uint secondsToWait;
|
||||
private Func<bool> connected;
|
||||
private readonly IrdaConnection irdaConnection;
|
||||
private readonly UniProConnection uniProConnection;
|
||||
|
||||
Reference in New Issue
Block a user