1253 lines
41 KiB
C#
1253 lines
41 KiB
C#
namespace Xylem.Common.Ui.GenesisToolBox
|
|
{
|
|
using global::CordonelPreadjustmentUi;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.IO.Pipes;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
using TempFlansh;
|
|
|
|
using Xylem.Common.Hardware.WaterMeter.Genesis.DataPackages;
|
|
using Xylem.Common.Hardware.WaterMeter.Genesis.DataPackages.MeasurementRecords;
|
|
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore;
|
|
using Xylem.Common.Hardware.WaterMeter.Genesis.Registers;
|
|
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore;
|
|
using Xylem.Common.Logic.SoftwareAccessHelper;
|
|
using Xylem.Common.Ui.GenesisToolBox.Infrastructure;
|
|
|
|
public partial class FrmTempMeter : Form, ITempMeterForm
|
|
{
|
|
private ToolStripLabel slotsLabel;
|
|
private ToolStripComboBox slotsList;
|
|
private ToolStripLabel pcbidLabel;
|
|
private ToolStripTextBox pcbidTextBox;
|
|
private ToolStripButton connectBtn;
|
|
private ToolStripButton disconnectBtn;
|
|
private ToolStripButton testBtn;
|
|
|
|
private ProgrammState programmState;
|
|
private ZeroFlowGenesisMeter currentGenesis;
|
|
private MeterBatch meterBatch;
|
|
private Boolean lutIsMissing;
|
|
private List<RefTempRange> reftempRangeReq;
|
|
private List<CordonelTempLut> cordonelTempLut;
|
|
|
|
public FrmTempMeter()
|
|
{
|
|
this.InitializeComponent();
|
|
this.InitializeContent();
|
|
|
|
this.meterBatch = new MeterBatch();
|
|
this.reftempRangeReq = new List<RefTempRange>()
|
|
{
|
|
new RefTempRange() { Min = 7, Max= 9 },
|
|
new RefTempRange() { Min = 13,Max = 15 },
|
|
new RefTempRange() { Min = 16,Max = 18 },
|
|
new RefTempRange() { Min = 19,Max = 20 },
|
|
new RefTempRange() { Min = 20,Max = 22 },
|
|
new RefTempRange() { Min = 23,Max = 25 },
|
|
new RefTempRange() { Min = 26,Max = 28 },
|
|
new RefTempRange() { Min = 28,Max = 30 },
|
|
new RefTempRange() { Min = 33,Max = 36 },
|
|
};
|
|
|
|
nudCal1.Maximum = Int32.MaxValue;
|
|
nudCal2.Maximum = Int32.MaxValue;
|
|
nudCal3.Maximum = Int32.MaxValue;
|
|
nudZeroOffset1.Minimum = Int32.MinValue;
|
|
nudZeroOffset2.Minimum = Int32.MinValue;
|
|
nudZeroOffset3.Minimum = Int32.MinValue;
|
|
|
|
}
|
|
|
|
private void BtnConnect_Click(Object sender, EventArgs e)
|
|
=> this.Connect();
|
|
|
|
private void Connect()
|
|
{
|
|
try
|
|
{
|
|
if (int.TryParse($"{this.slotsList.SelectedItem}", out var slotNr))
|
|
{
|
|
var meter = new ZeroFlowGenesisMeter(slotNr, 3, false);
|
|
this.meterBatch.AddMeter(meter);
|
|
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
meter.Login();
|
|
|
|
if (!meter.IsLoggedOn)
|
|
{
|
|
Thread.Sleep(2000);
|
|
meter.Login();
|
|
}
|
|
|
|
if (!meter.IsLoggedOn)
|
|
{
|
|
SetEnabled(ProgrammState.Start);
|
|
return;
|
|
}
|
|
|
|
this.currentGenesis = meter;
|
|
|
|
meter.InitMeasurement();
|
|
|
|
meter.WriteRegister(Register.Genesisflow.SampleRate, (byte)10, checkRegister: true);
|
|
meter.WriteRegister("GENESISFLOW_MaxValidDeltaToF", 4294967295, checkRegister: true);
|
|
meter.WriteRegister("GENESISFLOW_MaxValidToF", 4294967295, checkRegister: true);
|
|
meter.WriteRegister("GENESISFLOW_MinValidToF", (uint)0, checkRegister: true);
|
|
|
|
meter.StartMeasurement();
|
|
|
|
InvokeOnUIThread(new Action(() =>
|
|
{
|
|
txtLuts.Text = GetLutsText(meter.PcbId);
|
|
|
|
if (lutIsMissing)
|
|
{
|
|
txtLuts.BackColor = Color.IndianRed;
|
|
}
|
|
else
|
|
{
|
|
txtLuts.BackColor = Color.LightGreen;
|
|
}
|
|
|
|
this.pcbidTextBox.Text = meter.PcbId;
|
|
this.SetEnabled(ProgrammState.Connected);
|
|
this.UpdateConnectedMetersList();
|
|
}));
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
txtLuts.Text = "-";
|
|
MessageBox.Show(ex.Message);
|
|
SetEnabled(ProgrammState.Start);
|
|
}
|
|
}
|
|
|
|
private void UpdateConnectedMetersList()
|
|
{
|
|
this.InvokeOnUIThread(() =>
|
|
{
|
|
this.connectedMeters.Clear();
|
|
|
|
foreach (var meter in this.meterBatch.ListOfMeters)
|
|
{
|
|
if (meter is ZeroFlowGenesisMeter gns)
|
|
{
|
|
var pcbId = gns.PcbId;
|
|
var connected = !string.IsNullOrWhiteSpace(pcbId);
|
|
var item = new ListViewItem
|
|
{
|
|
BackColor = connected ? Color.LightGreen : Color.IndianRed,
|
|
Text = pcbId,
|
|
};
|
|
|
|
this.connectedMeters.Items.Add(item);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void btnRefreshLut_Click(Object sender, EventArgs e)
|
|
{
|
|
if (!string.IsNullOrEmpty(currentGenesis?.PcbId))
|
|
{
|
|
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
var a = GetLutsText(currentGenesis.PcbId);
|
|
|
|
InvokeOnUIThread(new Action(() =>
|
|
{
|
|
txtLuts.Text = a;
|
|
if (lutIsMissing)
|
|
{
|
|
txtLuts.BackColor = Color.IndianRed;
|
|
}
|
|
else
|
|
{
|
|
txtLuts.BackColor = Color.LightGreen;
|
|
}
|
|
}));
|
|
});
|
|
}
|
|
else
|
|
{
|
|
txtLuts.Text = "-";
|
|
}
|
|
|
|
}
|
|
|
|
private String GetLutsText(String PcbId)
|
|
{
|
|
var ret = new StringBuilder();
|
|
var resp = LocalWebRequest.GetRequest($"http://10.49.40.25/MeterProcessState/api/TempFlange/GetLut?PcbId={PcbId}");
|
|
|
|
ret.AppendLine($"Ref Temp C°\t|\t AvgTof");
|
|
|
|
var CurrentReqRequierd = new List<RefTempRange>();
|
|
CurrentReqRequierd.AddRange(reftempRangeReq);
|
|
try
|
|
{
|
|
cordonelTempLut = JsonConvert.DeserializeObject<List<CordonelTempLut>>(resp);
|
|
|
|
foreach (var item in cordonelTempLut.OrderBy(a => a.RefTempC))
|
|
{
|
|
CurrentReqRequierd.RemoveAll(rem => item.RefTempC >= rem.Min && item.RefTempC <= rem.Max);
|
|
ret.AppendLine($"{item.RefTempC.ToString("n8")}\t|\t{item.AvgTof}");
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
ret.AppendLine($"No entrys");
|
|
}
|
|
|
|
lutIsMissing = false;
|
|
foreach (var item in CurrentReqRequierd)
|
|
{
|
|
lutIsMissing = true;
|
|
ret.AppendLine($"{item.Min}-{item.Max}\t|\t Missing");
|
|
}
|
|
return ret.ToString();
|
|
}
|
|
|
|
private DateTimeOffset start;
|
|
|
|
private void BtnStartRecord_Click(Object sender, EventArgs e)
|
|
{
|
|
automatic = false;
|
|
start = DateTimeOffset.Now;
|
|
currentGenesis.StartRecordData();
|
|
currentGenesis.WriteRegister<Byte>(Register.Genesisflow.SampleRate, 16, checkRegister: true);
|
|
SetEnabled(ProgrammState.RecordRuning);
|
|
timer2.Enabled = true;
|
|
|
|
}
|
|
|
|
private void BtnStopRecord_Click(Object sender, EventArgs e)
|
|
{
|
|
timer2.Enabled = false;
|
|
OutputAvgTof(true);
|
|
currentGenesis.WriteRegister<Byte>(Register.Genesisflow.SampleRate, 0, checkRegister: true);
|
|
SetEnabled(ProgrammState.RecordStoped);
|
|
|
|
}
|
|
|
|
private void btnAddLut_Click(Object sender, EventArgs e)
|
|
{
|
|
|
|
var req = LocalWebRequest.PostRequestAsync($"http://10.49.40.25/MeterProcessState/api/TempFlange/AddLut?PcbId={currentGenesis.PcbId}&RefTempC={nudRef.Value.ToString().Replace(",", ".")}&AvgTof={lblAvgDutTof.Text.Replace(",", ".")}");
|
|
|
|
|
|
SetEnabled(ProgrammState.Connected);
|
|
}
|
|
|
|
private TempHelper helper;
|
|
private void btnCheck_Click(Object sender, EventArgs e)
|
|
{
|
|
if (btnCheck.Text == "Start Check")
|
|
{
|
|
short port = 0;
|
|
if (short.TryParse(txtComport.Text, out port))
|
|
{
|
|
helper = new TempHelper();
|
|
TempHelper.UniversalOpenCom((port), 4800, 8, 0, 0);
|
|
|
|
|
|
}
|
|
|
|
currentGenesis.StartRecordData();
|
|
timer1.Enabled = true;
|
|
|
|
btnCheck.Text = "Stop Check";
|
|
}
|
|
else
|
|
{
|
|
|
|
currentGenesis.StopRecordData();
|
|
timer1.Enabled = false;
|
|
btnCheck.Text = "Start Check";
|
|
}
|
|
}
|
|
|
|
private static Decimal interPol(List<CordonelTempLut> points, Double x)
|
|
{
|
|
var x1 = 0.000;
|
|
var y1 = 0.000;
|
|
var x2 = 0.000;
|
|
var y2 = 0.000;
|
|
Decimal checkTempMeter;
|
|
if ((x <= points[0].AvgTof) && (x >= points.Last().AvgTof))
|
|
{
|
|
var i = 0;
|
|
foreach (var val in points)
|
|
{
|
|
if (points[i].AvgTof <= x)
|
|
{
|
|
x1 = points[i - 1].AvgTof;
|
|
y1 = points[i - 1].RefTempC;
|
|
x2 = points[i].AvgTof;
|
|
y2 = points[i].RefTempC;
|
|
break;
|
|
}
|
|
i = i + 1;
|
|
}
|
|
|
|
checkTempMeter = (Decimal)((y2 - y1) / (x2 - x1) * (x - x1) + y1);
|
|
}
|
|
else
|
|
{
|
|
checkTempMeter = 1;
|
|
}
|
|
|
|
return checkTempMeter;
|
|
}
|
|
|
|
private void timer1_Tick(Object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
|
|
var cRecords = currentGenesis.CurrentRecords.ToList();
|
|
|
|
|
|
if (cRecords != null && cRecords.Any())
|
|
{
|
|
Decimal checkTempMeter = 0;
|
|
|
|
var sumTof = 0.0;
|
|
var countTof = 0;
|
|
foreach (var tofItem in cRecords.Where(c => c.Channel == 2))
|
|
{
|
|
|
|
|
|
if (tofItem.Channel == 2)
|
|
{
|
|
sumTof = tofItem.TotalTimeOfFlightS * 1000 + sumTof;
|
|
countTof = countTof + 1;
|
|
}
|
|
|
|
}
|
|
|
|
var x = sumTof / countTof / 1000;
|
|
|
|
checkTempMeter = interPol(cordonelTempLut, x);
|
|
|
|
var a = new CalibrationRecord();
|
|
|
|
while (currentGenesis.CurrentRecords.TryDequeue(out a))
|
|
{
|
|
// do nothing
|
|
}
|
|
|
|
var refTemp = helper.ReadCurrentDisplayValue().ToString();
|
|
|
|
File.AppendAllLines("Check.txt", new List<string> { $"{currentGenesis.PcbId },{checkTempMeter.ToString()},{refTemp.ToString()} " });
|
|
lblCheck.Text = "DUT:" + checkTempMeter.ToString() + "vs Ref:" + refTemp.ToString();
|
|
|
|
//
|
|
|
|
}
|
|
else
|
|
{
|
|
lblCheck.Text = $"temp NA";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
lblCheck.Text = $"temp NA - {ex.Message}";
|
|
|
|
}
|
|
}
|
|
|
|
private void timer2_Tick(Object sender, EventArgs e)
|
|
{
|
|
if (programmState == ProgrammState.RecordRuning)
|
|
{
|
|
if (automatic)
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
OutputAvgTof();
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
private double OutputAvgTof(Boolean stop = false)
|
|
{
|
|
// TODO: Prüfung der TOF signal ???
|
|
|
|
var avgToF = default(double);
|
|
|
|
try
|
|
{
|
|
StringBuilder cTofs = new StringBuilder();
|
|
var listOfRec = currentGenesis.CurrentRecords.Where(s => s.Channel == 2).ToList();
|
|
|
|
if (stop)
|
|
{
|
|
currentGenesis.StopRecordData();
|
|
}
|
|
|
|
var list = listOfRec.Select(s => new { ToF = s.TotalTimeOfFlightS * 1000, s.Channel });
|
|
|
|
foreach (var item in list.OrderBy(o => o.Channel))
|
|
{
|
|
cTofs.Append(item.Channel);
|
|
cTofs.Append(":");
|
|
cTofs.AppendLine((item.ToF / 1000).ToString());
|
|
}
|
|
|
|
avgToF = list.Average(s => s.ToF) / 1000;
|
|
|
|
lblAvgDutTof.Text = avgToF.ToString();
|
|
txtCurrentTofs.Text = cTofs.ToString();
|
|
lblLines.Text = list.Count().ToString();
|
|
lblDuration.Text = $"{(DateTimeOffset.Now - start).TotalSeconds}S";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
txtCurrentTofs.Text = ex.Message;
|
|
|
|
}
|
|
|
|
return avgToF;
|
|
}
|
|
|
|
private class TempTime
|
|
{
|
|
public TempTime(Double temp)
|
|
{
|
|
date = DateTime.UtcNow;
|
|
Temp = temp;
|
|
}
|
|
|
|
public TempTime(String temp)
|
|
{
|
|
Double tmp = double.MinValue;
|
|
if (double.TryParse(temp, out tmp))
|
|
{
|
|
date = DateTime.UtcNow;
|
|
|
|
Temp = tmp;
|
|
}
|
|
}
|
|
|
|
public DateTime date;
|
|
public Double Temp;
|
|
}
|
|
|
|
private class ToFTime
|
|
{
|
|
public ToFTime(Double temp, DateTimeOffset date)
|
|
{
|
|
Date = date.ToUniversalTime().DateTime;
|
|
Tof = temp;
|
|
}
|
|
|
|
public DateTime Date;
|
|
public Double Tof;
|
|
}
|
|
|
|
private ConcurrentQueue<TempTofCalc> listTempTime = new ConcurrentQueue<TempTofCalc>();
|
|
|
|
private void RunTemConnection()
|
|
{
|
|
using (var pipeClient = new NamedPipeClientStream(".", "TempPipe", PipeDirection.In))
|
|
{
|
|
// Connect to the pipe or wait until the pipe is available.
|
|
//Console.Write("Attempting to connect to pipe...");
|
|
pipeClient.Connect();
|
|
|
|
//Console.WriteLine("Connected to pipe.");
|
|
//Console.WriteLine("There are currently {0} pipe server instances open.",
|
|
// pipeClient.NumberOfServerInstances);
|
|
using (var sr = new StreamReader(pipeClient))
|
|
{
|
|
string temp;
|
|
|
|
while ((temp = sr.ReadLine()) != null)
|
|
{
|
|
CalibrationRecord record;
|
|
Double avgTof = 0;
|
|
Double reftemp = 0;
|
|
if (double.TryParse(temp, out reftemp))
|
|
{
|
|
avgTof = 0;
|
|
var i = 0;
|
|
while (currentGenesis.CurrentRecords.TryDequeue(out record))
|
|
{
|
|
if (record.Channel == 2)
|
|
{
|
|
avgTof = avgTof + record.TotalTimeOfFlightS;
|
|
i = 1 + i;
|
|
}
|
|
|
|
}
|
|
if (i > 0)
|
|
{
|
|
|
|
|
|
avgTof = avgTof / i;
|
|
if (!double.IsNaN(avgTof))
|
|
{
|
|
listTempTime.Enqueue(new TempTofCalc() { RefDate = DateTime.Now, RefTemp = reftemp, Tof = avgTof });
|
|
if (avgTof.ToString().Contains("a"))
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private List<ToFTime> toFTimes = new List<ToFTime>();
|
|
private void MergeToAutoFile()
|
|
{
|
|
List<TempTofCalc> itimestowri = new List<TempTofCalc>();
|
|
TempTofCalc lItem;
|
|
while (listTempTime.TryDequeue(out lItem))
|
|
{
|
|
itimestowri.Add(lItem);
|
|
}
|
|
var fiName = $"{currentGenesis.PcbId}_{start.ToString("yyyy_MM_dd_HH-mm-ss")}.TempTof";
|
|
String jsonData = JsonConvert.SerializeObject(itimestowri.ToList());
|
|
jsonData = jsonData.TrimStart('[').TrimEnd(']');
|
|
if (File.Exists(fiName))
|
|
{
|
|
jsonData = $",{jsonData}";
|
|
}
|
|
|
|
File.AppendAllText(fiName, jsonData);
|
|
}
|
|
|
|
//private Int32 PlateauMinLengt = 15;
|
|
//private Double TempConstMarker = 0.01;
|
|
private List<TempTofCalc> toGrid = new List<TempTofCalc>();
|
|
|
|
private void timer3_Tick(Object sender, EventArgs e)
|
|
{
|
|
MergeToAutoFile();
|
|
}
|
|
|
|
private Double generateTof(Random r, Double temp)
|
|
{
|
|
var tmp = r.Next(1, 5);
|
|
var retrn = temp / 9000 - Math.Pow(temp, 1.6) / 100_000;
|
|
|
|
return retrn - (retrn * tmp / 100 + Math.Pow(tmp, 1.6) / 100_000);
|
|
}
|
|
|
|
private void TestBtn_Click(Object sender, EventArgs e)
|
|
{
|
|
List<TempTofCalc> refTempss = new List<TempTofCalc>();
|
|
var sDate = DateTime.UtcNow;
|
|
Double refTemp = 5.0;
|
|
for (Int32 i = 0; i <= 5; i++)
|
|
{
|
|
var needUp = 2.5;
|
|
var rramp = new Random();
|
|
var rtof = new Random();
|
|
|
|
//plateau
|
|
while (needUp >= 0)
|
|
{
|
|
|
|
var temp = rramp.NextDouble() / (needUp * 100); // +0
|
|
needUp = needUp - temp;
|
|
refTemp = refTemp + temp;
|
|
refTempss.Add(new TempTofCalc() { RefDate = sDate.AddSeconds(refTempss.Count), RefTemp = refTemp, Tof = generateTof(rtof, refTemp) });
|
|
}
|
|
|
|
var ra = new Random();
|
|
var tillI = ra.Next(150, 4000); // +0
|
|
var r = new Random(tillI);
|
|
for (Int32 platI = 0; platI < tillI; platI++)
|
|
{
|
|
|
|
|
|
var temp = r.NextDouble() / 5;
|
|
refTempss.Add(new TempTofCalc() { RefDate = sDate.AddSeconds(refTempss.Count), RefTemp = refTemp + temp, Tof = generateTof(rtof, refTemp) });
|
|
}
|
|
}
|
|
|
|
var jsonData = JsonConvert.SerializeObject(refTempss);
|
|
File.AppendAllText($"124563_{start.ToString("yyyy_MM_dd_HH-mm-ss")}.TempTof", jsonData);
|
|
}
|
|
|
|
private void InitializeContent()
|
|
{
|
|
this.mainMenuStrip.Padding = new Padding(5);
|
|
|
|
var menuFont = this.mainMenuStrip.Font;
|
|
var fontBold = new Font(menuFont.FontFamily, menuFont.Size, FontStyle.Bold);
|
|
|
|
this.slotsLabel = new ToolStripLabel
|
|
{
|
|
Text = "Slot: ",
|
|
AutoSize = true,
|
|
Font = fontBold,
|
|
ForeColor = Color.Gray
|
|
};
|
|
|
|
this.slotsList = new ToolStripComboBox
|
|
{
|
|
Items = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 },
|
|
SelectedIndex = 0,
|
|
AutoSize = true,
|
|
FlatStyle = FlatStyle.System,
|
|
DropDownStyle = ComboBoxStyle.DropDownList,
|
|
};
|
|
|
|
this.pcbidLabel = new ToolStripLabel
|
|
{
|
|
Text = "Pcb Id: ",
|
|
AutoSize = true,
|
|
TextAlign = ContentAlignment.MiddleRight,
|
|
Font = fontBold,
|
|
Margin = new Padding(10, 0, 0, 0),
|
|
ForeColor = Color.Gray
|
|
};
|
|
|
|
this.pcbidTextBox = new ToolStripTextBox
|
|
{
|
|
Text = "n/a",
|
|
AutoSize = true,
|
|
TextAlign = ContentAlignment.MiddleRight,
|
|
BorderStyle = BorderStyle.None,
|
|
ReadOnly = true,
|
|
BackColor = this.slotsList.BackColor
|
|
};
|
|
|
|
this.connectBtn = new ToolStripButton
|
|
{
|
|
Text = "Connect",
|
|
AutoSize = true,
|
|
Font = fontBold,
|
|
TextAlign = ContentAlignment.MiddleCenter,
|
|
Margin = new Padding(10, 0, 0, 0),
|
|
Padding = new Padding(10, 0, 10, 0),
|
|
BackColor = Color.LightGray,
|
|
Visible = true
|
|
};
|
|
this.connectBtn.Click += this.BtnConnect_Click;
|
|
|
|
this.disconnectBtn = new ToolStripButton
|
|
{
|
|
Text = "Disconnect",
|
|
AutoSize = true,
|
|
Font = fontBold,
|
|
TextAlign = ContentAlignment.MiddleCenter,
|
|
Margin = new Padding(10, 0, 0, 0),
|
|
Padding = new Padding(10, 0, 10, 0),
|
|
BackColor = Color.LightGray,
|
|
Visible = true
|
|
};
|
|
this.disconnectBtn.Click += this.BtnDisconnect_Click;
|
|
|
|
this.testBtn = new ToolStripButton
|
|
{
|
|
Text = "Test",
|
|
AutoSize = true,
|
|
Font = fontBold,
|
|
TextAlign = ContentAlignment.MiddleCenter,
|
|
Margin = new Padding(5, 0, 0, 0),
|
|
Padding = new Padding(10, 0, 10, 0),
|
|
BackColor = Color.LightGray
|
|
};
|
|
this.connectBtn.Click += this.TestBtn_Click;
|
|
|
|
this.mainMenuStrip.Items.Add(this.slotsLabel);
|
|
this.mainMenuStrip.Items.Add(this.slotsList);
|
|
this.mainMenuStrip.Items.Add(this.pcbidLabel);
|
|
this.mainMenuStrip.Items.Add(this.pcbidTextBox);
|
|
this.mainMenuStrip.Items.Add(this.connectBtn);
|
|
this.mainMenuStrip.Items.Add(this.disconnectBtn);
|
|
this.mainMenuStrip.Items.Add(this.testBtn);
|
|
|
|
this.multiflancshCalibration.GotFocus += (Object sender, EventArgs e) =>
|
|
{
|
|
this.GMHPort.Text = Appsettings.GMHPort.ToString();
|
|
this.messurementStepMS.Text = Appsettings.MessurementStep.ToString();
|
|
this.messurementDauerMS.Text = Appsettings.MessurementDauer.ToString();
|
|
};
|
|
|
|
this.SetEnabled(ProgrammState.Start);
|
|
}
|
|
|
|
private void BtnDisconnect_Click(Object sender, EventArgs args)
|
|
{
|
|
try
|
|
{
|
|
this.currentGenesis.Dispose();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
finally
|
|
{
|
|
this.meterBatch.RemoveMeter(this.currentGenesis);
|
|
}
|
|
|
|
this.InvokeOnUIThread(new Action(() =>
|
|
{
|
|
this.currentGenesis = null;
|
|
this.txtLuts.Text = string.Empty;
|
|
this.txtLuts.BackColor = Color.White;
|
|
this.pcbidTextBox.Text = "not connected";
|
|
this.SetEnabled(ProgrammState.Start);
|
|
this.UpdateConnectedMetersList();
|
|
}));
|
|
}
|
|
|
|
private void SetEnabled(ProgrammState state)
|
|
{
|
|
this.programmState = state;
|
|
|
|
switch (programmState)
|
|
{
|
|
case ProgrammState.Start:
|
|
this.btnStartRecord.Enabled = false;
|
|
this.btnStopRecord.Enabled = false;
|
|
this.btnAddLut.Enabled = false;
|
|
this.nudRef.Visible = false;
|
|
this.btnRefreshLut.Enabled = false;
|
|
break;
|
|
case ProgrammState.Connected:
|
|
this.btnStartRecord.Enabled = true;
|
|
this.btnStopRecord.Enabled = false;
|
|
this.btnAddLut.Enabled = false;
|
|
this.nudRef.Visible = false;
|
|
this.btnRefreshLut.Enabled = true;
|
|
break;
|
|
case ProgrammState.RecordRuning:
|
|
this.btnStartRecord.Enabled = false;
|
|
this.btnStopRecord.Enabled = true;
|
|
this.btnAddLut.Enabled = false;
|
|
this.nudRef.Visible = false;
|
|
this.btnRefreshLut.Enabled = true;
|
|
break;
|
|
case ProgrammState.RecordStoped:
|
|
this.btnStartRecord.Enabled = true;
|
|
this.btnStopRecord.Enabled = false;
|
|
this.btnAddLut.Enabled = true;
|
|
this.nudRef.Visible = true;
|
|
this.btnRefreshLut.Enabled = true;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
this.startAutomaticBtn.Enabled = true;
|
|
this.stopAutomaticBtn.Enabled = false;
|
|
}
|
|
|
|
private bool automatic = false;
|
|
private AutomaticTemperatureCalibration automaticCalibration;
|
|
|
|
private void StartAutomaticBtn_Clicked(object sender, EventArgs args)
|
|
{
|
|
if (this.connectedMeters.Items.Count == 0)
|
|
{
|
|
MessageBox.Show(this, "There is no meter not connected!", "Start aborted", MessageBoxButtons.OK);
|
|
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
this.automatic = true;
|
|
this.start = DateTimeOffset.Now;
|
|
this.automaticCalibration = new AutomaticTemperatureCalibration(this);
|
|
this.automaticCalibration.StartMessuring();
|
|
this.SetEnabled(ProgrammState.RecordRuning);
|
|
|
|
this.startAutomaticBtn.Enabled = false;
|
|
this.stopAutomaticBtn.Enabled = true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.AddMessage($"{e}");
|
|
}
|
|
}
|
|
|
|
private void StopAutomaticBtn_Clicked(object sender, EventArgs args)
|
|
{
|
|
try
|
|
{
|
|
this.automatic = false;
|
|
|
|
this.MergeToAutoFile();
|
|
|
|
this.automaticCalibration.StopMessuring();
|
|
this.meterBatch.Dispose();
|
|
this.meterBatch.RemoveAllMeters();
|
|
|
|
this.SetEnabled(ProgrammState.RecordStoped);
|
|
|
|
this.startAutomaticBtn.Enabled = true;
|
|
this.stopAutomaticBtn.Enabled = false;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.AddMessage($"{e}");
|
|
}
|
|
}
|
|
|
|
public void StartRecording()
|
|
{
|
|
foreach (var meter in this.meterBatch.ListOfMeters)
|
|
{
|
|
if (meter is ZeroFlowGenesisMeter zfgnsm)
|
|
{
|
|
zfgnsm.StartRecordData();
|
|
}
|
|
}
|
|
}
|
|
|
|
private const int TAKE_LINES = 50;
|
|
|
|
public void AddMessage(String message)
|
|
{
|
|
this.BeginInvoke(new Action(() =>
|
|
{
|
|
try
|
|
{
|
|
var text = $"{message}{Environment.NewLine}" + this.automaticContentBox.Text;
|
|
text = text.Substring(0, Math.Min(2000, text?.Length ?? 0));
|
|
|
|
this.automaticContentBox.ResetText();
|
|
this.automaticContentBox.AppendText(text);
|
|
this.automaticContentBox.ScrollBars = RichTextBoxScrollBars.None;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// TODO: implement logging.
|
|
}
|
|
}));
|
|
}
|
|
|
|
protected override void OnClosed(EventArgs args)
|
|
{
|
|
this.automatic = false;
|
|
|
|
if (this.automaticCalibration != null)
|
|
{
|
|
this.automaticCalibration.StopMessuring();
|
|
this.meterBatch.Dispose();
|
|
}
|
|
|
|
try
|
|
{
|
|
this.MergeToAutoFile();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.AddMessage($"{e}");
|
|
}
|
|
|
|
base.OnClosed(args);
|
|
}
|
|
|
|
protected override void OnFormClosed(FormClosedEventArgs e)
|
|
=> this.BtnDisconnect_Click(default(Object), EventArgs.Empty);
|
|
|
|
private void SaveSettingsBtn_Click(Object sender, EventArgs e)
|
|
{
|
|
if (short.TryParse($"{this.GMHPort.Text}", out short gmhPort))
|
|
{
|
|
Appsettings.GMHPort = gmhPort;
|
|
}
|
|
|
|
if (int.TryParse($"{this.messurementStepMS.Text}", out var msStep))
|
|
{
|
|
Appsettings.MessurementStep = msStep;
|
|
}
|
|
|
|
if (int.TryParse($"{this.messurementDauerMS.Text}", out var msDauer))
|
|
{
|
|
Appsettings.MessurementDauer = msDauer;
|
|
}
|
|
|
|
Appsettings.SaveSettings();
|
|
}
|
|
|
|
public void InvokeOnUIThread(Action action)
|
|
=> this.Invoke(action);
|
|
|
|
|
|
|
|
public bool StopLED(int slot)
|
|
{
|
|
try
|
|
{
|
|
|
|
|
|
foreach (var meter in this.meterBatch.ListOfMeters.Where(m => m.Slot == slot))
|
|
{
|
|
if (meter is ZeroFlowGenesisMeter zfgnsm)
|
|
{
|
|
if (!zfgnsm.IsLoggedOn)
|
|
{
|
|
zfgnsm.ReLogin();
|
|
}
|
|
|
|
zfgnsm.SetLcdText(false, new Byte[] { 0x10, 0x10 });
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
public bool StartLED(int slot)
|
|
{
|
|
try
|
|
{
|
|
|
|
|
|
foreach (var meter in this.meterBatch.ListOfMeters.Where(m => m.Slot == slot))
|
|
{
|
|
if (meter is ZeroFlowGenesisMeter zfgnsm)
|
|
{
|
|
if (!zfgnsm.IsLoggedOn)
|
|
{
|
|
zfgnsm.ReLogin();
|
|
}
|
|
|
|
zfgnsm.SetLcdText(true, new byte[] { 0x00, 0x00 });
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
public IDictionary<String, Double> GetAverageTOFForeachMeter(int Slot)
|
|
{
|
|
var results = new Dictionary<string, double>();
|
|
|
|
foreach (var meter in this.meterBatch.ListOfMeters.Where(m => m.Slot == Slot ) )
|
|
{
|
|
if (meter is ZeroFlowGenesisMeter zfgnsm)
|
|
{
|
|
var avg = default(Double);
|
|
|
|
try
|
|
{
|
|
avg = zfgnsm
|
|
.CurrentRecords
|
|
.Where(s => s.Channel == 2)
|
|
.Average(x => x.TotalTimeOfFlightS);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.AddMessage(e.Message);
|
|
}
|
|
finally
|
|
{
|
|
zfgnsm.StartRecordData();
|
|
}
|
|
|
|
var pcbId = zfgnsm.PcbId;
|
|
|
|
if (string.IsNullOrWhiteSpace(pcbId))
|
|
{
|
|
pcbId = "---------";
|
|
}
|
|
|
|
results[pcbId] = avg;
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
private void MeterSelected(Object sender, ListViewItemSelectionChangedEventArgs e)
|
|
{
|
|
var pcbId = e?.Item?.Text;
|
|
|
|
this.InvokeOnUIThread(() =>
|
|
{
|
|
foreach (var meter in this.meterBatch.ListOfMeters)
|
|
{
|
|
if (meter is ZeroFlowGenesisMeter zfgnsm && zfgnsm.PcbId == pcbId)
|
|
{
|
|
this.currentGenesis = zfgnsm;
|
|
this.pcbidTextBox.Text = pcbId;
|
|
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
class tempEntry
|
|
{
|
|
public DateTime dt;
|
|
public double refT;
|
|
public double tof;
|
|
|
|
public double MaxRefTPast;
|
|
public double MaxRefTFutrue;
|
|
|
|
|
|
}
|
|
private void button1_Click(Object sender, EventArgs e)
|
|
{
|
|
var of = new OpenFileDialog();
|
|
var r = of.ShowDialog();
|
|
|
|
if (r == DialogResult.OK)
|
|
{
|
|
var lines = File.ReadAllLines(of.FileName);
|
|
|
|
var allData = new Dictionary<int, List<tempEntry>>();
|
|
foreach (var item in lines)
|
|
{
|
|
var it = item.Split(';');
|
|
if (it.Length >= 4 && DateTime.TryParse(it[0], out var dtTemp ) && double.TryParse(it[1], out var refTempTemp) && int.TryParse(it[2], out var pcbTemp) && double.TryParse(it[3], out var tofTemp))
|
|
{
|
|
if (!allData.ContainsKey(pcbTemp))
|
|
{
|
|
allData.Add(pcbTemp, new List<tempEntry>());
|
|
}
|
|
allData[pcbTemp].Add(new tempEntry() { dt = dtTemp, refT = refTempTemp, tof = tofTemp });
|
|
|
|
|
|
}
|
|
}
|
|
|
|
foreach (var pcb in allData.Keys)
|
|
{
|
|
|
|
for (int i = 20; i < allData[pcb].Count - 20; i++)
|
|
{
|
|
|
|
allData[pcb][i].MaxRefTPast = Math.Abs(allData[pcb].GetRange(i - 20, 20).Average(a => a.refT) - allData[pcb][i].refT);
|
|
allData[pcb][i].MaxRefTFutrue = Math.Abs(allData[pcb].GetRange(i, 20).Average(a => a.refT) - allData[pcb][i].refT);
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
private void button2_Click(Object sender, EventArgs e)
|
|
{
|
|
|
|
var resp = LocalWebRequest.GetRequest($"http://10.49.40.25/MeterProcessState/api/TempFlange/GetLut?PcbId={txtInterPolPcbId.Text}", 50000);
|
|
var cordonelTempLut = JsonConvert.DeserializeObject<List<CordonelTempLut>>(resp);
|
|
var TempFromTofLUT = new List<Tuple<double, double>>();
|
|
|
|
foreach (var item in cordonelTempLut.OrderBy(a => a.RefTempC))
|
|
{
|
|
TempFromTofLUT.Add(new Tuple<double, double>(item.AvgTof, item.RefTempC));
|
|
}
|
|
var result = new StringBuilder();
|
|
var list = new List<string>(
|
|
txtInterpolTofs.Text.Split(new string[] { "\r\n" },
|
|
StringSplitOptions.RemoveEmptyEntries));
|
|
foreach (var item in list)
|
|
{
|
|
var tof = 0.0;
|
|
if (double.TryParse(item, out tof))
|
|
{
|
|
result.AppendLine(interPol(cordonelTempLut, tof).ToString());
|
|
}
|
|
else
|
|
{
|
|
result.AppendLine("NAN");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
private void btnWirte_Click(Object sender, EventArgs e)
|
|
{
|
|
|
|
foreach (var meter in this.meterBatch.ListOfMeters)
|
|
{
|
|
if (meter is GenesisMeter gen)
|
|
{
|
|
gen.WriteRegister("GENESISFLOW_SampleRate", (int)nudSampleRate.Value);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void BtnWritetemp_Click(Object sender, EventArgs e)
|
|
{
|
|
foreach (var meter in this.meterBatch.ListOfMeters)
|
|
{
|
|
if (meter is GenesisMeter gen)
|
|
{
|
|
UInt32 TempToRegister = (UInt32)(nudTemp.Value * 4096);
|
|
gen.WriteRegister(Register.Genesisflow.ToFTempCalibrate, TempToRegister);
|
|
gen.Logout();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void brnRead_Click(Object sender, EventArgs e)
|
|
{
|
|
foreach (var meter in this.meterBatch.ListOfMeters)
|
|
{
|
|
if (meter is GenesisMeter gen)
|
|
{
|
|
gen.ReLogin();
|
|
lblTof1.Text = BitConverter.ToInt32(gen.ReadRegister(Register.Genesisflow.ToFTempOffset1), 0).ToString();
|
|
lblTof2.Text = BitConverter.ToInt32(gen.ReadRegister(Register.Genesisflow.ToFTempOffset2), 0).ToString();
|
|
lblTof3.Text = BitConverter.ToInt32(gen.ReadRegister(Register.Genesisflow.ToFTempOffset3), 0).ToString();
|
|
|
|
nudZeroOffset1.Value = BitConverter.ToInt32(gen.ReadRegister(Register.Genesisflow.ZeroOffset1), 0);
|
|
nudZeroOffset2.Value = BitConverter.ToInt32(gen.ReadRegister(Register.Genesisflow.ZeroOffset2), 0);
|
|
nudZeroOffset3.Value = BitConverter.ToInt32(gen.ReadRegister(Register.Genesisflow.ZeroOffset3), 0);
|
|
|
|
|
|
nudCal1.Value = BitConverter.ToInt32(gen.ReadRegister(Register.Genesisflow.CalFactor1), 0);
|
|
nudCal2.Value = BitConverter.ToInt32(gen.ReadRegister(Register.Genesisflow.CalFactor2), 0);
|
|
nudCal3.Value = BitConverter.ToInt32(gen.ReadRegister(Register.Genesisflow.CalFactor3), 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void button3_Click(Object sender, EventArgs e)
|
|
{
|
|
|
|
foreach (var meter in this.meterBatch.ListOfMeters)
|
|
{
|
|
if (meter is GenesisMeter gen)
|
|
{
|
|
gen.ReLogin();
|
|
|
|
gen.WriteRegister(Register.Genesisflow.ZeroOffset1, nudZeroOffset1.Value);
|
|
gen.WriteRegister(Register.Genesisflow.ZeroOffset2, nudZeroOffset2.Value);
|
|
gen.WriteRegister(Register.Genesisflow.ZeroOffset3, nudZeroOffset3.Value);
|
|
|
|
var r = gen.WriteRegister(Register.Genesisflow.CalFactor1, (UInt16)nudCal1.Value, true,true);
|
|
gen.WriteRegister(Register.Genesisflow.CalFactor2, (UInt16)nudCal2.Value, true, true);
|
|
gen.WriteRegister(Register.Genesisflow.CalFactor3, (UInt16)nudCal3.Value, true, true);
|
|
|
|
gen.WriteRegister(Register.Genesisflow.StoreCalibration, 1);
|
|
|
|
gen.Logout();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void ReCalibration_Click(Object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void btnStartM_Click(Object sender, EventArgs e)
|
|
{
|
|
|
|
foreach (var meter in this.meterBatch.ListOfMeters)
|
|
{
|
|
if (meter is GenesisMeter gen)
|
|
{
|
|
gen.StartMeasurement();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnRefeshM_Click(Object sender, EventArgs e)
|
|
{
|
|
foreach (var meter in this.meterBatch.ListOfMeters)
|
|
{
|
|
if (meter is GenesisMeter gen)
|
|
{
|
|
try
|
|
{
|
|
var r = gen.GetAllMeasurementResults(null, null, true);
|
|
foreach (var item in r)
|
|
{
|
|
|
|
switch (item.Channel)
|
|
{
|
|
case 0 :
|
|
lblFlowDisplay.Text = item.DutFlowRateCmPh.ToString();
|
|
break;
|
|
|
|
case 1:
|
|
lblFlowChnl1.Text = item.DutFlowRateCmPh.ToString();
|
|
break;
|
|
case 2:
|
|
lblFlowChnl2.Text = item.DutFlowRateCmPh.ToString();
|
|
break;
|
|
|
|
case 3:
|
|
lblFlowChnl3.Text = item.DutFlowRateCmPh.ToString();
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
throw;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnStopM_Click(Object sender, EventArgs e)
|
|
{
|
|
foreach (var meter in this.meterBatch.ListOfMeters)
|
|
{
|
|
if (meter is GenesisMeter gen)
|
|
{
|
|
gen.GetAllMeasurementResults(null, null, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|