temp commit: branch changing

This commit is contained in:
Stoyan Zlatev
2024-07-30 14:02:14 +02:00
parent 44688c5dc4
commit d4f6fa1eb1
76 changed files with 2037 additions and 1120 deletions
@@ -64,10 +64,12 @@
<Compile Include="SIRTConstants.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SIRTTask.cs" />
<Compile Include="SIRTTask[TRequest].cs" />
<Compile Include="Tasks\ActivateSIRTTask.cs" />
<Compile Include="Tasks\IdentifySIRTTask.cs" />
<Compile Include="Tasks\ReadPamPoolTask.cs" />
<Compile Include="Tasks\WritePamTask.cs" />
<Compile Include="Tasks\RPAMTask.cs" />
<Compile Include="Tasks\WPAMTask.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\Utils\Crc16Ccitt\Crc16Ccitt.csproj">
@@ -0,0 +1,57 @@
namespace Common.Hardware.Interfaces.Ports.SIRT
{
using Common.Hardware.Interfaces.Ports.SIRT.Responses;
using System;
public abstract class SIRTTask<TRequest> : SIRTTask where TRequest : SIRTRequest, new()
{
protected readonly TRequest request;
protected SIRTTask(UInt32 timeoutSeconds = TIMEOUT_SECONDS)
{
this.request = new TRequest();
this.Cmd = this.request.message.Cmd;
}
protected SIRTTask(TRequest request, UInt32 timeoutSeconds = TIMEOUT_SECONDS)
: this(timeoutSeconds)
=> this.request = request;
public override UInt32 Address
{
get => this.request.Address;
set => this.request.Address = value;
}
public override Byte[] Payload
{
get => this.request.Payload;
set => this.request.Payload = value;
}
internal override void BeginAddResponse(AcknOrAnswerResponse response)
{
this.BeginAddResponse();
var done = this.AcknOrAnswerReceived(response);
this.EndAddResponse(done);
}
internal override void BeginAddResponse(FromAirResponse response)
{
this.BeginAddResponse();
var done = this.FromAirReceived(response);
this.EndAddResponse(done);
}
protected virtual Boolean AcknOrAnswerReceived(AcknOrAnswerResponse response) => false;
protected virtual Boolean FromAirReceived(FromAirResponse response) => false;
protected override Byte[] GetRequestBytes() => this.request;
}
}
@@ -27,22 +27,29 @@
this.DataReceived += this.SirtBroadcaster_DataReceived;
}
public String SIRTID { get; private set; }
public UInt16 Frequency { get; private set; }
public event Action<String, Int32> Activated;
public event Action<UInt32> EndpointAvailable;
public event Action<UInt32[]> PamPoolChanged;
public String SIRTID { get; private set; }
public UInt16 Frequency { get; private set; }
public void Run(SIRTTask task)
{
task.AddressChanged += (oldAddress, newAddress) =>
{
task.Done += _ =>
{
/// Remove old address from the tasks pool
this.tasks.TryRemove(oldAddress, out var _);
};
/// Add the new address to the tasks pool
this.tasks
.GetOrAdd(newAddress, new Queue<SIRTTask>())
.Enqueue(task);
/// Remove old address from the pam pool
this.Write(new WritePamRequest
{
DelPamAdr = true,
@@ -116,6 +123,8 @@
this.Run(identificationTask);
manuelResetEventSlim.Wait();
manuelResetEventSlim.Reset();
this.Activated?.Invoke(this.SIRTID, this.Frequency);
}
private void SirtBroadcaster_DataReceived(Byte[] data)
@@ -160,8 +169,6 @@
{
if (state is SIRTBroadcaster broadcaster)
{
var awaiter = new ManualResetEventSlim();
/// Loop while cancellation requested ...
while (!broadcaster.cancellationToken.IsCancellationRequested)
{
@@ -190,11 +197,12 @@
/// When current task is not running and is not cancelled or done ...
if (!current.IsRunning)
{
var notWritePamTask = current.Type != SIRTConstants.WritePam;
var notInPamPool = !broadcaster.pamPool.Values.ToArray().Any(x => x == current.Address);
var notWritePamTask = current.Cmd != SIRTConstants.WritePam;
var pamPoolNotFull = !broadcaster.pamPool.Values.Any(x => x == 0);
var notInPamPool = !broadcaster.pamPool.Values.Any(x => x == current.Address);
/// ... and also is write to pam task and is not into the pam pool ...
if (notWritePamTask || notInPamPool)
if (notWritePamTask || (pamPoolNotFull && notInPamPool))
{
/// ... mark as running ...
current.IsRunning = true;
@@ -207,16 +215,7 @@
/// Break and go to the next address with tasks.
break;
}
/// Break in case the pam pool is full.
if (pamPool.Count > 0 && !pamPool.Values.Any(x => x == 0))
{
break;
}
}
awaiter.Wait(250, broadcaster.cancellationToken);
awaiter.Reset();
}
}
}, this, this.cancellationToken)
@@ -249,8 +248,6 @@
task.Done += _ => awaiter.Set();
task.AddressesParsed += addresses =>
{
broadcaster.PamPoolChanged?.Invoke(addresses);
var length = addresses.Length;
for (Byte i = 0; i < length; i++)
@@ -265,10 +262,7 @@
broadcaster.Run(task);
awaiter.Wait(broadcaster.cancellationToken);
awaiter.Reset();
awaiter.Wait(500, broadcaster.cancellationToken);
awaiter.Wait();
awaiter.Reset();
}
}
@@ -1,67 +1,60 @@
namespace Common.Hardware.Interfaces.Ports.SIRT
{
using Common.Hardware.Interfaces.Ports.SIRT.Requests;
using Common.Hardware.Interfaces.Ports.SIRT.Responses;
using System;
public abstract class SIRTTask
{
protected readonly SIRTRequest request;
private readonly UInt32 timeout;
protected const UInt32 TIMEOUT_SECONDS = 60;
protected readonly UInt32 timeout;
private WriteRegRequest writeRegRequest;
private ReadRegRequest readRegRequest;
private ReadPamRequest readPamRequest;
public SIRTTask(SIRTRequest request, UInt32 timeout = 10)
public SIRTTask(UInt32 timeoutSeconds = TIMEOUT_SECONDS)
{
this.request = request;
this.timeout = timeout;
this.Type = this.request.message.Cmd;
this.Address = this.request.Address;
this.timeout = timeoutSeconds;
this.Timestamp = DateTime.MinValue;
}
protected SIRTTask(WriteRegRequest writeRegRequest)
{
this.writeRegRequest = writeRegRequest;
}
protected SIRTTask(ReadRegRequest readRegRequest)
{
this.readRegRequest = readRegRequest;
}
protected SIRTTask(ReadPamRequest readPamRequest)
{
this.readPamRequest = readPamRequest;
}
public event Action<SIRTTask> Done;
public event Action<SIRTTask> Cancelled;
public event Action<UInt32, UInt32> AddressChanged;
public Byte Type { get; }
public virtual UInt32 Address { get; set; }
public Boolean IsRunning { get; internal set; }
public virtual Byte[] Payload { get; set; }
public Boolean IsRunning { get; set; }
public Byte Cmd { get; protected set; }
public DateTime Timestamp { get; protected set; }
public Boolean IsDone { get; protected set; }
public Boolean IsCancelled { get; protected set; }
public DateTime? Timestamp { get; protected set; }
internal abstract void BeginAddResponse(AcknOrAnswerResponse response);
internal void BeginAddResponse(AcknOrAnswerResponse response)
{
if (this.CanReceiveResponses())
{
var done = this.AcknOrAnswerReceived(response);
this.EndAddResponse(done);
}
}
internal void BeginAddResponse(FromAirResponse response)
{
if (this.CanReceiveResponses())
{
var done = this.FromAirReceived(response);
this.EndAddResponse(done);
}
}
protected virtual Boolean AcknOrAnswerReceived(AcknOrAnswerResponse response)
{
return false;
}
protected virtual Boolean FromAirReceived(FromAirResponse response)
{
return false;
}
internal abstract void BeginAddResponse(FromAirResponse response);
protected void NotifyCancelled()
{
@@ -78,38 +71,30 @@
}
protected void NotifyAddressChanged(UInt32 oldAddress, UInt32 newAddress)
=> this.AddressChanged?.Invoke(oldAddress, newAddress);
protected void BeginAddResponse()
{
this.Address = newAddress;
this.AddressChanged?.Invoke(oldAddress, newAddress);
}
protected virtual Byte[] GetRequestBytes() => this.request;
private Boolean CanReceiveResponses()
{
var canReceiveResponses = !this.IsDone && !this.IsCancelled;
if (canReceiveResponses && this.Timestamp is null)
if (this.Timestamp == DateTime.MinValue)
{
this.Timestamp = DateTime.Now;
}
return canReceiveResponses;
}
private void EndAddResponse(Boolean isDone)
protected void EndAddResponse(Boolean isDone)
{
if (isDone)
{
this.NotifyDone();
}
else if ((DateTime.Now - this.Timestamp.Value).TotalSeconds > this.timeout)
else if ((DateTime.Now - this.Timestamp).TotalSeconds > this.timeout)
{
this.NotifyCancelled();
}
}
public static implicit operator Byte[] (SIRTTask task) => task.request;
protected abstract Byte[] GetRequestBytes();
public static implicit operator Byte[] (SIRTTask task) => task.GetRequestBytes();
}
}
@@ -36,6 +36,21 @@
}
}) { }
protected override Boolean AcknOrAnswerReceived(AcknOrAnswerResponse response) => response.IsValid;
protected Boolean AcknOrAnswerReceived(AcknOrAnswerResponse response) => response.IsValid;
protected override Byte[] GetRequestBytes()
{
throw new NotImplementedException();
}
internal override void BeginAddResponse(AcknOrAnswerResponse response)
{
throw new NotImplementedException();
}
internal override void BeginAddResponse(FromAirResponse response)
{
throw new NotImplementedException();
}
}
}
@@ -15,7 +15,7 @@
public event Action<String> Identified;
protected override Boolean AcknOrAnswerReceived(AcknOrAnswerResponse response)
protected Boolean AcknOrAnswerReceived(AcknOrAnswerResponse response)
{
if (!response.IsValid)
{
@@ -36,5 +36,20 @@
return identified;
}
protected override Byte[] GetRequestBytes()
{
throw new NotImplementedException();
}
internal override void BeginAddResponse(AcknOrAnswerResponse response)
{
throw new NotImplementedException();
}
internal override void BeginAddResponse(FromAirResponse response)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,19 @@
namespace Common.Hardware.Interfaces.Ports.SIRT.Tasks
{
using Common.Hardware.Interfaces.Ports.SIRT.Requests;
using System;
public class RPAMTask : SIRTTask<ReadPamRequest>
{
public RPAMTask(UInt32 timeoutSeconds = TIMEOUT_SECONDS) : base(timeoutSeconds)
{
}
public Boolean PamStruct
{
get => this.request.PamStruct;
set => this.request.PamStruct = value;
}
}
}
@@ -14,7 +14,7 @@
public event Action<UInt32[]> AddressesParsed;
protected override Boolean AcknOrAnswerReceived(AcknOrAnswerResponse response)
protected Boolean AcknOrAnswerReceived(AcknOrAnswerResponse response)
{
if (!response.IsValid)
{
@@ -55,5 +55,20 @@
return parsed;
}
protected override Byte[] GetRequestBytes()
{
throw new NotImplementedException();
}
internal override void BeginAddResponse(AcknOrAnswerResponse response)
{
throw new NotImplementedException();
}
internal override void BeginAddResponse(FromAirResponse response)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,56 @@
namespace Common.Hardware.Interfaces.Ports.SIRT.Tasks
{
using Common.Hardware.Interfaces.Ports.SIRT.Requests;
using System;
public class WPAMTask : SIRTTask<WritePamRequest>
{
public WPAMTask(UInt32 timeoutSeconds = TIMEOUT_SECONDS) : base(timeoutSeconds)
{
}
public Boolean WakeUpModul
{
get => this.request.WakeUpModul;
set => this.request.WakeUpModul = value;
}
public Boolean WakeUpCmd
{
get => this.request.WakeUpCmd;
set => this.request.WakeUpCmd = value;
}
public Boolean IgnoreTimeout
{
get => this.request.IgnoreTimeout;
set => this.request.IgnoreTimeout = value;
}
public Boolean DelPamSemi
{
get => this.request.DelPamSemi;
set => this.request.DelPamSemi = value;
}
public Boolean DelPamTx
{
get => this.request.DelPamTx;
set => this.request.DelPamTx = value;
}
public Boolean DelPamAdr
{
get => this.request.DelPamAdr;
set => this.request.DelPamAdr = value;
}
public Boolean DelPamAll
{
get => this.request.DelPamAll;
set => this.request.DelPamAll = value;
}
}
}
@@ -1,15 +0,0 @@
namespace Common.Hardware.Interfaces.Ports.SIRT.Tasks
{
using System;
public class WritePamTask : SIRTTask
{
public WritePamTask(SIRTRequest request) : base(request)
{
}
public WritePamTask(SIRTRequest request, UInt32 timeout) : base(request, timeout)
{
}
}
}
@@ -42,7 +42,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="IRDASerialPort.cs" />
<Compile Include="SerialPort.cs" />
<Compile Include="SerialPortBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SerialPortBaudRate.cs" />
@@ -52,6 +51,7 @@
<Compile Include="SerialPortSettings.cs" />
<Compile Include="SerialPortStopBits.cs" />
<Compile Include="SIRTSerialPort.cs" />
<Compile Include="UI1236Control.cs" />
<Compile Include="UniProSerialPort.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
@@ -1,9 +1,8 @@
namespace Common.Hardware.Interfaces.Ports
{
using System;
using System.Threading;
public class IRDASerialPort : SerialPort
public class IRDASerialPort : SerialPortBase
{
public IRDASerialPort(SerialPortSettings settings) : base(settings)
{
@@ -15,33 +14,24 @@
if (data.TryParseToIrda(out var bytes))
{
for (var i = 0; i < 3; i++)
var timeout = 3;
for (var i = 0; i < timeout; i++)
{
this.serialPortBase.Write(new Byte[] { 0x01, 0x01, 0x01, 0x01 }, 0, 4);
Thread.Sleep(1);
this.serialPortBase.Write(bytes, 0, bytes.Length);
var timeout = 3;
this.Write(new Byte[] { 0x01, 0x01, 0x01, 0x01 }, 0, 4);
this.Write(bytes, 0, bytes.Length);
var answerd = false;
var buffer = new Byte[0];
while (timeout-- >= 0)
{
Thread.Sleep(50);
var _bytes = this.serialPortBase.Read();
var _bytes = this.Read();
var bytesLength = _bytes.Length;
var bufferLength = buffer.Length;
Thread.Sleep(50);
Array.Resize(ref buffer, bufferLength + bytesLength);
Thread.Sleep(50);
Array.Copy(_bytes, 0, buffer, bufferLength, bytesLength);
Thread.Sleep(50);
if (buffer.TryParseFromIrda(out _data))
{
@@ -1,37 +0,0 @@
namespace Common.Hardware.Interfaces.Ports
{
using System;
public abstract class SerialPort
{
internal readonly SerialPortBase serialPortBase;
public SerialPort(SerialPortSettings settings)
=> this.serialPortBase = new SerialPortBase(settings);
public event Action<String> Logging
{
add => this.serialPortBase.Logging += value;
remove => this.serialPortBase.Logging -= value;
}
public Boolean IsOpen => this.serialPortBase.IsOpen;
public virtual void Close() => this.serialPortBase.Close();
public virtual void EnsureConnected()
{
if (!this.serialPortBase.IsOpen)
{
this.serialPortBase.Close();
this.serialPortBase.Open();
}
}
protected void Notify(String message) => this.serialPortBase.Notify(message);
public virtual void Open() => this.serialPortBase.Open();
public abstract Byte[] Write(params Byte[] data);
}
}
@@ -2,10 +2,10 @@
{
using System;
using System.Collections.Concurrent;
using System.Threading;
using SystemSerialPort = System.IO.Ports.SerialPort;
internal class SerialPortBase
public abstract class SerialPortBase
{
private static readonly ConcurrentDictionary<String, SystemSerialPort> serialPorts
= new ConcurrentDictionary<String, SystemSerialPort>();
@@ -30,7 +30,7 @@
? serialPort?.ReadBufferSize ?? default(Int32)
: default(Int32);
public void Close()
public virtual void Close()
{
try
{
@@ -60,7 +60,16 @@
}
}
public void Open()
public virtual void EnsureConnected()
{
if (!this.IsOpen)
{
this.Close();
this.Open();
}
}
public virtual void Open()
{
try
{
@@ -96,33 +105,6 @@
}
}
public Byte[] Read()
{
var bytes = new Byte[0];
if (serialPorts.TryGetValue(this.PortName, out var serialPort) && serialPort.IsOpen)
{
var size = this.ReadBufferSize;
var buffer = new Byte[size];
try
{
var copyLength = serialPort.Read(buffer, 0, size);
Array.Resize(ref bytes, copyLength);
Array.Copy(buffer, 0, bytes, 0, copyLength);
this.Notify($"RX|{BitConverter.ToString(bytes)}");
}
catch (Exception e)
{
this.Notify(e.ToString());
}
}
return bytes;
}
public override String ToString()
=> serialPorts.TryGetValue(this.PortName, out var serialPort)
? $"{serialPort.PortName}, " +
@@ -136,7 +118,51 @@
$"{this.settings.Parity}, " +
$"{this.settings.StopBits}";
public void Write(Byte[] bytes, Int32 start, Int32 length)
public virtual Byte[] Write(params Byte[] data)
{
this.Write(data, 0, data.Length);
return data;
}
protected void Notify(String message) => this.Logging?.Invoke(message);
protected Byte[] Read(Int32 sleep = 100)
{
Thread.Sleep(sleep);
var bytes = new Byte[0];
if (serialPorts.TryGetValue(this.PortName, out var serialPort) && serialPort.IsOpen)
{
var size = this.ReadBufferSize;
var buffer = new Byte[size];
try
{
if (serialPort.IsOpen)
{
var copyLength = serialPort.Read(buffer, 0, size);
Array.Resize(ref bytes, copyLength);
Array.Copy(buffer, 0, bytes, 0, copyLength);
if (this.settings.RXLoggingEnabled)
{
this.Notify($"RX|{BitConverter.ToString(bytes)}");
}
}
}
catch (Exception e)
{
this.Notify(e.ToString());
}
}
return bytes;
}
protected void Write(Byte[] bytes, Int32 start, Int32 length, Int32 sleep = 1)
{
if (serialPorts.TryGetValue(this.PortName, out var serialPort) && serialPort.IsOpen)
{
@@ -144,16 +170,18 @@
{
serialPort.Write(bytes, start, length);
this.Notify($"TX|{BitConverter.ToString(bytes)}");
if (this.settings.TXLoggingEnabled)
{
this.Notify($"TX|{BitConverter.ToString(bytes)}");
}
}
catch (Exception e)
{
this.Notify(e.ToString());
}
}
}
public void Notify(String message)
=> this.Logging?.Invoke(message);
Thread.Sleep(sleep);
}
}
}
@@ -2,7 +2,7 @@
{
using System;
public static class SerialPortExtensions
public static partial class SerialPortExtensions
{
public const Byte UNIPRO_START = 0x53;
public const Byte UNIPRO_PC2REG = 0x57;
@@ -257,7 +257,7 @@
/// <para>n-2. .... - Checksum 0 </para>
/// <para>n-1. .... - Checksum 1 </para>
/// </summary>
public static Boolean FromUI1236(this Byte[] bytes, out Byte[] data)
public static Boolean TryParseFromUI1236(this Byte[] bytes, out Byte[] data)
{
data = new Byte[0];
@@ -288,7 +288,7 @@
return false;
}
var dataLength = bytes[lengthIndex];
var dataLength = bytes[lengthIndex] - 3; // 3 = protocol bytes length (start|control|length)
if (dataIndex + dataLength > bytesLength)
{
@@ -415,69 +415,5 @@
return BitConverter.GetBytes(crcSum);
}
internal class UI1236Control
{
private readonly Byte[] bits;
private UI1236Control(Byte[] bits)
=> this.bits = bits;
public Boolean E0Encryption => this.bits[0] == 1;
public Boolean E1Encryption => this.bits[1] == 1;
public Boolean E2Encryption => this.bits[2] == 1;
public Boolean ReturnResponse
{
get => this.bits[3] == 1;
set => this.bits[3] = (Byte)(value ? 1 : 0);
}
public Boolean NFNetwork => this.bits[4] == 1;
public Boolean MultipleCommands
{
get => this.bits[5] == 1;
set => this.bits[5] = (Byte)(value ? 1 : 0);
}
public Boolean LongControl
{
get => this.bits[6] == 1;
set => this.bits[6] = (Byte)(value ? 1 : 0);
}
public Boolean MultipleFrames
{
get => this.bits[7] == 1;
set => this.bits[7] = (Byte)(value ? 1 : 0);
}
public static implicit operator UI1236Control(Byte controlByte)
{
var bits = new Byte[8];
for (var i = 0; i < 8; i++)
{
bits[i] = (Byte)((controlByte >> i) & 1);
}
return new UI1236Control(bits);
}
public static implicit operator Byte(UI1236Control control)
{
var @Byte = default(Byte);
for (var i = 0; i < 8; i++)
{
@Byte ^= (Byte)(control.bits[i] << i);
}
return @Byte;
}
}
}
}
@@ -15,5 +15,9 @@
public SerialPortStopBits StopBits { get; set; }
public Int32 RWTimeout { get; set; } = 3000;
public Boolean RXLoggingEnabled { get; set; }
public Boolean TXLoggingEnabled { get; set; }
}
}
@@ -4,7 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
public class SIRTSerialPort : SerialPort
public class SIRTSerialPort : SerialPortBase
{
private CancellationTokenSource cancellationTokenSource;
private CancellationToken cancellationToken;
@@ -21,7 +21,6 @@
public override void Close()
{
this.StopListening();
base.Close();
}
@@ -29,27 +28,20 @@
{
base.EnsureConnected();
if (!this.listening)
if (this.listening)
{
this.StopListening();
this.StartListening();
}
this.StartListening();
}
public override void Open()
{
base.Open();
this.StartListening();
}
public override Byte[] Write(params Byte[] data)
{
this.serialPortBase.Write(data, 0, data.Length);
return data;
}
private void StartListening()
{
this.cancellationTokenSource = new CancellationTokenSource();
@@ -66,9 +58,9 @@
var buffer = new Byte[0];
while (true)
while (!sirtSerialPort.cancellationToken.IsCancellationRequested)
{
var bytesIn = sirtSerialPort.serialPortBase.Read();
var bytesIn = sirtSerialPort.Read(0);
var bytesInLength = bytesIn.Length;
var bufferLength = buffer.Length;
@@ -119,7 +111,7 @@
{
if (task.Exception != null)
{
sirtSerialPort.serialPortBase.Notify(task.Exception.ToString());
sirtSerialPort.Notify(task.Exception.ToString());
}
sirtSerialPort.listening = false;
@@ -137,8 +129,10 @@
}
catch (Exception e)
{
this.serialPortBase.Notify(e.ToString());
this.Notify(e.ToString());
}
Thread.Sleep(1);
}
}
}
@@ -0,0 +1,71 @@
namespace Common.Hardware.Interfaces.Ports
{
using System;
public static partial class SerialPortExtensions
{
internal class UI1236Control
{
private readonly Byte[] bits;
private UI1236Control(Byte[] bits)
=> this.bits = bits;
public Boolean E0Encryption => this.bits[0] == 1;
public Boolean E1Encryption => this.bits[1] == 1;
public Boolean E2Encryption => this.bits[2] == 1;
public Boolean ReturnResponse
{
get => this.bits[3] == 1;
set => this.bits[3] = (Byte)(value ? 1 : 0);
}
public Boolean NFNetwork => this.bits[4] == 1;
public Boolean MultipleCommands
{
get => this.bits[5] == 1;
set => this.bits[5] = (Byte)(value ? 1 : 0);
}
public Boolean LongControl
{
get => this.bits[6] == 1;
set => this.bits[6] = (Byte)(value ? 1 : 0);
}
public Boolean MultipleFrames
{
get => this.bits[7] == 1;
set => this.bits[7] = (Byte)(value ? 1 : 0);
}
public static implicit operator UI1236Control(Byte controlByte)
{
var bits = new Byte[8];
for (var i = 0; i < 8; i++)
{
bits[i] = (Byte)((controlByte >> i) & 1);
}
return new UI1236Control(bits);
}
public static implicit operator Byte(UI1236Control control)
{
var @Byte = default(Byte);
for (var i = 0; i < 8; i++)
{
@Byte ^= (Byte)(control.bits[i] << i);
}
return @Byte;
}
}
}
}
@@ -1,9 +1,8 @@
namespace Common.Hardware.Interfaces.Ports
{
using System;
using System.Threading;
public class UniProSerialPort : SerialPort
public class UniProSerialPort : SerialPortBase
{
public UniProSerialPort(SerialPortSettings settings) : base(settings)
{
@@ -15,31 +14,23 @@
if (data.TryParseToUniPro(out var bytes))
{
for (int i = 0; i < 3; i++)
var timeout = 3;
for (var i = 0; i < timeout; i++)
{
this.serialPortBase.Write(bytes, 0, bytes.Length);
this.Write(bytes, 0, bytes.Length);
Thread.Sleep(100);
var timeout = 3;
var answerd = false;
var buffer = new Byte[0];
while (timeout-- >= 0)
{
Thread.Sleep(100);
var _bytes = this.serialPortBase.Read();
var _bytes = this.Read();
var bytesLength = _bytes.Length;
var bufferLength = buffer.Length;
Thread.Sleep(100);
Array.Resize(ref buffer, bufferLength + bytesLength);
Thread.Sleep(100);
Array.Copy(_bytes, 0, buffer, bufferLength, bytesLength);
Thread.Sleep(100);
if (buffer.TryParseFromUniPro(out _data))
{