Files
htolhast/HTOLHAST/HMP4040.cs
wesley 46ad1a708c Implement PowerUp Sequencing
bindings to datagridview
2025-09-19 00:42:19 +02:00

128 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.ComponentModel;
namespace HTOLHAST
{
public class HMP4040 : SCPIInstrument
{
public HMP4040(string resourceString) : base(resourceString) { }
public Sample MeasureCurrent(int channel)
{
string Channel = channel.ToString();
WriteCommand($"INST:NSEL {Channel}");
WriteCommand("MEAS:CURR?");
string response = ReadResponse();
Console.WriteLine($"[Hameg] Measured Current : {response}");
Sample sample = CreateSample(response, "A");
return sample;
}
public double ReadSetpoint(int channel)
{
double response = double.NaN;
WriteCommand($"INST OUT{channel}");
WriteCommand($"VOLT? CH{channel}");
double.TryParse(ReadResponse(), out response);
return response;
}
public Sample MeasureVolt(int channel)
{
string Channel = channel.ToString();
WriteCommand($"INST:NSEL {Channel}");
WriteCommand("MEAS:VOLT?");
string response = ReadResponse();
Console.WriteLine($"[Hameg] Measured Voltage : {response}");
Sample sample = CreateSample(response,"V");
return sample;
}
public string SetVoltage (double setpoint, int channel)
{
WriteCommand($"INST:NSEL {channel}");
WriteCommand($"VOLT {setpoint}");
WriteCommand($"VOLT?");
return ReadResponse();
}
public string SetCurrent(double setpoint, int channel)
{
WriteCommand($"INST:NSEL {channel}");
WriteCommand($"CURR {setpoint}");
WriteCommand($"CURR?");
return ReadResponse();
}
public void SetOutputChannel (int channel, bool enabled)
{
WriteCommand($"INST:NSEL {channel}");
if (enabled)
{
WriteCommand("OUTP ON");
}
else
{
WriteCommand("OUTP OFF");
}
}
public void SetGeneralOutputState(bool enabled)
{
if (enabled)
{
WriteCommand("OUTP:GEN ON");
}
else
{
WriteCommand("OUTP:GEN OFF");
}
}
public void PowerUpSequence(BindingList<PowerSequence> powerUpSequence)
{
foreach (var sequence in powerUpSequence)
{
SetOutputChannel(sequence.Channel,true);
Thread.Sleep(sequence.Delay);
}
}
public void PowerDownSequence(BindingList<PowerSequence> powerUpSequence)
{
foreach (var sequence in powerUpSequence)
{
SetOutputChannel(sequence.Channel, false);
Thread.Sleep(sequence.Delay);
}
}
public BindingList<PowerSequence> GenerateSimulationPowerUpSequence()
{
BindingList<PowerSequence> powerUpSequence = new BindingList<PowerSequence> {
new PowerSequence { Channel = 1, Delay = 1000},
new PowerSequence { Channel = 3, Delay = 1000},
new PowerSequence { Channel = 4, Delay = 1000},
new PowerSequence { Channel = 2, Delay = 1000},
};
return powerUpSequence;
}
public override Sample CreateSample(string response, string unit)
{
string Response = response;
if (double.TryParse(Response, out double voltage))
{
HMP4040Sample sample = new HMP4040Sample(DateTime.Now, voltage, unit);
// Optionally, store or process the sample here
Console.WriteLine($"[HMP4040] Sample Created, Value: {sample.Value} {sample.Unit}");
return sample;
}
throw new InvalidOperationException("Unsupported instrument type.");
}
}
}