OMNI: Thread with task replaced.
This commit is contained in:
@@ -17,7 +17,7 @@ namespace OmniPlus
|
||||
|
||||
private SerialPort serialPort;
|
||||
private SerialPortState state;
|
||||
private Thread readingThread;
|
||||
private bool buisy;
|
||||
|
||||
protected SerialPortConnection()
|
||||
=> this.Options = new SerialPortOptions();
|
||||
@@ -28,15 +28,6 @@ namespace OmniPlus
|
||||
|
||||
public virtual void Close()
|
||||
{
|
||||
try
|
||||
{
|
||||
this.readingThread?.Abort();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.LogMessage?.Invoke(e.ToString());
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (this.serialPort != null)
|
||||
@@ -145,14 +136,7 @@ namespace OmniPlus
|
||||
+ $", {this.Options.BaudRate}"
|
||||
+ $", {this.Options.DataBits}"
|
||||
+ $", {this.Options.Parity}"
|
||||
+ $", {this.Options.StopBits}");
|
||||
|
||||
if (this.serialPort.IsOpen)
|
||||
{
|
||||
this.readingThread = new Thread(this.SerialPortConnectionBeginRead);
|
||||
this.readingThread.Start(this.serialPort.BaseStream);
|
||||
}
|
||||
|
||||
+ $", {this.Options.StopBits}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -168,61 +152,20 @@ namespace OmniPlus
|
||||
protected void InvokeLogMessage(string message)
|
||||
=> this.LogMessage?.Invoke(message);
|
||||
|
||||
protected void Write(params byte[] message)
|
||||
protected void Write(params byte[] bytes)
|
||||
{
|
||||
if (this.Connected && message.Length > 0)
|
||||
this.BeginWaitingResponseAsync();
|
||||
|
||||
lock (this.serialPort)
|
||||
{
|
||||
lock (this.serialPort)
|
||||
{
|
||||
this.serialPort.Write(message, 0, message.Length);
|
||||
}
|
||||
this.serialPort.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
this.LogMessage?.Invoke($"TX: {BitConverter.ToString(bytes)}");
|
||||
}
|
||||
|
||||
protected abstract void SerialPortConnection_DataReceived(byte[] bytes);
|
||||
|
||||
private void SerialPortConnectionBeginRead(object parameter)
|
||||
{
|
||||
if (parameter is Stream stream && stream.CanRead)
|
||||
{
|
||||
byte[] data;
|
||||
byte[] buffer = new byte[128];
|
||||
AsyncCallback endReadHandler = _asyncResult =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_asyncResult.AsyncState is Stream _stream)
|
||||
{
|
||||
var bytesReaded = _stream.EndRead(_asyncResult);
|
||||
|
||||
if (bytesReaded > 0)
|
||||
{
|
||||
data = new byte[bytesReaded];
|
||||
|
||||
Array.Copy(buffer, 0, data, 0, bytesReaded);
|
||||
|
||||
this.DataReceived?.Invoke(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.LogMessage?.Invoke(e.ToString());
|
||||
}
|
||||
};
|
||||
|
||||
while (stream.CanRead)
|
||||
{
|
||||
stream
|
||||
.BeginRead(buffer, 0, buffer.Length, endReadHandler, stream)
|
||||
.AsyncWaitHandle
|
||||
.WaitOne();
|
||||
}
|
||||
}
|
||||
|
||||
this.Close();
|
||||
}
|
||||
|
||||
protected void SerialPortConnection_LogMessage(string message)
|
||||
{
|
||||
try
|
||||
@@ -273,22 +216,40 @@ namespace OmniPlus
|
||||
|
||||
this.Close();
|
||||
}
|
||||
|
||||
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs args)
|
||||
{
|
||||
var bytes = new byte[this.serialPort.BytesToRead];
|
||||
|
||||
/// <summary>
|
||||
/// TODO: investigate the usage of port.Read... (may be its wrong)
|
||||
/// New approach implemented whit the base stream. See above!
|
||||
/// </summary>
|
||||
//private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs args)
|
||||
//{
|
||||
// if (this.Connected && sender is SerialPort port)
|
||||
// {
|
||||
// var bytes = new byte[port.BytesToRead];
|
||||
this.serialPort.Read(bytes, 0, this.serialPort.BytesToRead);
|
||||
|
||||
// port.Read(bytes, 0, port.BytesToRead);
|
||||
this.DataReceived?.Invoke(bytes);
|
||||
this.LogMessage?.Invoke($"RX: {BitConverter.ToString(bytes)}");
|
||||
}
|
||||
|
||||
// this.DataReceived?.Invoke(bytes);
|
||||
// this.LogMessage?.Invoke($"RX: {BitConverter.ToString(bytes)}");
|
||||
// }
|
||||
//}
|
||||
private void BeginWaitingResponseAsync()
|
||||
{
|
||||
var buffer = new byte[1024];
|
||||
var stream = this.serialPort.BaseStream;
|
||||
|
||||
this.serialPort.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult asyncResult)
|
||||
{
|
||||
try
|
||||
{
|
||||
var actualLength = this.serialPort.BaseStream.EndRead(asyncResult);
|
||||
var received = new byte[actualLength];
|
||||
|
||||
Buffer.BlockCopy(buffer, 0, received, 0, actualLength);
|
||||
|
||||
this.DataReceived?.Invoke(received);
|
||||
this.LogMessage?.Invoke($"RX: {BitConverter.ToString(received)}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.StateChanged?.Invoke(this.state, $"{this.Options.PortName}: {e}");
|
||||
this.LogMessage?.Invoke(e.ToString());
|
||||
}
|
||||
}, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
this.IsValid = 3.19 <= this.BatteryVoltage.Volts && this.BatteryVoltage.Volts <= 3.75;
|
||||
this.IsValid = 3190 <= this.BatteryVoltage.Volts && this.BatteryVoltage.Volts <= 3750;
|
||||
|
||||
if (!this.IsValid)
|
||||
{
|
||||
|
||||
@@ -157,7 +157,7 @@ namespace OmniPlus
|
||||
return new InputVolume(response);
|
||||
}
|
||||
|
||||
public ChamberCalibrationRequest StartChamberCalibration(ushort rotations, ushort duration)
|
||||
public ChamberCalibrationResponse StartChamberCalibration(ushort rotations, ushort duration)
|
||||
{
|
||||
var response = this.MessageHandler.Invoke(new ChamberCalibrationRequest
|
||||
{
|
||||
@@ -166,7 +166,7 @@ namespace OmniPlus
|
||||
Control = ChamberCalibrationRequest.ACTIVATE_TEST
|
||||
}[DEVICE_SPECIFIC, CHAMBER_CALIBRATION]);
|
||||
|
||||
return new ChamberCalibrationRequest(response);
|
||||
return new ChamberCalibrationResponse(response);
|
||||
}
|
||||
|
||||
public ChamberCalibrationResponse ViewChamberCalibration()
|
||||
|
||||
@@ -111,7 +111,7 @@ namespace OmniPlus
|
||||
position++;
|
||||
}
|
||||
|
||||
this.database.LoadOmniPlusType(this.size, this.variant, this.omniType);
|
||||
this.database.LoadOmniDefaults(this.size, this.variant, this.omniType);
|
||||
}
|
||||
|
||||
public void CancelExecution()
|
||||
|
||||
@@ -5,14 +5,14 @@ using System.Data.SqlClient;
|
||||
|
||||
namespace OmniPlus
|
||||
{
|
||||
internal class OmniPlusDatabase
|
||||
public class OmniPlusDatabase
|
||||
{
|
||||
private readonly string connectionString;
|
||||
|
||||
public OmniPlusDatabase(string omniDatabase)
|
||||
=> this.connectionString = omniDatabase;
|
||||
|
||||
internal void AddOrUpdate(int position, SystemParameters parameters)
|
||||
public void AddOrUpdate(int position, SystemParameters parameters)
|
||||
{
|
||||
using (var sqlConnection = new SqlConnection(this.connectionString))
|
||||
{
|
||||
@@ -49,7 +49,7 @@ namespace OmniPlus
|
||||
}
|
||||
}
|
||||
|
||||
internal void AddOrUpdate(int position, ConfigurationParameters parameters)
|
||||
public void AddOrUpdate(int position, ConfigurationParameters parameters)
|
||||
{
|
||||
using (var sqlConnection = new SqlConnection(this.connectionString))
|
||||
{
|
||||
@@ -102,7 +102,7 @@ namespace OmniPlus
|
||||
}
|
||||
}
|
||||
|
||||
internal void LoadOmniPlusType(byte size, string variant, OmniPlusType omniType)
|
||||
public void LoadOmniDefaults(byte size, string variant, OmniPlusType omniType)
|
||||
{
|
||||
using (var sqlConnection = new SqlConnection(this.connectionString))
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace OmniPlus
|
||||
{
|
||||
internal struct OmniPlusTestPoint : IComparable<OmniPlusTestPoint>
|
||||
public struct OmniPlusTestPoint : IComparable<OmniPlusTestPoint>
|
||||
{
|
||||
public double GalPerMin { get; set; }
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
|
||||
namespace OmniPlus
|
||||
{
|
||||
internal class OmniPlusType
|
||||
public class OmniPlusType
|
||||
{
|
||||
public byte OmniId { get; set; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user