iPerl opto data logging: calculated data added, TODO: (1) ref.flow, (2) start/end mark.

This commit is contained in:
Milan Hanajik 2015-12-04 13:52:00 +01:00
parent e3aa3f6721
commit 29ece2f91c
3 changed files with 66 additions and 29 deletions

View File

@ -97,6 +97,8 @@ namespace TBF.BenchControl.WaterMeters.iPerl
public enum OptoTelegramFlags : byte
{
OK = 0,
OK_TestStart,
OK_TestEnd,
InvalidTelegram, /// Wrong telegram format of checksum error
SyncError,
}

View File

@ -6,43 +6,50 @@ namespace TBF.BenchControl.WaterMeters.iPerl
public class OptoTelegramRaw
{
public static readonly int Length = 42;
///
/// Stored values
///
public OptoTelegramFlags flags;
public DateTime DateTime;
public int Counter;
public DateTime DateTime; /// From PC
public int Counter;
public UInt32 EmfRaw;
public Int16 MagneticField;
public UInt32 EmfRaw; /// From iPerl opto data
public Int16 MagneticFieldRaw;
public Int16 FlowRaw;
public Int32 VolumeRaw;
public Int16 Impedance;
public Int64 Timestamp;
public byte CheckSum;
///
/// Calculated values
///
public double EMF() { return 0.000000333 * (double)EmfRaw; }
public double MagneticField() { return (double)MagneticFieldRaw; }
public double Flow(double scalingFactor) { return 0.225 * scalingFactor * (double)FlowRaw; }
public double Volume(double scalingFactor) { return 0.0000625 * scalingFactor * (double)VolumeRaw; }
public Int32 FlipTime() { return 0; }
public decimal TimestampDec() { return (decimal)Timestamp / (decimal)8192; }
public double RefFlow() { return 0; }
public double VolumeDelta(double scalingFactor, OptoTelegramRaw previous) { return (previous == null) ? 0 : Volume(scalingFactor) - previous.Volume(scalingFactor); }
public decimal TimeDelta(OptoTelegramRaw previous) { return (previous == null) ? 0 : TimestampDec() - previous.TimestampDec(); }
public string Label()
{
if (flags == OptoTelegramFlags.OK_TestStart) return "#### start test ####";
else if (flags == OptoTelegramFlags.OK_TestEnd) return "#### end of test ####";
else return string.Empty;
}
public OptoTelegramRaw()
{
}
public override string ToString()
public string ToString(double scalingFactor, OptoTelegramRaw previous)
{
if (flags == OptoTelegramFlags.OK)
{
return string.Format("{0}:{1}:{2}.{3}\t{4} :\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}",
DateTime.Hour.ToString("D2"),
DateTime.Minute.ToString("D2"),
DateTime.Second.ToString("D2"),
DateTime.Millisecond.ToString("D3"),
Counter,
EmfRaw.ToString("X6"),
MagneticField.ToString("X4"),
FlowRaw.ToString("X4"),
VolumeRaw.ToString("X6"),
Impedance.ToString("X4"),
Timestamp.ToString("X8"),
CheckSum.ToString("X2"));
}
else if (flags == OptoTelegramFlags.SyncError)
if (flags == OptoTelegramFlags.SyncError)
{
return "Sychronization error";
}
@ -50,9 +57,32 @@ namespace TBF.BenchControl.WaterMeters.iPerl
{
return "Invalid telegram";
}
else
else /// if (flags == OptoTelegramFlags.OK / OptoTelegramFlags.OK_TestStart / OptoTelegramFlags.OK_TestEnd)
{
return "???";
return string.Format("{0}:{1}:{2}.{3}\t{4} :\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}\t{13}\t{14}\t{15}\t{16}\t{17}\t{18}\t{19}\t{20}\t{21}\t{22}",
DateTime.Hour.ToString("D2"),
DateTime.Minute.ToString("D2"),
DateTime.Second.ToString("D2"),
DateTime.Millisecond.ToString("D4"),
Counter,
EmfRaw.ToString("X6"),
MagneticFieldRaw.ToString("X4"),
FlowRaw.ToString("X4"),
VolumeRaw.ToString("X6"),
Impedance.ToString("X4"),
Timestamp.ToString("X8"),
CheckSum.ToString("X2"),
EMF().ToString("F4"),
MagneticField().ToString("F0"),
Flow(scalingFactor).ToString("F2"),
Volume(scalingFactor).ToString("F4"),
FlipTime().ToString("F0"),
TimestampDec().ToString("F4"),
RefFlow().ToString("F2"),
VolumeDelta(scalingFactor, previous).ToString("F4"),
TimeDelta(previous).ToString("F3"),
scalingFactor.ToString("F1"),
Label());
}
}
@ -98,7 +128,7 @@ namespace TBF.BenchControl.WaterMeters.iPerl
}
bool f1 = UInt32.TryParse(telegram.Substring(0, 6), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out EmfRaw);
bool f2 = Int16.TryParse(telegram.Substring(7, 4), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out MagneticField);
bool f2 = Int16.TryParse(telegram.Substring(7, 4), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out MagneticFieldRaw);
bool f3 = Int16.TryParse(telegram.Substring(12, 4), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out FlowRaw);
bool f4 = Int32.TryParse(telegram.Substring(17, 6), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out VolumeRaw);
bool f5 = Int16.TryParse(telegram.Substring(24, 4), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out Impedance);

View File

@ -524,9 +524,14 @@ namespace TBF.BenchControl.WaterMeters.iPerl
directory += (StateMachine.CycleStartDD + "\\");
Directory.CreateDirectory(directory);
TextWriter optoLogFile = new StreamWriter(directory + optoDataLogFileName + ".txt");
for (int i = 0; i < optoDataCount; i++) optoLogFile.WriteLine(optoData[i].ToString());
optoLogFile.Close();
double scalFact = ScalingFactor();
using (TextWriter optoLogFile = new StreamWriter(directory + optoDataLogFileName + ".txt"))
{
optoLogFile.WriteLine(optoData[0].ToString(scalFact, null));
for (int i = 1; i < optoDataCount; i++) optoLogFile.WriteLine(optoData[i].ToString(scalFact, optoData[i - 1]));
optoLogFile.Close();
}
}
void ReadPulses()