160 lines
5.2 KiB
C#
160 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using ScottPlot;
|
|
using System.Diagnostics;
|
|
|
|
|
|
namespace daq_testing
|
|
{
|
|
public partial class UserControlDMM : UserControl
|
|
{
|
|
public double MeasurementValue { get; set; }
|
|
|
|
// Control variables
|
|
private bool isMonitoring = false;
|
|
private List<double> dataPoints = new List<double>();
|
|
private List<double> timePoints = new List<double>();
|
|
private double time = 0;
|
|
private const int MaxPoints = 100;
|
|
|
|
// Tolerance band values
|
|
private double UpperTolerance = 1.0;
|
|
private double LowerTolerance = -1.0;
|
|
|
|
// Histogram parameters
|
|
private int NumBins = 20; // Number of bins for the histogram
|
|
private double MaxVoltage = 1; // Maximum possible voltage for histogram scaling
|
|
private double MinVoltage = -1; // Minimum possible voltage for histogram scaling
|
|
|
|
public UserControlDMM()
|
|
{
|
|
InitializeComponent();
|
|
|
|
// Initialize the graph
|
|
formsPlot1.Plot.Title("Real-Time Voltage");
|
|
formsPlot1.Plot.YLabel("Voltage (V)");
|
|
formsPlot1.Plot.XLabel("Time (s)");
|
|
formsPlot1.Refresh();
|
|
|
|
// Set up the tolerance band once
|
|
UpdateToleranceBand();
|
|
|
|
}
|
|
private void UpdateToleranceBand()
|
|
{
|
|
// Generate tolerance band data
|
|
double[] xs = new double[MaxPoints];
|
|
double[] upperValues = new double[MaxPoints];
|
|
double[] lowerValues = new double[MaxPoints];
|
|
|
|
for (int i = 0; i < MaxPoints; i++)
|
|
{
|
|
xs[i] = i;
|
|
upperValues[i] = UpperTolerance;
|
|
lowerValues[i] = LowerTolerance;
|
|
}
|
|
|
|
// Add the tolerance band as a shaded region between lower and upper bounds
|
|
var fill = formsPlot1.Plot.Add.FillY(
|
|
xs: xs,
|
|
ys1: lowerValues,
|
|
ys2: upperValues
|
|
);
|
|
fill.FillColor = Colors.Green.WithAlpha(20);
|
|
fill.LineColor = Colors.Green;
|
|
fill.MarkerColor = Colors.Green;
|
|
fill.LineWidth = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async void btnMonitor_ClickAsync(object sender, EventArgs e)
|
|
{
|
|
var mainForm = this.FindForm() as MainForm;
|
|
if (mainForm == null) return;
|
|
|
|
formsPlot1.Plot.Clear();
|
|
dataPoints.Clear();
|
|
timePoints.Clear();
|
|
|
|
// Toggle the monitoring state
|
|
isMonitoring = !isMonitoring;
|
|
|
|
if (isMonitoring)
|
|
{
|
|
// Change button text to indicate stopping
|
|
btnMonitor.Text = "Stop Monitoring";
|
|
|
|
// Start the monitoring configuration
|
|
mainForm.DAQ.DAQInstrument.Voltage.DCVoltage.AutoRangeListConfig(tbDataloggerChannel.Text, true);
|
|
mainForm.DAQ.DAQInstrument.Voltage.DCVoltage.Configure(tbDataloggerChannel.Text, 10, 0.01);
|
|
mainForm.DAQ.DAQInstrument.Route.Monitor = Convert.ToInt32(tbDataloggerChannel.Text);
|
|
mainForm.DAQ.DAQInstrument.Route.MonitorEnable = true;
|
|
|
|
Stopwatch sw = new Stopwatch();
|
|
|
|
// Start the loop
|
|
while (isMonitoring)
|
|
{
|
|
|
|
sw.Start();
|
|
// Get the voltage value
|
|
double voltage = mainForm.DAQ.DAQInstrument.Route.MonitorData;
|
|
|
|
// Update the Label
|
|
lbMeasurementValue.Text = $"{voltage:F3} V";
|
|
|
|
// Update the data points
|
|
dataPoints.Add(voltage);
|
|
timePoints.Add(time);
|
|
|
|
if (dataPoints.Count > 100) // Limit to 100 points for better performance
|
|
{
|
|
dataPoints.RemoveAt(0);
|
|
timePoints.RemoveAt(0);
|
|
}
|
|
|
|
// Clear previous plots
|
|
formsPlot1.Plot.Clear();
|
|
|
|
// Re-add the tolerance band
|
|
// UpdateToleranceBand();
|
|
// Update the histogram and plot
|
|
|
|
// Add the updated scatter plot
|
|
formsPlot1.Plot.Add.ScatterPoints(timePoints.ToArray(), dataPoints.ToArray());
|
|
|
|
// Autoscroll: Set X-axis to show the latest MaxPoints data
|
|
formsPlot1.Plot.Axes.SetLimits(timePoints[0], timePoints[timePoints.Count - 1]);
|
|
|
|
|
|
// Refresh the plot
|
|
formsPlot1.Refresh();
|
|
|
|
// Increment time
|
|
time += 0.100;
|
|
|
|
// Wait asynchronously for 100 ms
|
|
await Task.Delay(100);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Change button text to indicate starting
|
|
btnMonitor.Text = "Start Monitoring";
|
|
|
|
// Stop the monitoring
|
|
mainForm.DAQ.DAQInstrument.Route.MonitorEnable = false;
|
|
}
|
|
}
|
|
}
|
|
}
|