diff --git a/Common/CommonConsole/Program.cs b/Common/CommonConsole/Program.cs
index 28976e38..d5d39fa9 100644
--- a/Common/CommonConsole/Program.cs
+++ b/Common/CommonConsole/Program.cs
@@ -1,16 +1,22 @@
-namespace CommonConsole
-{
- using OmniPlus;
+using OmniPlus;
+using OmniPlus.Features;
+using System;
+namespace CommonConsole
+{
public static class Program
{
public static void Main()
{
var connection = new OmniIrdaConnection("COM36");
+ connection.StateChenged += Console.WriteLine;
connection.Open();
- var alarmState = connection.ViewAlarmsState();
+ for (int i = 0; i < 1000; i++)
+ {
+ var readings = connection.ViewReadings();
+ }
connection.Close();
}
diff --git a/Common/OmniPlus/InspectionUI/App.xaml.cs b/Common/OmniPlus/InspectionUI/App.xaml.cs
index 52ee3854..959eb971 100644
--- a/Common/OmniPlus/InspectionUI/App.xaml.cs
+++ b/Common/OmniPlus/InspectionUI/App.xaml.cs
@@ -10,9 +10,7 @@
internal static event Action Exiting;
- protected override void OnExit(ExitEventArgs e)
- {
- Exiting?.Invoke();
- }
+ protected override void OnExit(ExitEventArgs e)
+ => Exiting?.Invoke();
}
}
diff --git a/Common/OmniPlus/InspectionUI/InspectionUI.csproj b/Common/OmniPlus/InspectionUI/InspectionUI.csproj
index c26ab542..a5f58428 100644
--- a/Common/OmniPlus/InspectionUI/InspectionUI.csproj
+++ b/Common/OmniPlus/InspectionUI/InspectionUI.csproj
@@ -61,10 +61,12 @@
+
+
-
+
diff --git a/Common/OmniPlus/InspectionUI/MainWindow.xaml b/Common/OmniPlus/InspectionUI/MainWindow.xaml
index aeb535d4..2bd8765a 100644
--- a/Common/OmniPlus/InspectionUI/MainWindow.xaml
+++ b/Common/OmniPlus/InspectionUI/MainWindow.xaml
@@ -14,12 +14,14 @@
+
-
+
+
@@ -96,11 +98,20 @@
+
+
@@ -108,14 +119,14 @@
Content="Abbrechen"
Cursor="Hand"
FontFamily="Consolas"
- Grid.Column="8"
+ Grid.Column="9"
IsEnabled="{Binding IsCancelInspectionEnabled}"
Margin="5"
Padding="10, 5" />
-
+
-
+
diff --git a/Common/OmniPlus/InspectionUI/ViewModels/DefaultsHeaderRow.cs b/Common/OmniPlus/InspectionUI/ViewModels/DefaultsHeaderRow.cs
index 957946ed..211ea2f4 100644
--- a/Common/OmniPlus/InspectionUI/ViewModels/DefaultsHeaderRow.cs
+++ b/Common/OmniPlus/InspectionUI/ViewModels/DefaultsHeaderRow.cs
@@ -9,7 +9,6 @@
this.Add(new ItemsGridHeaderVM());
this.Add(new ItemsGridHeaderVM());
this.Add(new ItemsGridHeaderVM());
- this.Add(new ItemsGridHeaderVM());
var pruefpunkte = OmniContext
.Current
diff --git a/Common/OmniPlus/InspectionUI/ViewModels/DefaultsRowVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/DefaultsRowVM.cs
index 13a4ce75..56f00f17 100644
--- a/Common/OmniPlus/InspectionUI/ViewModels/DefaultsRowVM.cs
+++ b/Common/OmniPlus/InspectionUI/ViewModels/DefaultsRowVM.cs
@@ -8,7 +8,6 @@
{
this.Add(new ItemsGridHeaderVM());
this.Add(new ItemsGridHeaderVM { Value = "PP:" });
- this.Add(new ItemsGridHeaderVM());
this.Add(new ItemsGridHeaderVM { Value = "0.0" });
var pruefpunkte = OmniContext
diff --git a/Common/OmniPlus/InspectionUI/ViewModels/DisplayHeadersRowVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/DisplayHeadersRowVM.cs
new file mode 100644
index 00000000..42ed65a8
--- /dev/null
+++ b/Common/OmniPlus/InspectionUI/ViewModels/DisplayHeadersRowVM.cs
@@ -0,0 +1,14 @@
+namespace InspectionUI.ViewModels
+{
+ public class DisplayHeadersRowVM : ItemsGridRowVM
+ {
+ public DisplayHeadersRowVM() : base()
+ {
+ this.Add(new ItemsGridHeaderVM { Width = ItemsGridHeaderVM.Auto });
+ this.Add(new ItemsGridHeaderVM { Value = "MeterNr", Width = ItemsGridHeaderVM.Star });
+ this.Add(new ItemsGridHeaderVM { Value = "Display Units", Width = ItemsGridHeaderVM.Star });
+ this.Add(new ItemsGridHeaderVM { Value = "Start value", Width = ItemsGridHeaderVM.Star });
+ this.Add(new ItemsGridHeaderVM { Value = "Current value", Width = ItemsGridHeaderVM.Star });
+ }
+ }
+}
diff --git a/Common/OmniPlus/InspectionUI/ViewModels/DisplayReaderVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/DisplayReaderVM.cs
new file mode 100644
index 00000000..1838d44e
--- /dev/null
+++ b/Common/OmniPlus/InspectionUI/ViewModels/DisplayReaderVM.cs
@@ -0,0 +1,110 @@
+namespace InspectionUI.ViewModels
+{
+ using InspectionUI.Models;
+
+ using OmniPlus;
+
+ using System;
+ using System.Collections.Generic;
+ using System.Collections.ObjectModel;
+ using System.Threading;
+ using System.Threading.Tasks;
+
+ internal class DisplayReaderVM : VM
+ {
+ private readonly OmniContext omniContext;
+
+ private CancellationTokenSource cancellationTokenSource;
+ private CancellationToken cancellationToken;
+ private List readers;
+
+ public DisplayReaderVM()
+ {
+ this.Meters = new ObservableCollection();
+ this.omniContext = OmniContext.Create(Appsettings.OmniDB, Appsettings.CurrentDirectory);
+ }
+
+ public ObservableCollection Meters { get; }
+
+ private void StartReading()
+ {
+ try
+ {
+ this.cancellationTokenSource?.Cancel(true);
+ this.readers?.Clear();
+ this.Meters?.Clear();
+ }
+ catch (Exception e)
+ {
+
+ }
+
+ this.cancellationTokenSource = new CancellationTokenSource();
+ this.cancellationToken = this.cancellationTokenSource.Token;
+ this.readers = new List();
+
+ this.omniContext.LoadMeters(Appsettings.COMPorts, Appsettings.PSNR);
+
+ foreach (var meter in this.omniContext.Meters)
+ {
+ var meterVM = new MeterDisplayVM(meter);
+
+ this.Meters.Add(meterVM);
+ this.readers.Add(meterVM.StartReadingAsync(this.cancellationToken));
+ }
+ }
+ }
+
+ internal class MeterDisplayVM : VM
+ {
+ private readonly OmniMeter meter;
+
+ public MeterDisplayVM(OmniMeter meter)
+ => this.meter = meter;
+
+ private string meterId;
+ public string MeterNr
+ {
+ get => this.meterId;
+ set => this.Set(() => this.meterId = value);
+ }
+
+ private string displayValue;
+ public string DisplayValue
+ {
+ get => this.displayValue;
+ set => this.Set(() => this.displayValue = value);
+ }
+
+ public Task StartReadingAsync(CancellationToken cancellationToken)
+ {
+ cancellationToken.Register(this.meter.Dispose);
+
+ void StartReading(object state)
+ {
+ if (state is MeterDisplayVM vm)
+ {
+ var _meter = vm.meter;
+ _meter.Connect();
+ _meter.IdentifySelf();
+
+ if (!_meter.IsActive)
+ {
+ return;
+ }
+
+ vm.MeterNr = _meter.MeterNr;
+
+ while (_meter.IsActive)
+ {
+ vm.DisplayValue = _meter.ReadDisplay();
+
+ Thread.Sleep(1000);
+ }
+ }
+ }
+
+ return Task.Factory.StartNew(StartReading, this, cancellationToken);
+ }
+ }
+}
diff --git a/Common/OmniPlus/InspectionUI/ViewModels/HeadersRowVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/InspectionHeadersRowVM.cs
similarity index 80%
rename from Common/OmniPlus/InspectionUI/ViewModels/HeadersRowVM.cs
rename to Common/OmniPlus/InspectionUI/ViewModels/InspectionHeadersRowVM.cs
index d8656bf9..e678c9f8 100644
--- a/Common/OmniPlus/InspectionUI/ViewModels/HeadersRowVM.cs
+++ b/Common/OmniPlus/InspectionUI/ViewModels/InspectionHeadersRowVM.cs
@@ -1,16 +1,13 @@
namespace InspectionUI.ViewModels
{
- using InspectionUI.Models;
-
using OmniPlus;
- public class HeadersRowVM : ItemsGridRowVM
+ public class InspectionHeadersRowVM : ItemsGridRowVM
{
- public HeadersRowVM() : base()
+ public InspectionHeadersRowVM() : base()
{
this.Add(new ItemsGridHeaderVM { Width = ItemsGridHeaderVM.Auto });
this.Add(new ItemsGridHeaderVM { Value = "MeterNr", Width = ItemsGridHeaderVM.Star });
- this.Add(new ItemsGridHeaderVM { Value = "SerialNr", Width = ItemsGridHeaderVM.Star });
this.Add(new ItemsGridHeaderVM { Value = "Crr.F. %", Width = ItemsGridHeaderVM.Auto });
var pruefpunkte = OmniContext
diff --git a/Common/OmniPlus/InspectionUI/ViewModels/ItemsGridVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/ItemsGridVM.cs
index 5d64ffaf..36dab0fd 100644
--- a/Common/OmniPlus/InspectionUI/ViewModels/ItemsGridVM.cs
+++ b/Common/OmniPlus/InspectionUI/ViewModels/ItemsGridVM.cs
@@ -19,7 +19,7 @@
IEnumerator IEnumerable.GetEnumerator()
{
- return this.GetEnumerator();
+ return this.rows.GetEnumerator();
}
protected void Add(ItemsGridRowVM item)
@@ -46,5 +46,10 @@
{
this.rows.Insert(index, item);
}
+
+ internal void Remove(ItemsGridRowVM row)
+ {
+ this.rows.Remove(row);
+ }
}
}
diff --git a/Common/OmniPlus/InspectionUI/ViewModels/MainVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/MainVM.cs
index 387ac3e1..d18c49da 100644
--- a/Common/OmniPlus/InspectionUI/ViewModels/MainVM.cs
+++ b/Common/OmniPlus/InspectionUI/ViewModels/MainVM.cs
@@ -1,6 +1,5 @@
namespace InspectionUI.ViewModels
{
- using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
@@ -11,6 +10,7 @@
{
this.IsReloadEnabled = true;
this.Reload = new Command(this.ReloadCommand);
+ this.StartDisplayReading = new Command(this.StartDisplayReadingCommand);
this.StartInspection = new Command(this.StartInspectionCommand);
this.CancelInspection = new Command(this.CancelInspectionCommand);
this.MetersPool = new MetersPoolVM(this.ConnectionType);
@@ -64,6 +64,8 @@
public ICommand Reload { get; }
+ public ICommand StartDisplayReading { get; }
+
private bool startInspectionEnabled;
public bool IsStartInspectionEnabled
{
@@ -147,26 +149,28 @@
}
}
+ private void StartDisplayReadingCommand()
+ {
+ this.StartNewTask(() =>
+ {
+ this.EnableButtons(false);
+ this.MetersPool.StartDisplayReading();
+ });
+ }
+
private void StartInspectionCommand()
{
this.StartNewTask(() =>
{
this.EnableButtons(false);
this.MetersPool.RunInspection(this.KeepCorrection);
- this.IsCancelInspectionEnabled = false;
- this.IsReloadEnabled = true;
});
}
private void CancelInspectionCommand()
{
- this.StartNewTask(() =>
- {
- this.EnableButtons(false);
- this.MetersPool.CancelInspection();
- this.IsCancelInspectionEnabled = false;
- this.IsReloadEnabled = true;
- });
+ this.StartNewTask(this.MetersPool.CancelInspection)
+ .ContinueWith(_ => this.EnableButtons(true));
}
private void EnableButtons(bool enabled)
diff --git a/Common/OmniPlus/InspectionUI/ViewModels/MeterVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/MeterVM.cs
index 150b498e..825d5447 100644
--- a/Common/OmniPlus/InspectionUI/ViewModels/MeterVM.cs
+++ b/Common/OmniPlus/InspectionUI/ViewModels/MeterVM.cs
@@ -3,6 +3,7 @@
using OmniPlus;
using System;
+ using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
@@ -22,7 +23,6 @@
this.Add(new ItemsGridHeaderVM { Value = $"{this.meter.SlotNr, 2}." });
this.Add(new ItemsGridCellVM());
this.Add(new ItemsGridCellVM());
- this.Add(new ItemsGridCellVM());
var pruefpunkte = OmniContext
.Current
@@ -40,9 +40,12 @@
this.Add(new ItemsGridCellVM());
this.MeterNr = this.meter.MeterNr;
- this.SerialNr = this.meter.SerialNr;
+ // this.SerialNr = this.meter.SerialNr;
}
+ protected int? SlotNr
+ => this.meter?.SlotNr;
+
private string meterId;
protected string MeterNr
{
@@ -50,13 +53,17 @@
set => this.Set(() => this.meterId = value, value, "{0}", 1);
}
- private int? serialNr;
- protected int? SerialNr
- {
- get => this.serialNr;
- set => this.Set(() => this.serialNr = value, value, "{0}", 2);
- }
-
+ //private int? serialNr;
+ //protected int? SerialNr
+ //{
+ // get => this.serialNr;
+ // set => this.Set(() => this.serialNr = value, value, "{0}", 2);
+ //}
+
+ protected string DisplayValue => this.meter.ReadDisplay();
+
+ protected string MeterUnits => this.meter.MeterUnits;
+
internal Task ConnectAsync(int connectionType, CancellationToken cancellationToken)
{
void Connect(object state)
@@ -129,7 +136,7 @@
var meter = vm.meter;
meter.InitializeForInspection(keepCorrection);
- vm.Set(meter.TestRun.CrrfBefore, "{0:0.0}", 3);
+ vm.Set(meter.TestRun.CrrfBefore, "{0:0.0}", 2);
}
}
@@ -148,7 +155,7 @@
var meter = vm.meter;
meter.StartCalibration((ushort)_pp.Rotations, (ushort)_pp.Duration);
- vm.ShowRestTime(_pp.Duration, vm.uinr * 3 + 1);
+ vm.ShowRestTime(_pp.Duration, vm.uinr * 3);
}
}
@@ -165,7 +172,7 @@
var meter = vm.meter;
var response = meter.StopCalibration(_pp, refm3s);
- var index = vm.uinr * 3 + 1;
+ var index = vm.uinr * 3;
vm.Set(response.Item2, "{0}", index);
vm.Set(response.Item1, "{0:0.0000000}", index + 1);
@@ -197,7 +204,7 @@
foreach (var testedPP in meter.TestRun.TestedPoints)
{
- var index = vm.uinr * 3 + 1 + 2;
+ var index = vm.uinr * 3 + 2;
vm.Set(testedPP.Acc, "{0:0.00}", index);
vm.Set(x =>
@@ -216,6 +223,57 @@
return this.StartTask(FinishInspection, cancellationToken);
}
+ private string waageFile;
+
+ internal void InitDisplayReading()
+ {
+ this.Clear();
+
+ var timestamp = DateTime.Now;
+ var displayValue = this.DisplayValue;
+ var currentValueCell = new ItemsGridCellVM
+ {
+ Value = $"{displayValue}"
+ };
+
+ var dir = Directory.CreateDirectory("./waage").FullName;
+ this.waageFile = $"{dir}/{this.MeterNr}.txt";
+
+ this.Add(new ItemsGridHeaderVM { Value = $"{this.SlotNr,2}." });
+ this.Add(new ItemsGridCellVM { Value = this.MeterNr });
+ this.Add(new ItemsGridCellVM { Value = this.MeterUnits });
+ this.Add(new ItemsGridCellVM { Value = displayValue });
+ this.Add(currentValueCell);
+
+ File.AppendAllText(this.waageFile, $"{timestamp:yyyy-MM-dd HH:mm:ss};{displayValue}{Environment.NewLine}");
+
+ this.NotifyPropertyChanged();
+ }
+
+ internal Task StartDisplayReadingAsync(CancellationToken cancellationToken)
+ {
+ void StartDisplayReading(object state)
+ {
+ if (state is MeterVM vm)
+ {
+ var awaiter = new ManualResetEventSlim();
+
+ while (!cancellationToken.IsCancellationRequested)
+ {
+ var displayValue = vm.DisplayValue;
+ var timestamp = DateTime.Now;
+ vm[4].Value = displayValue;
+ File.AppendAllText(this.waageFile, $"{timestamp:yyyy-MM-dd HH:mm:ss};{displayValue}{Environment.NewLine}");
+
+ awaiter.Reset();
+ awaiter.Wait(1000);
+ }
+ }
+ }
+
+ return Task.Factory.StartNew(StartDisplayReading, this, cancellationToken);
+ }
+
private void ShowRestTime(int duration, int index)
{
var interval = 1000;
diff --git a/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs
index 39c881c7..dc3154f7 100644
--- a/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs
+++ b/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs
@@ -179,6 +179,54 @@
this.WaitAll(tasks);
}
+ internal void StartDisplayReading()
+ {
+ this.DispatcherInvoke(() =>
+ {
+ var headers = new List();
+
+ foreach (var row in this)
+ {
+ if (!(row is MeterVM _))
+ {
+ headers.Add(row);
+ }
+ }
+
+ foreach (var header in headers)
+ {
+ this.Remove(header);
+ }
+
+ this.Insert(0, new DisplayHeadersRowVM());
+
+ foreach (var row in this)
+ {
+ if (row is MeterVM meterVM)
+ {
+ meterVM.InitDisplayReading();
+ }
+ }
+
+ this.NotifyPropertyChanged();
+ });
+
+ this.cancellationTokenSource = new CancellationTokenSource();
+ var cancellationToken = this.cancellationTokenSource.Token;
+ var tasks = new List();
+ var metersVM = this
+ .Select(x => x is MeterVM m ? m : null)
+ .Where(x => x != null)
+ .ToArray();
+
+ foreach (var meterVM in metersVM)
+ {
+ tasks.Add(meterVM.StartDisplayReadingAsync(cancellationToken));
+ }
+
+ this.WaitAll(tasks);
+ }
+
internal void LoadAuftrag(string size, string type)
{
this.Dispose();
@@ -213,7 +261,7 @@
{
this.DispatcherInvoke(() =>
{
- this.Add(new HeadersRowVM());
+ this.Add(new InspectionHeadersRowVM());
this.Add(new DefaultsHeaderRow());
this.Add(new DefaultsRowVM());
});
diff --git a/Common/OmniPlus/InspectionUI/ViewModels/ReferenceMeterVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/ReferenceMeterVM.cs
index f714284a..681dc955 100644
--- a/Common/OmniPlus/InspectionUI/ViewModels/ReferenceMeterVM.cs
+++ b/Common/OmniPlus/InspectionUI/ViewModels/ReferenceMeterVM.cs
@@ -23,7 +23,7 @@
this.omniContext = omniContext;
this.signarR = new ExchangeConnection();
this.signarR.Connect(Appsettings.HubURL);
- this.ppnr = 1;
+ this.ppnr = 0;
this.LoadHeaders();
}
@@ -60,6 +60,8 @@
void TimerElapsed(object _, ElapsedEventArgs _1)
{
+ this.Set(--timeout, "... {0}", this.ppnr + 1);
+
if (timeout < 0 || cancel)
{
timeout = 0;
@@ -67,12 +69,6 @@
timer.Stop();
waitHandle.Set();
}
- else
- {
- this.Set(timeout, "... {0}", this.ppnr + 1);
- }
-
- timeout--;
}
timer.Elapsed += TimerElapsed;
@@ -86,7 +82,8 @@
this.signarR.StartFlow -= FlowStarted;
timer.Elapsed -= TimerElapsed;
- waitHandle.Wait(1000);
+ waitHandle.Wait(5000);
+ waitHandle.Reset();
this.Set(volume, "{0:0.000}", this.ppnr + 1);
}
@@ -232,7 +229,6 @@
this.Add(new ItemsGridHeaderVM());
this.Add(new ItemsGridHeaderVM { Value = "RZ:" });
this.Add(new ItemsGridHeaderVM());
- this.Add(new ItemsGridHeaderVM());
foreach (var _ in this.omniContext.Pruefpunkte)
{
diff --git a/Common/OmniPlus/OmniPlus/Connections/IRDAEncoder.cs b/Common/OmniPlus/OmniPlus/Connections/IRDAEncoder.cs
index 3a8bdacf..3fbc0bf7 100644
--- a/Common/OmniPlus/OmniPlus/Connections/IRDAEncoder.cs
+++ b/Common/OmniPlus/OmniPlus/Connections/IRDAEncoder.cs
@@ -1,29 +1,132 @@
namespace OmniPlus.Connections
{
+ using System;
+
internal sealed class IRDAEncoder : SerialDataEncoder
{
- public override bool TryDecode(byte[] data, out byte[] message, out int start, out int length)
- {
- message = default(byte[]);
+ const byte IRDA_START = 0x9B;
+ const byte IRDA_PING = 0x82;
+ const byte IRDA_PC2REG = 0x33;
+ const byte IRDA_REG2PC = 0x43;
+ const byte IRDA_MIN_LENGTH = 5;
- if (data.FromIrdaMessage(out byte[] payload, out start, out length))
+ public IRDAEncoder(SerialDataEncoder encoder = null) : base(encoder)
+ {
+ }
+
+ public override bool TryDecode(byte[] bytes, out byte[] data, out int start, out int length)
+ {
+ start = -1;
+ length = -1;
+ data = default(byte[]);
+ var payload = default(byte[]);
+
+ if (bytes is null)
{
- if (payload.FromUI1236Message(out message, out start, out _))
+ return false;
+ }
+
+ var bytesLength = bytes.Length;
+
+ if (bytesLength == 0)
+ {
+ return false;
+ }
+
+ var reg2pc = -1;
+ var succeeded = false;
+
+ while (!succeeded && start < bytesLength)
+ {
+ start = Array.IndexOf(bytes, IRDA_START, start + 1);
+
+ if (start < 0)
{
- return true;
+ break;
+ }
+
+ reg2pc = Array.IndexOf(bytes, IRDA_REG2PC);
+ succeeded = reg2pc - start == 1;
+
+ if (reg2pc < 0 && !succeeded)
+ {
+ break;
}
}
- return false;
+ if (!succeeded)
+ {
+ return false;
+ }
+
+ var reg2pcIndex = start + 1;
+ var lengthIndex = start + 2;
+ var startIndexD = start + 3;
+
+ if (lengthIndex >= bytesLength)
+ {
+ return false;
+ }
+
+ length = bytes[lengthIndex] * 2;
+
+ if (start + length + 5 > bytesLength)
+ {
+ return false;
+ }
+
+ payload = new byte[length];
+
+ Array.Copy(bytes, startIndexD, payload, 0, length);
+
+ length += 5;
+
+ if (this.encoder is null)
+ {
+ data = payload;
+
+ return true;
+ }
+
+ return this.encoder.TryDecode(payload, out data, out _, out _);
}
- public override bool TryEncode(byte[] data, out byte[] message)
+ public override bool TryEncode(byte[] data, out byte[] bytes)
{
- message = data
- .ToUI1236Message()
- .ToIrdaMessage();
+ bytes = new byte[0];
- return message != null && message.Length > 0;
+ if (data is null || data.Length == 0)
+ {
+ return false;
+ }
+
+ var payload = default(byte[]);
+
+ this.encoder?.TryEncode(data, out payload);
+
+ if (payload is null || payload.Length == 0)
+ {
+ return false;
+ }
+
+ var payloadLength = payload.Length;
+ var bytesLength = payloadLength;
+ bytesLength += bytesLength % 2;
+
+ var index = 0;
+ bytes = new byte[bytesLength + IRDA_MIN_LENGTH];
+ bytes[index++] = IRDA_START;
+ bytes[index++] = IRDA_PC2REG;
+ bytes[index++] = (byte)(bytesLength / 2);
+
+ payload.CopyTo(bytes, index);
+
+ var length = bytes.Length;
+ var crc16 = bytes.CalculateCRC8408(1, length - 2);
+ bytes[length - 2] = crc16[0];
+ bytes[length - 1] = crc16[1];
+
+ return true;
}
}
}
diff --git a/Common/OmniPlus/OmniPlus/Connections/IRDAStream.cs b/Common/OmniPlus/OmniPlus/Connections/IRDAStream.cs
index 40d7a3a7..b610a538 100644
--- a/Common/OmniPlus/OmniPlus/Connections/IRDAStream.cs
+++ b/Common/OmniPlus/OmniPlus/Connections/IRDAStream.cs
@@ -4,47 +4,37 @@
internal sealed class IRDAStream : SerialDataStream
{
- private readonly ManualResetEventSlim waitHandle;
-
public IRDAStream(SerialDataEncoder encoder, SerialPortSettings settings) : base(encoder, settings)
{
- this.waitHandle = new ManualResetEventSlim();
}
public override byte[] Send(params byte[] message)
{
- var response = new byte[0];
- var received = false;
+ var awaiter = new ManualResetEventSlim();
+ var response = default(byte[]);
void DataReceived(byte[] data)
{
response = data;
- received = true;
- this.waitHandle.Set();
+ awaiter.Set();
+
+ this.DataReceived -= DataReceived;
}
this.DataReceived += DataReceived;
- this.StartListening();
-
- for (int i = 0; i < 3 && !received; i++)
+ for (int i = 0; i < 3 && response is null; i++)
{
this.WritePlain(1, 1, 1, 1);
-
- this.waitHandle.Reset();
- this.waitHandle.Wait(1);
+ awaiter.Wait(1);
+ awaiter.Reset();
this.WriteEncoded(message);
-
- this.waitHandle.Reset();
- this.waitHandle.Wait(3000);
+ awaiter.Wait(1000);
+ awaiter.Reset();
}
- this.StopListening();
-
- this.DataReceived -= DataReceived;
-
return response;
}
}
diff --git a/Common/OmniPlus/OmniPlus/Connections/SerialDataEncoder.cs b/Common/OmniPlus/OmniPlus/Connections/SerialDataEncoder.cs
index 096f390a..d758c662 100644
--- a/Common/OmniPlus/OmniPlus/Connections/SerialDataEncoder.cs
+++ b/Common/OmniPlus/OmniPlus/Connections/SerialDataEncoder.cs
@@ -2,8 +2,13 @@
{
internal abstract class SerialDataEncoder
{
- public abstract bool TryEncode(byte[] data, out byte[] message);
+ protected readonly SerialDataEncoder encoder;
- public abstract bool TryDecode(byte[] data, out byte[] message, out int start, out int length);
+ protected SerialDataEncoder(SerialDataEncoder encoder = null)
+ => this.encoder = encoder;
+
+ public abstract bool TryEncode(byte[] bytes, out byte[] payload);
+
+ public abstract bool TryDecode(byte[] data, out byte[] bytes, out int start, out int length);
}
}
diff --git a/Common/OmniPlus/OmniPlus/Connections/SerialDataExtensions.cs b/Common/OmniPlus/OmniPlus/Connections/SerialDataExtensions.cs
index 30956c0d..466bf034 100644
--- a/Common/OmniPlus/OmniPlus/Connections/SerialDataExtensions.cs
+++ b/Common/OmniPlus/OmniPlus/Connections/SerialDataExtensions.cs
@@ -218,11 +218,11 @@
/// n-2. .... - CRC16 0
/// n-1. .... - CRC16 1
///
- public static bool FromIrdaMessage(this byte[] bytes, out byte[] message, out int start, out int length)
+ public static bool FromIrdaMessage(this byte[] bytes, out byte[] payload, out int start, out int length)
{
- message = default(byte[]);
- length = default(int);
- start = default(int);
+ payload = default(byte[]);
+ length = -1;
+ start = -1;
if (bytes is null)
{
@@ -231,41 +231,58 @@
var bytesLength = bytes.Length;
- while (start < bytesLength)
+ if (bytesLength == 0)
{
- start = Array.IndexOf(bytes, IRDA_REG2PC, start) - 1;
+ return false;
+ }
- if (start < 0 || bytes[start] == IRDA_START)
+ var reg2pc = -1;
+ var succeeded = false;
+
+ while (!succeeded && start < bytesLength)
+ {
+ start = Array.IndexOf(bytes, IRDA_START, start + 1);
+
+ if (start < 0)
{
break;
}
- start += 2;
+ reg2pc = Array.IndexOf(bytes, IRDA_REG2PC);
+ succeeded = reg2pc - start == 1;
+
+ if (reg2pc < 0 && !succeeded)
+ {
+ break;
+ }
}
- if (start < 0)
+ if (!succeeded)
{
return false;
}
- start += 2;
+ var reg2pcIndex = start + 1;
+ var lengthIndex = start + 2;
+ var startIndexD = start + 3;
- if (start >= bytesLength)
+ if (lengthIndex >= bytesLength)
{
return false;
}
- length = bytes[start] * 2;
+ length = bytes[lengthIndex] * 2;
- if (start + length >= bytesLength)
+ if (start + length + 5 > bytesLength)
{
return false;
}
- start += 1;
- message = new byte[length];
+ payload = new byte[length];
- Array.Copy(bytes, start, message, 0, length);
+ Array.Copy(bytes, startIndexD, payload, 0, length);
+
+ length += 5;
return true;
}
@@ -279,17 +296,24 @@
/// n-2. .... - Checksum 0
/// n-1. .... - Checksum 1
///
- public static bool FromUI1236Message(this byte[] bytes, out byte[] message, out int start, out Int32 length)
+ public static bool FromUI1236Message(this byte[] bytes, out byte[] message, out int start, out int length)
{
message = default(byte[]);
- length = default(int);
- start = default(int);
+ length = -1;
+ start = -1;
if (bytes is null)
{
return false;
}
+ var bytesLength = bytes.Length;
+
+ if (bytesLength < 5)
+ {
+ return false;
+ }
+
start = Array.IndexOf(bytes, UI1236_START);
if (start < 0)
@@ -313,7 +337,7 @@
start += 2;
- if (start + length >= bytes.Length)
+ if (start + length > bytes.Length)
{
return false;
}
diff --git a/Common/OmniPlus/OmniPlus/Connections/SerialDataStream.cs b/Common/OmniPlus/OmniPlus/Connections/SerialDataStream.cs
index 6f0f6fc8..c9c0b35c 100644
--- a/Common/OmniPlus/OmniPlus/Connections/SerialDataStream.cs
+++ b/Common/OmniPlus/OmniPlus/Connections/SerialDataStream.cs
@@ -7,9 +7,7 @@
internal abstract class SerialDataStream : SerialPortStream
{
- private readonly ManualResetEventSlim waitHandle;
private readonly SerialDataEncoder encoder;
- private readonly List buffer;
private CancellationTokenSource cancellationTokenSource;
private CancellationToken cancellationToken;
@@ -18,8 +16,6 @@
protected SerialDataStream(SerialDataEncoder encoder, SerialPortSettings settings) : base(settings)
{
this.encoder = encoder;
- this.buffer = new List();
- this.waitHandle = new ManualResetEventSlim();
}
protected event Action DataReceived;
@@ -55,32 +51,6 @@
}
}
- protected void StartListening()
- {
- try
- {
- this.buffer.Clear();
- this.waitHandle.Set();
- }
- catch (Exception e)
- {
- this.NotifyStateChanged($"EX|{e}");
- }
- }
-
- protected void StopListening()
- {
- try
- {
- this.waitHandle.Reset();
- this.buffer.Clear();
- }
- catch (Exception e)
- {
- this.NotifyStateChanged($"EX|{e}");
- }
- }
-
protected void WriteEncoded(params byte[] bytes)
{
try
@@ -88,7 +58,6 @@
if (this.encoder.TryEncode(bytes, out byte[] message))
{
this.Write(message, 0, message.Length);
-
this.NotifyStateChanged($"TX|{BitConverter.ToString(bytes)}");
}
}
@@ -112,23 +81,30 @@
private void ReadingTaskHandler(object state)
{
- if (state is SerialDataStream serialDataStream)
+ if (state is SerialDataStream stream)
{
- while (serialDataStream.IsOpen)
+ var buffer = new List();
+
+ while (stream.IsOpen)
{
- serialDataStream.waitHandle.Wait();
+ var bytes = stream.Read();
- var data = serialDataStream.Read();
- var buffer = serialDataStream.buffer;
-
- buffer.AddRange(data);
-
- if (serialDataStream.encoder.TryDecode(buffer.ToArray(), out byte[] message, out int start, out int count))
+ if (bytes.Length == 0)
{
- buffer.RemoveRange(0, Math.Min(start + count, buffer.Count));
-
- serialDataStream.DataReceived?.Invoke(message);
- serialDataStream.NotifyStateChanged($"RX|{BitConverter.ToString(message)}");
+ continue;
+ }
+
+ buffer.AddRange(bytes);
+
+ if (stream.encoder.TryDecode(buffer.ToArray(), out var data, out var start, out var length))
+ {
+ stream.DataReceived?.Invoke(data);
+ stream.NotifyStateChanged($"RX|{BitConverter.ToString(data)}");
+ buffer.RemoveRange(0, Math.Min(start + length, buffer.Count));
+ }
+ else if (start < 0)
+ {
+ buffer.Clear();
}
}
}
@@ -147,7 +123,8 @@
}
this.StartReading();
- });
+
+ }, this.cancellationToken);
}
}
}
diff --git a/Common/OmniPlus/OmniPlus/Connections/SerialPortStream.cs b/Common/OmniPlus/OmniPlus/Connections/SerialPortStream.cs
index 3aa43e0b..4fde33d0 100644
--- a/Common/OmniPlus/OmniPlus/Connections/SerialPortStream.cs
+++ b/Common/OmniPlus/OmniPlus/Connections/SerialPortStream.cs
@@ -23,39 +23,9 @@
public event StateChanged StateChanged;
- public int BytesToRead
- {
- get
- {
- try
- {
- return this.serialPort?.BytesToRead ?? default(int);
- }
- catch (Exception e)
- {
- this.NotifyStateChanged($"EX|{e}");
+ public int BytesToRead => this.serialPort?.BytesToRead ?? default(int);
- return default(int);
- }
- }
- }
-
- public bool IsOpen
- {
- get
- {
- try
- {
- return this.serialPort?.IsOpen ?? false;
- }
- catch (Exception e)
- {
- this.NotifyStateChanged($"EX|{e}");
-
- return default(bool);
- }
- }
- }
+ public bool IsOpen => this.serialPort?.IsOpen ?? false;
public virtual void Close()
{
@@ -126,24 +96,17 @@
public abstract byte[] Send(params byte[] message);
- protected void NotifyStateChanged(object state)
- {
- this.StateChanged?.Invoke(state);
- }
+ protected void NotifyStateChanged(object state)
+ => this.StateChanged?.Invoke(state);
protected byte[] Read()
{
- var bytes = new byte[] { };
+ var bytes2read = this.BytesToRead;
+ var bytes = new byte[bytes2read];
- try
+ if (bytes2read > 0)
{
- bytes = new byte[this.BytesToRead];
-
- this.serialPort.Read(bytes, 0, bytes.Length);
- }
- catch (Exception e)
- {
- this.NotifyStateChanged($"EX|{e}");
+ this.serialPort.Read(bytes, 0, bytes2read);
}
return bytes;
@@ -151,22 +114,15 @@
protected void Write(byte[] bytes, int start, int length)
{
- try
- {
- var lock_object = new object();
+ var lock_object = new object();
- lock (lock_object)
- {
- this.serialPort.Write(bytes, 0, length);
- }
- }
- catch (Exception e)
+ lock (lock_object)
{
- this.NotifyStateChanged($"EX|{e}");
+ this.serialPort.Write(bytes, 0, length);
}
}
- private void DiscardInOut()
+ protected void DiscardInOut()
{
try
{
diff --git a/Common/OmniPlus/OmniPlus/Connections/UI1236Encoder.cs b/Common/OmniPlus/OmniPlus/Connections/UI1236Encoder.cs
new file mode 100644
index 00000000..b5645969
--- /dev/null
+++ b/Common/OmniPlus/OmniPlus/Connections/UI1236Encoder.cs
@@ -0,0 +1,102 @@
+namespace OmniPlus.Connections
+{
+ using System;
+
+ internal sealed class UI1236Encoder : SerialDataEncoder
+ {
+ public const byte UI1236_START = 0x0D;
+ public const byte UI1236_LEN_INDEX = 1;
+ public const byte UI1236_DATA_INDEX = 3;
+ public const byte UI1236_MIN_LENGTH = 5;
+
+ public UI1236Encoder(SerialDataEncoder encoder = null) : base(encoder)
+ {
+ }
+
+ public override bool TryDecode(byte[] bytes, out byte[] payload, out int start, out int length)
+ {
+ payload = default(byte[]);
+ length = -1;
+ start = -1;
+
+ if (bytes is null || bytes.Length == 0)
+ {
+ return false;
+ }
+
+ var bytesLength = bytes.Length;
+
+ start = Array.IndexOf(bytes, UI1236_START);
+
+ if (start < 0)
+ {
+ return false;
+ }
+
+ var lengthIndex = start + 1;
+
+ if (lengthIndex >= bytesLength)
+ {
+ return false;
+ }
+
+ length = bytes[lengthIndex] - 3;
+
+ if (length <= 0)
+ {
+ return false;
+ }
+
+ var controlIndex = lengthIndex + 1;
+
+ if (controlIndex >= bytesLength)
+ {
+ return false;
+ }
+
+ var dataIndex = controlIndex + 1;
+
+ if (dataIndex + length > bytesLength)
+ {
+ return false;
+ }
+
+ payload = new byte[length];
+
+ Array.Copy(bytes, dataIndex, payload, 0, length);
+
+ return true;
+ }
+
+ public override bool TryEncode(byte[] data, out byte[] bytes)
+ {
+ bytes = new byte[0];
+
+ if (data is null || data.Length == 0)
+ {
+ return false;
+ }
+
+ var dataLength = data.Length;
+ var bytesLength = dataLength + UI1236_MIN_LENGTH;
+
+ var index = 0;
+ bytes = new byte[bytesLength];
+ bytes[index++] = UI1236_START;
+ bytes[index++] = (byte)(dataLength + 3);
+
+ UI1236Control control = 0;
+ control.ReturnResponse = true;
+ bytes[index++] = control;
+
+ data.CopyTo(bytes, index);
+
+ var checksum = bytes.CalculateCheckSUM(bytesLength - 2);
+ var checksumIndex = dataLength + 3;
+ bytes[checksumIndex++] = checksum[1];
+ bytes[checksumIndex++] = checksum[0];
+
+ return true;
+ }
+ }
+}
diff --git a/Common/OmniPlus/OmniPlus/Connections/UniProStream.cs b/Common/OmniPlus/OmniPlus/Connections/UniProStream.cs
index 52f99b9c..d5ce78db 100644
--- a/Common/OmniPlus/OmniPlus/Connections/UniProStream.cs
+++ b/Common/OmniPlus/OmniPlus/Connections/UniProStream.cs
@@ -4,43 +4,44 @@
internal class UniProStream : SerialDataStream
{
- private readonly ManualResetEventSlim waitHandle;
-
public UniProStream(SerialDataEncoder encoder, SerialPortSettings settings) : base(encoder, settings)
{
- this.waitHandle = new ManualResetEventSlim();
}
public override byte[] Send(params byte[] message)
{
- var response = new byte[0];
- var received = false;
-
- void DataReceived(byte[] data)
+ byte[] SendMessage(int count = 3)
{
- response = data;
- received = true;
+ var awaiter = new ManualResetEventSlim();
+ var response = new byte[0];
+ var received = false;
- this.waitHandle.Set();
- }
+ void DataReceived(byte[] data)
+ {
+ response = data;
+ received = true;
- this.DataReceived += DataReceived;
+ awaiter.Set();
+ }
- this.StartListening();
+ this.DataReceived += DataReceived;
- for (int i = 0; i < 3 && !received; i++)
- {
this.WriteEncoded(message);
- this.waitHandle.Reset();
- this.waitHandle.Wait(3000);
+ awaiter.Wait(5000);
+ awaiter.Reset();
+
+ this.DataReceived -= DataReceived;
+
+ if (!received && count > 1)
+ {
+ return SendMessage(count - 1);
+ }
+
+ return response;
}
- this.StopListening();
-
- this.DataReceived -= DataReceived;
-
- return response;
+ return SendMessage();
}
}
}
diff --git a/Common/OmniPlus/OmniPlus/OmniContext.cs b/Common/OmniPlus/OmniPlus/OmniContext.cs
index 9bc9cc01..423a006f 100644
--- a/Common/OmniPlus/OmniPlus/OmniContext.cs
+++ b/Common/OmniPlus/OmniPlus/OmniContext.cs
@@ -95,7 +95,10 @@
public static OmniContext Create(String omniDB, String outputDirectory)
{
- Current = new OmniContext(omniDB, outputDirectory);
+ if (Current is null)
+ {
+ Current = new OmniContext(omniDB, outputDirectory);
+ }
return Current;
}
diff --git a/Common/OmniPlus/OmniPlus/OmniIrdaConnection.cs b/Common/OmniPlus/OmniPlus/OmniIrdaConnection.cs
index 0394a516..2fb46dd3 100644
--- a/Common/OmniPlus/OmniPlus/OmniIrdaConnection.cs
+++ b/Common/OmniPlus/OmniPlus/OmniIrdaConnection.cs
@@ -11,7 +11,7 @@
public OmniIrdaConnection(string portName)
{
this.stream = new IRDAStream(
- encoder: new IRDAEncoder(),
+ encoder: new IRDAEncoder(new UI1236Encoder()),
settings: new SerialPortSettings
{
BaudRate = 115200,
diff --git a/Common/OmniPlus/OmniPlus/OmniMeter.cs b/Common/OmniPlus/OmniPlus/OmniMeter.cs
index be4e22a1..0c8a11a8 100644
--- a/Common/OmniPlus/OmniPlus/OmniMeter.cs
+++ b/Common/OmniPlus/OmniPlus/OmniMeter.cs
@@ -45,6 +45,37 @@
public bool IsActive { get; private set; }
+ private RegisterSettings registerSettings;
+ private string meterUnits;
+ public string MeterUnits
+ {
+ get
+ {
+ if (this.registerSettings is null)
+ {
+ this.registerSettings = this.connection.ViewRegisterSettings();
+ }
+
+ if (string.IsNullOrWhiteSpace(this.meterUnits))
+ {
+ if (this.registerSettings.MeterUnits == (byte)MessurementUnits.CubicMeters)
+ {
+ this.meterUnits = "m³";
+ }
+ else if (this.registerSettings.MeterUnits == (byte)MessurementUnits.USGallons)
+ {
+ this.meterUnits = "GAL";
+ }
+ else
+ {
+ this.meterUnits = "unknown";
+ }
+ }
+
+ return this.meterUnits;
+ }
+ }
+
public void Connect(int connectionType = 0)
{
if (this.connection is null)
@@ -90,6 +121,36 @@
}
}
+ public string ReadDisplay()
+ {
+ try
+ {
+ if (this.registerSettings is null)
+ {
+ this.registerSettings = this.connection.ViewRegisterSettings();
+ }
+
+ var displayValue = this.connection.ViewReadings().Value ?? string.Empty;
+
+ if (double.TryParse(displayValue, out var readings))
+ {
+ if (this.registerSettings.MeterUnits == (byte)MessurementUnits.CubicMeters)
+ {
+ readings = readings / 1000;
+
+ displayValue = $"{readings:0.000}";
+ }
+ }
+
+ return displayValue;
+ }
+ catch (Exception)
+ {
+ return string.Empty;
+ }
+ }
+
+
public void Dispose()
{
if (this.connection != null)
@@ -149,9 +210,6 @@
public Tuple StopCalibration(OmniPruefpunkt pp, double refm3s)
{
var response = this.connection.ViewChamberCalibration();
-
- Thread.Sleep(1000);
-
var rotations = response.Rotations;
var duration = response.DurationSec;
@@ -288,9 +346,9 @@
Size = this.defaults.Size,
Range = this.defaults.Range,
VolumePerPulseUnit = 0x00,
- MeterUnits = (byte)MessurementUnits.USGallons,
- FlowUnits = (byte)MessurementUnits.USGallons,
- FlowRate = (byte)TimeUnits.Minutes,
+ MeterUnits = (byte)MessurementUnits.CubicMeters,
+ FlowUnits = (byte)MessurementUnits.CubicMeters,
+ FlowRate = (byte)TimeUnits.Hours,
FactoryCorrection = (sbyte)(crrf * 10),
FieldCorrection = 0x00,
PulseWeight = 1,
@@ -298,6 +356,8 @@
PulseUpdate = 0,
});
+ Thread.Sleep(2000);
+
this.TestRun.Succeeded = succeeded;
this.TestRun.CrrfAfter = this.connection.ViewFactoryCorrection();
this.TestRun.CloseTestRun();
@@ -327,7 +387,7 @@
{
CreateDirs = true,
KeepFileOpen = true,
- FileName = $"${{CurrentDir}}/{name}/{DateTime.Now:yyyy-MM-dd}/{DateTime.Now:HHmmss}.txt"
+ FileName = $"${{CurrentDir}}/{name}/{DateTime.Now:yyyyMMdd}.txt"
});
LogManager.Configuration.AddRule(LogLevel.Trace, LogLevel.Fatal, name);
LogManager.ReconfigExistingLoggers();
diff --git a/Common/OmniPlus/OmniPlus/OmniPlus.csproj b/Common/OmniPlus/OmniPlus/OmniPlus.csproj
index 04110e71..ef5c847f 100644
--- a/Common/OmniPlus/OmniPlus/OmniPlus.csproj
+++ b/Common/OmniPlus/OmniPlus/OmniPlus.csproj
@@ -69,6 +69,7 @@
+
diff --git a/Common/OmniPlus/OmniPlus/muster_config.txt b/Common/OmniPlus/OmniPlus/muster_config.txt
index f3a7060d..9d6f271b 100644
--- a/Common/OmniPlus/OmniPlus/muster_config.txt
+++ b/Common/OmniPlus/OmniPlus/muster_config.txt
@@ -1,21 +1,21 @@
-01: 01-39-34-37-30-35-36-39-33-00
-03: 01-39-38-30-35-38-34-31-34-00
-05: 01-52-31-2E-32-34-2C-4F-4D-4E-49-2D-30-31-2C-52-30-2E-33-2E-31-39-00
-07: 01-00
-09: 01-08-00
-0B: 01-04
-0D: 01-04-02
-0F: 01-00
-11: 01-FD
-13: 01-30-30-30-30-30-31-30-30-00
-15: 01-FF
-17: 01-38-31-36-32-33-33-35-31-35-36-35-30-00
-1F: 02
-21: 02
-25: 02
-27: 01-FC-01-0C-00
-29: 02
-2B: 01-2E-59-FC-01-05-04-08-00-01-34-00-00-00-00-00-FF-C9-9A-3B
+ 01: 01-39-34-37-30-35-36-39-33-00
+ 03: 01-39-38-30-35-38-34-31-34-00
+ 05: 01-52-31-2E-32-34-2C-4F-4D-4E-49-2D-30-31-2C-52-30-2E-33-2E-31-39-00
+ 07: 01-00
+ 09: 01-08-00
+ 0B: 01-04
+ 0D: 01-04-02
+ 0F: 01-00
+ 11: 01-FD
+ 13: 01-30-30-30-30-30-31-30-30-00
+ 15: 01-FF
+ 17: 01-38-31-36-32-33-33-35-31-35-36-35-30-00
+ 1F: 02
+ 21: 02
+ 25: 02
+ 27: 01-FC-01-0C-00
+ 29: 02
+ 2B: 01-2E-59-FC-01-05-04-08-00-01-34-00-00-00-00-00-FF-C9-9A-3B
FD-02: 01-00-00-E1-84-05-00
FD-05: 01-B2-C2-0D-2D
FD-06: 01-00-00-00-01-FE-04