-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
120 lines (103 loc) · 5.09 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using At.Matus.StatisticPod;
using Bev.Instruments.P9710;
namespace optometer
{
class Program
{
readonly static string fatSeparator = new string('=', 80);
readonly static string thinSeparator = new string('-', 80);
public static void Main(string[] args)
{
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
DateTime timeStamp = DateTime.UtcNow;
string appName = Assembly.GetExecutingAssembly().GetName().Name;
var appVersion = Assembly.GetExecutingAssembly().GetName().Version;
string appVersionString = $"{appVersion.Major}.{appVersion.Minor}";
Options options = new Options();
if (!CommandLine.Parser.Default.ParseArgumentsStrict(args, options))
Console.WriteLine("*** ParseArgumentsStrict returned false");
var streamWriter = new StreamWriter(options.LogFileName, true);
var device = new P9710(options.Port);
var stp = new StatisticPod("Statistics");
if (options.MaximumSamples < 2) options.MaximumSamples = 2;
DisplayOnly("");
LogOnly(fatSeparator);
DisplayOnly($"Application: {appName} {appVersionString}");
LogOnly($"Application: {appName} {appVersion}");
LogAndDisplay($"StartTimeUTC: {timeStamp:dd-MM-yyyy HH:mm}");
LogAndDisplay($"InstrumentID: {device.InstrumentManufacturer} {device.InstrumentID}");
LogAndDisplay($"Battery: {device.InstrumentBatteryLevel} %");
LogAndDisplay($"DetectorID: {device.DetectorID}");
LogAndDisplay($"Calibration: {device.DetectorCalibrationFactor} A/({device.DetectorPhotometricUnit})");
LogAndDisplay($"Samples (n): {options.MaximumSamples}");
LogAndDisplay($"Comment: {options.UserComment}");
LogOnly(fatSeparator);
DisplayOnly("");
DisplayOnly("press any key to start a measurement - 'q' to quit");
int measurementIndex = 0;
while (Console.ReadKey(true).Key != ConsoleKey.Q)
{
measurementIndex++;
DisplayOnly("");
DisplayOnly($"Measurement #{measurementIndex}");
stp.Restart();
timeStamp = DateTime.UtcNow;
while (stp.SampleSize < options.MaximumSamples)
{
double current = device.GetCurrent();
stp.Update(current);
DisplayOnly($"{stp.SampleSize,4}: {current * 1e9:F4} nA");
}
double stdDev = stp.StandardDeviation;
double uSpecs = device.GetMeasurementUncertainty(stp.AverageValue);
double uCombined = Math.Sqrt(stdDev * stdDev + uSpecs * uSpecs);
double sensitivity = device.DetectorCalibrationFactor;
double photValue = stp.AverageValue / sensitivity;
double photU = uCombined / sensitivity;
DisplayOnly("");
LogOnly($"Measurement number: {measurementIndex}");
LogOnly($"Triggered at: {timeStamp:dd-MM-yyyy HH:mm:ss}");
LogAndDisplay($"Average value: {stp.AverageValue * 1e9:F4} nA ({device.EstimateMeasurementRange(stp.AverageValue)})");
LogAndDisplay($"Standard deviation: {stdDev * 1e9:F4} nA");
LogAndDisplay($"Specification uncertainty: {uSpecs * 1e9:F4} nA");
LogAndDisplay($"Combined standard uncertainty: {uCombined * 1e9:F4} nA");
LogAndDisplay($"Photometric value: {photValue:F3} ± {photU:F3} {device.DetectorPhotometricUnit}");
LogOnly(thinSeparator);
DisplayOnly("");
DisplayOnly("press any key to start a measurement - 'q' to quit");
}
DisplayOnly("bye.");
LogOnly("");
LogOnly(fatSeparator);
if (measurementIndex == 1)
LogOnly($"{measurementIndex} measurement logged - StopTimeUTC: {timeStamp:dd-MM-yyyy HH:mm}");
else
LogOnly($"{measurementIndex} measurements logged - StopTimeUTC: {timeStamp:dd-MM-yyyy HH:mm}");
LogOnly(fatSeparator);
LogOnly("");
streamWriter.Close();
/***************************************************/
void LogAndDisplay(string line)
{
DisplayOnly(line);
LogOnly(line);
}
/***************************************************/
void LogOnly(string line)
{
streamWriter.WriteLine(line);
streamWriter.Flush();
}
/***************************************************/
void DisplayOnly(string line)
{
Console.WriteLine(line);
}
/***************************************************/
}
}
}