92 lines
3.9 KiB
C#
92 lines
3.9 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO.Ports;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Common
|
|
{
|
|
public class Utils
|
|
{
|
|
public static string GetTestName(string name, int repeats, int repetitionNr)
|
|
{
|
|
if (name == null) return null;
|
|
if (repeats == 1) return name;
|
|
return string.Format("{0} ({1}/{2})", name, repetitionNr, repeats);
|
|
}
|
|
|
|
public static void CalculateFeatureVector(Iperl.OptoTelegramRaw[] optoData, int optoDataCount, int startIx, int endIx, ref float[] x)
|
|
{
|
|
if (startIx < 0 || endIx >= optoDataCount || startIx >= endIx) return;
|
|
|
|
int n = 0;
|
|
Int64 sumFlow = 0;
|
|
Int64 sumFlow2 = 0;
|
|
Int64 sumEmf = 0;
|
|
Int64 sumEmf2 = 0;
|
|
Int64 sumMagF = 0;
|
|
Int64 sumMagF2 = 0;
|
|
Int64 sumImp = 0;
|
|
Int64 sumImp2 = 0;
|
|
|
|
for (int ix = startIx; ix <= endIx; ix++)
|
|
{
|
|
if (optoData[ix].Flags == Iperl.OptoTelegramFlags.OK ||
|
|
optoData[ix].Flags == Iperl.OptoTelegramFlags.OK_TestStart ||
|
|
optoData[ix].Flags == Iperl.OptoTelegramFlags.OK_TestEnd)
|
|
{
|
|
n++;
|
|
sumFlow += optoData[ix].FlowRaw;
|
|
sumFlow2 += ((int)optoData[ix].FlowRaw * (int)optoData[ix].FlowRaw);
|
|
sumEmf += (int)optoData[ix].EmfRaw;
|
|
sumEmf2 += ((Int64)optoData[ix].EmfRaw * (Int64)optoData[ix].EmfRaw);
|
|
sumMagF += optoData[ix].MagneticFieldRaw;
|
|
sumMagF2 += ((int)optoData[ix].MagneticFieldRaw * (int)optoData[ix].MagneticFieldRaw);
|
|
sumImp += optoData[ix].Impedance;
|
|
sumImp2 += ((int)optoData[ix].Impedance * (int)optoData[ix].Impedance);
|
|
}
|
|
}
|
|
|
|
if (n >= 2)
|
|
{
|
|
if (x.Length > 0) x[0] = ((float)sumFlow2 - (float)sumFlow * (float)sumFlow / n) / (n - 1);
|
|
if (x.Length > 1) x[1] = ((float)sumEmf2 - (float)sumEmf * (float)sumEmf / n) / (n - 1);
|
|
if (x.Length > 2) x[2] = ((float)sumMagF2 - (float)sumMagF * (float)sumMagF / n) / (n - 1);
|
|
if (x.Length > 3) x[3] = ((float)sumImp2 - (float)sumImp * (float)sumImp / n) / (n - 1);
|
|
}
|
|
}
|
|
|
|
public static Parity GetParity(string str, Parity defaultParity = Parity.None)
|
|
{
|
|
if (str.Equals(Parity.None.ToString())) return Parity.None;
|
|
if (str.Equals(Parity.Even.ToString())) return Parity.Even;
|
|
if (str.Equals(Parity.Odd.ToString())) return Parity.Odd;
|
|
if (str.Equals(Parity.Mark.ToString())) return Parity.Mark;
|
|
if (str.Equals(Parity.Space.ToString())) return Parity.Space;
|
|
return defaultParity;
|
|
}
|
|
|
|
public static StopBits GetStopBits(string str, StopBits defaultStopBits = StopBits.One)
|
|
{
|
|
if (str.Equals(StopBits.None.ToString())) return StopBits.None;
|
|
if (str.Equals(StopBits.One.ToString())) return StopBits.One;
|
|
if (str.Equals(StopBits.OnePointFive.ToString())) return StopBits.OnePointFive;
|
|
if (str.Equals(StopBits.Two.ToString())) return StopBits.Two;
|
|
return defaultStopBits;
|
|
}
|
|
|
|
public static Handshake GetHandshake(string str, Handshake defaultHandshake = Handshake.None)
|
|
{
|
|
if (str.Equals(Handshake.None.ToString())) return Handshake.None;
|
|
if (str.Equals(Handshake.RequestToSend.ToString())) return Handshake.RequestToSend;
|
|
if (str.Equals(Handshake.XOnXOff.ToString())) return Handshake.XOnXOff;
|
|
if (str.Equals(Handshake.RequestToSendXOnXOff.ToString())) return Handshake.RequestToSendXOnXOff;
|
|
return defaultHandshake;
|
|
}
|
|
}
|
|
}
|