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 System.Threading; using System.Text.RegularExpressions; using Maser.Palantir.Model; using System.IO; namespace HTOLHAST { public partial class Form1 : Form { public int PositionIncrement { get { int result; if (int.TryParse(tbPositionIncrement.Text, out result)) { return result; } else { // Handle the case where the conversion fails throw new FormatException("Invalid input for PAMMaxCurrent."); } } set { tbPositionIncrement.Text = value.ToString(); } } public int CurrentPosition { get { int result; if (int.TryParse(tbCurrentPosition.Text, out result)) { return result; } else { // Handle the case where the conversion fails throw new FormatException("Invalid input for PAMMaxCurrent."); } } set { tbCurrentPosition.Text = value.ToString(); } } HMP4040 PSU1; HMP4040 PSU2; Keithley6485 PAM; Agilent34970a DAQ; Project Project; List switchMatrix; List powerUpSequences; List powerDownSequences; BindingList measurements; BindingSource bindingSourceMeasurements = new BindingSource(); List headerSettings = new List(); DataTable dataTablePowersupplySettings = new DataTable(); List powerSupplySettings = new List(); List voltageChannels = new List() { 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 214, 215, 216, 217, 218, 219 }; public double PAMMaxCurrent { get { double result; if (double.TryParse(tbPamMaxCurrent.Text, out result)) { return result; } else { // Handle the case where the conversion fails throw new FormatException("Invalid input for PAMMaxCurrent."); } } } public Form1() { InitializeComponent(); InitializeEvents(); //InitializeDataTablePowerSupplySettings(); //InitializeDgvPowerSupplySettings(); //KeithleyValidationOfMethods(); InitializeButtons(); InitDgvPowersupply(); InitDgvHeaderSetting(); InitDgvMeasurements(); InitSwitchMatrix(); InitializePowerupSequence(); } #region INITIALIZATION private void InitializeButtons() { btnClear.Enabled = false; btnMeasure.Enabled = false; btnNextPosition.Enabled = false; btnSendPsuSettings.Enabled = false; } private void InitDgvMeasurements() { measurements = new BindingList(); bindingSourceMeasurements.DataSource = measurements; dgvMeasurements.DataSource = bindingSourceMeasurements; } private void InitBeginPositionOfSwitches() { foreach (var sw in switchMatrix) { DAQ.CloseChannel(sw.SW1); DAQ.OpenChannel(sw.SW2); DAQ.CloseChannel(sw.SW3); } } private void InitSwitchMatrix() { switchMatrix = new List() { //00926 20 Channel Actuator / GP Switch Module SLOT1 new SwitchMatrix { Channel = 1, SW1 = 102, SW2 = 103, SW3 = 104 }, new SwitchMatrix { Channel = 2, SW1 = 105, SW2 = 106, SW3 = 107 }, new SwitchMatrix { Channel = 3, SW1 = 108, SW2 = 109, SW3 = 110 }, new SwitchMatrix { Channel = 4, SW1 = 111, SW2 = 112, SW3 = 113 }, new SwitchMatrix { Channel = 5, SW1 = 114, SW2 = 115, SW3 = 116 }, new SwitchMatrix { Channel = 6, SW1 = 117, SW2 = 118, SW3 = 119 } }; } public void InitDgvHeaderSetting() { for (int headerpin = 1; headerpin <= 10; headerpin++) { HeaderSetting headerSetting = new HeaderSetting() { HeaderPin = headerpin.ToString(), Channel = voltageChannels[headerpin -1], Description = "-", Setpoint = 0.0, Tolerance = 0.0 }; headerSettings.Add(headerSetting); } dgvHeaderSettings.DataSource = headerSettings; dgvHeaderSettings.Update(); } public void InitDgvPowersupply () { for (int channel = 1; channel <= 6; channel++) { PowerSupplySetting powerSupplySetting = new PowerSupplySetting() { Channel = channel, Description = "-", Voltage = 0.0, Tolerance = 1, CurrentLimit = 0.1, Enabled = false }; powerSupplySettings.Add(powerSupplySetting); } dgvPowerSupplies.DataSource = powerSupplySettings; dgvPowerSupplies.Update(); } public void InitializeDataTablePowerSupplySettings() { dataTablePowersupplySettings.Columns.Add("Channel", typeof(int)); dataTablePowersupplySettings.Columns.Add("Description", typeof(string)); dataTablePowersupplySettings.Columns.Add("Voltage", typeof(double)); dataTablePowersupplySettings.Columns.Add("Tolerance", typeof(double)); dataTablePowersupplySettings.Columns.Add("CurrentLimit", typeof(double)); dataTablePowersupplySettings.Columns.Add("Enabled", typeof(bool)); } public void InitializeDgvPowerSupplySettings() { dgvPowerSupplies.DataSource = dataTablePowersupplySettings; for (int channel = 1; channel <= 6; channel++) { PowerSupplySetting powerSupplySetting = new PowerSupplySetting() { Channel = channel, Description = "-", Voltage = 0.0, Tolerance = 1, CurrentLimit = 0.1, Enabled = false }; dataTablePowersupplySettings.Rows.Add(powerSupplySetting.Channel, powerSupplySetting.Description, powerSupplySetting.Voltage, powerSupplySetting.Tolerance, powerSupplySetting.CurrentLimit, powerSupplySetting.Enabled); } dgvPowerSupplies.Update(); } private void InitializeEvents () { cbCH1.CheckedChanged += OnPowerSupplyCheckboxClicked; cbCH2.CheckedChanged += OnPowerSupplyCheckboxClicked; cbCH3.CheckedChanged += OnPowerSupplyCheckboxClicked; cbCH4.CheckedChanged += OnPowerSupplyCheckboxClicked; cbCH5.CheckedChanged += OnPowerSupplyCheckboxClicked; cbCH6.CheckedChanged += OnPowerSupplyCheckboxClicked; cbCH7.Enabled = false; cbCH8.Enabled = false; } private void InitializePowerupSequence() { powerUpSequences = new List() { new PowerUpSequence { Channel = 1, Delay = 100}, new PowerUpSequence { Channel = 2, Delay = 100}, new PowerUpSequence { Channel = 3, Delay = 100}, new PowerUpSequence { Channel = 4, Delay = 100} }; } private void InitializePowerDownSequence() { powerDownSequences = new List() { new PowerUpSequence { Channel = 1, Delay = 1}, new PowerUpSequence { Channel = 2, Delay = 1}, new PowerUpSequence { Channel = 3, Delay = 1}, new PowerUpSequence { Channel = 4, Delay = 1} }; } public void InitializeInstruments() { // Mandatory instruments: Task.Run(() => InitializePSU1(tbResourceStringPSU1.Text)); Task.Run(() => InitializeDAQ(tbResourceStringDAQ.Text)); // Optional Instruments check if to be used if (cbPSU2Enabled.Checked) { Task.Run(() => InitializePSU2(tbResourceStringPSU2.Text)); } if (cbPAMEnabled.Checked) { Task.Run(() => InitializePAM(tbResourceStringPAM.Text)); } } public void InitializePSU1(string resourceString) { try { PSU1 = new HMP4040(resourceString); foreach (var setting in powerSupplySettings) { if (setting.Channel <=4) { setting.Voltage = PSU1.ReadSetpoint(setting.Channel); } } tbResourceStringPSU1.BackColor = Color.Green; dgvPowerSupplies.Refresh(); } catch (Exception) { tbResourceStringPSU1.BackColor = Color.Red; MessageBox.Show("Unable to Connect to HAMEG 1 PowerSupply"); throw; } } public void InitializePSU2(string resourceString) { try { PSU2 = new HMP4040(resourceString); tbResourceStringPSU2.BackColor = Color.Green; } catch (Exception) { tbResourceStringPSU2.BackColor = Color.Red; MessageBox.Show("Unable to Connect to HAMEG 2 PowerSupply"); throw; } } public void InitializePAM(string resourceString) { try { PAM = new Keithley6485(resourceString); // Query the instrument ID PAM.Reset(); string idnResponse = PAM.GetID(); Console.WriteLine($"Instrument ID: {idnResponse}"); // Measure DC current PAM.PerformZeroCheck(); tbResourceStringPAM.BackColor = Color.Green; } catch (Exception) { tbResourceStringPAM.BackColor = Color.Red; MessageBox.Show("Unable to Connect to PicoAmmeter"); throw; } } public void InitializeDAQ(string resourceString) { try { DAQ = new Agilent34970a(resourceString); tbResourceStringDAQ.BackColor = Color.Green; } catch (Exception e) { tbResourceStringDAQ.BackColor = Color.Red; MessageBox.Show("Unable to Connect to DAQ"); throw; } } #endregion #region INSTRUMENTVALIDATION public void HamegValidationOfMethods() { string resourceString = "ASRL4::INSTR"; // Replace with your instrument's resource string HMP4040 hameg = new HMP4040(resourceString); hameg.GetID(); HMP4040Sample voltageResponse = (HMP4040Sample)hameg.MeasureVolt(1); Console.WriteLine($"Measured Voltage: {voltageResponse.Value} V"); HMP4040Sample currentResponse = (HMP4040Sample)hameg.MeasureCurrent(1); Console.WriteLine($"Measured Current: {currentResponse.Value} A"); string voltageSetpoint = hameg.SetVoltage(1.1, 1); Console.WriteLine($"Voltage Setpoint: {currentResponse} V"); string currentSetpoint = hameg.SetCurrent(0.1, 1); Console.WriteLine($"Current Setpoint: {currentResponse} A"); hameg.PowerUpSequence(hameg.GenerateSimulationPowerUpSequence()); hameg.PowerDownSequence(hameg.GenerateSimulationPowerUpSequence()); } public void KeithleyValidationOfMethods() { // Example usage string resourceString = "ASRL3::INSTR"; // Replace with your instrument's resource string Keithley6485 keithley = new Keithley6485(resourceString); // Query the instrument ID keithley.Reset(); string idnResponse = keithley.GetID(); Console.WriteLine($"Instrument ID: {idnResponse}"); // Measure DC current keithley.PerformZeroCheck(); Keithley6485Sample currentResponse = (Keithley6485Sample) keithley.MeasureDCCurrent(); Console.WriteLine($"Measured Current: {currentResponse.Value}"); // Close the instrument connection keithley.Close(); } public void AgilentValidationOfMethods() { // Example usage string resourceString = "ASRL9::INSTR"; // Replace with your instrument's resource string Agilent34970a agilent = new Agilent34970a(resourceString); // Query the instrument ID agilent.Reset(); string idnResponse = agilent.GetID(); Console.WriteLine($"Instrument ID: {idnResponse}"); //Measure DC Voltage foreach (var channel in voltageChannels) { agilent.ConfigVoltageChannel(channel); Agilent34970aSample sample =(Agilent34970aSample) agilent.MeasureVolt(channel); Console.WriteLine($"Measured voltage CH{channel} : {sample.Value}"); } // Close the instrument connection agilent.Close(); } private void btnInitializeInstruments_Click(object sender, EventArgs e) { InitializeInstruments(); } #endregion private void btnSelectProject_Click(object sender, EventArgs e) { // Create new Form and show in showdialog window. FormSelectProject formSelectProject = new FormSelectProject(); if (formSelectProject.ShowDialog() == DialogResult.OK) { // Get Project information from formSelectProject Form. Project = formSelectProject.Project; // Update Project information UpdateProjectInfoTextboxes(Project); } } private void UpdateProjectInfoTextboxes(Project project) { // Fill in project information textboxes. tbProject.Text = $"Project: P{project.MIDSProject} Sub: {project.MIDSSubProject} Step: {project.MIDSStep}"; tbProjectDescription.Text = project.MIDSProjectDescription; tbSubProjectDescription.Text = project.MIDSSubProjectDescription; tbStepDescription.Text = project.MIDSStepDescription; } public void OnPowerSupplyCheckboxClicked (object sender, EventArgs e) { CheckBox SelectedChannel = (CheckBox)sender; Console.WriteLine(SelectedChannel.Name); int.TryParse(SelectedChannel.Name[4].ToString(), out int channel); //DataRow row = dataTablePowersupplySettings.Rows[channel-1]; //row["Enabled"] = SelectedChannel.Checked; foreach (var setting in powerSupplySettings) { if (setting.Channel == channel) { setting.Enabled = SelectedChannel.Checked; dgvPowerSupplies.Refresh(); } } } private void btnMeasure_Click(object sender, EventArgs e) { InitBeginPositionOfSwitches(); PowerUpPSU(powerUpSequences); MeasurePSUVoltage(); MeasureHeaderVoltage(); MeasureCurrent(); PowerDownPSU(powerUpSequences); // Timestamp, Position, Device, Measurement, Voltage, Current // 2025-01-09, 1, HAMEG1, CH1, 1.0, 0.1 // 2025-01-09, 1, HAMEG2, CH5, 1.0, 0.1 // 2025-01-09, 1, KEITHLEY, CH1, -, 1.0 // 2025-01-09, 1, AGILENT, HEADER1, 1.0, - } private void PowerUpPSU(List powerUpsequences) { PSU1.PowerUpSequence(powerUpsequences); PSU1.SetGeneralOutputState(true); } private void PowerDownPSU(List powerUpsequences) { PSU1.PowerDownSequence(powerUpsequences); PSU1.SetGeneralOutputState(false); } private void MeasureHeaderVoltage() { //02700 20 Channel Multiplexer Module (BLR) SLOT2 foreach (var header in headerSettings) { DAQ.ConfigVoltageChannel(header.Channel); Agilent34970aSample sample =(Agilent34970aSample)DAQ.MeasureVolt(header.Channel); this.measurements.Add(new Measurement { Description = $"Headerpin : {header.HeaderPin}", Device = "Agilent34970A", Position = CurrentPosition, Timestamp = sample.TimeStamp, Value = sample.Value, Unit = sample.Unit }); Console.WriteLine($"Measured voltage CH{header.Channel} : {sample.Value}"); } } private void MeasurePSUVoltage() { var powerSupplies = GetEnabledPowerSupplies(); foreach (var powersupply in powerSupplies) { if (powersupply.Channel <= 4) { HMP4040Sample sample = (HMP4040Sample)PSU1.MeasureVolt(powersupply.Channel); this.measurements.Add(new Measurement { Description = $"PSU1_CH{powersupply.Channel}", Device = "HAMEG4040", Position = CurrentPosition, Timestamp = sample.TimeStamp, Value = sample.Value, Unit = sample.Unit }); sample = (HMP4040Sample)PSU1.MeasureCurrent(powersupply.Channel); this.measurements.Add(new Measurement { Description = $"PSU1_CH{powersupply.Channel}", Device = "HAMEG4040", Position = CurrentPosition, Timestamp = sample.TimeStamp, Value = sample.Value, Unit = sample.Unit }); } else { HMP4040Sample sample = (HMP4040Sample)PSU2.MeasureVolt(powersupply.Channel); this.measurements.Add(new Measurement { Description = $"PSU2_CH{powersupply.Channel}", Device = "HAMEG4040", Position = CurrentPosition, Timestamp = sample.TimeStamp, Value = sample.Value, Unit = sample.Unit }); sample = (HMP4040Sample)PSU2.MeasureCurrent(powersupply.Channel); this.measurements.Add(new Measurement { Description = $"PSU1_CH{powersupply.Channel}", Device = "HAMEG4040", Position = CurrentPosition, Timestamp = sample.TimeStamp, Value = sample.Value, Unit = sample.Unit }); } } } private void MeasureCurrent() { var enabledPowersupplies = GetEnabledPowerSupplies(); foreach (var powersupply in enabledPowersupplies) { if (powersupply.Channel <= 4) { HMP4040Sample current = (HMP4040Sample) PSU1.MeasureCurrent(powersupply.Channel); if (PAM == null) { return; } if (current.Value < PAMMaxCurrent ) { DAQ.CloseChannel(101); // Switch relay for PAM measurement SetSwitchesBeforeCurrentMeasurement(powersupply.Channel); Keithley6485Sample sample = (Keithley6485Sample) PAM.MeasureDCCurrent(); this.measurements.Add(new Measurement { Description = $"PSU1_CH{powersupply.Channel}", Device = "Keithley6485", Position = CurrentPosition, Timestamp = sample.TimeStamp, Value = sample.Value, Unit = sample.Unit }); SetSwitchesAfterCurrentMeasurement(powersupply.Channel); } } else { DAQ.CloseChannel(101); // Switch relay for PAM measurement HMP4040Sample current = (HMP4040Sample)PSU2.MeasureCurrent(powersupply.Channel); if (PAM == null) { return; } if (current.Value < PAMMaxCurrent) { SetSwitchesBeforeCurrentMeasurement(powersupply.Channel); PAM.MeasureDCCurrent(); Keithley6485Sample sample = (Keithley6485Sample)PAM.MeasureDCCurrent(); this.measurements.Add(new Measurement { Description = $"PSU2_CH{powersupply.Channel}", Device = "Keithley6485", Position = CurrentPosition, Timestamp = sample.TimeStamp, Value = sample.Value, Unit = sample.Unit }); SetSwitchesAfterCurrentMeasurement(powersupply.Channel); } } } } private void SetSwitchesBeforeCurrentMeasurement(int channel) { SwitchMatrix switches = switchMatrix.Where(x => x.Channel == channel).First(); DAQ.CloseChannel(switches.SW1); DAQ.CloseChannel(switches.SW2); DAQ.CloseChannel(switches.SW3); DAQ.OpenChannel(switches.SW1); } private void SetSwitchesAfterCurrentMeasurement(int channel) { SwitchMatrix switches = switchMatrix.Where(x => x.Channel == channel).First(); DAQ.CloseChannel(switches.SW1); DAQ.OpenChannel(switches.SW2); DAQ.OpenChannel(switches.SW3); } private List GetEnabledPowerSupplies() { var enabledPowerSupplies = powerSupplySettings.Where(x => x.Enabled == true).ToList(); return enabledPowerSupplies; } private void btnSendPsuSettings_Click(object sender, EventArgs e) { foreach (PowerSupplySetting powerSupplySetting in powerSupplySettings) { PSU1.SetVoltage(powerSupplySetting.Voltage, powerSupplySetting.Channel); PSU1.SetCurrent(powerSupplySetting.CurrentLimit, powerSupplySetting.Channel); } } private void WriteBindingListToCsv(BindingList list, string filePath) { StringBuilder csvContent = new StringBuilder(); // Add header csvContent.AppendLine("Timestamp,Position,Device,Description,Value,Unit"); // Add data foreach (var measurement in list) { csvContent.AppendLine($"{measurement.Timestamp:yyyy-MM-dd},{measurement.Position},{measurement.Device},{measurement.Description},{measurement.Value},{measurement.Unit}"); } // Write to file File.WriteAllText(filePath, csvContent.ToString()); MessageBox.Show("Data exported successfully!", "Export", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void btnNextPosition_Click(object sender, EventArgs e) { CurrentPosition += PositionIncrement; } private void btnClear_Click(object sender, EventArgs e) { measurements.Clear(); // Clear the list } private void btnExport_Click(object sender, EventArgs e) { using (SaveFileDialog saveFileDialog = new SaveFileDialog()) { saveFileDialog.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*"; saveFileDialog.Title = "Save a CSV File"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { WriteBindingListToCsv(measurements, saveFileDialog.FileName); } } } } }