So i have created an event, whenever the property ActualVoltage changed, it will update in Form1 but it doesnt. Property ActualVoltage change, when i send a set-voltage command to the machine, then it will send back a number and i assign that number to AcutalVoltage. pls help me, pls show me where is my mistake and explain it for me like i am a 5 years old kid.Here is my event code:
public delegate void ValueChange();
public event ValueChange Change;
public double ActualVoltage
{
get { return actualVoltage; }
set
{
if (actualVoltage == value) return;
else
{
actualVoltage = value;
OnValueChange();
}
}
}
private void OnValueChange()
{
Change?.Invoke();
}
in Form1:
private void Form1_Load(object sender, EventArgs e)
{
ps.Change += ps_change;
}
private void ps_change()
{
lblValueActualVoltage.Text = ps.ActualVoltage.ToString();
lblValueActualCurrent.Text = ps.ActualCurrent.ToString();
lblHexCur.Text = ps.HexActualCurrent1;
lblHexVol.Text = ps.HexActualVoltage1;
}
updated: in class PS2000B
public void GetDeviceStatusInformation(byte[] rawData)
{
remoteMode = ((byte)(rawData[0] & 0b1)).ToString();
outputMode = ((byte)(rawData[1] & 0b1)).ToString();
List<byte> temp = new List<byte>();
foreach (var v in rawData)
temp.Add(v);
byte[] vontageBytes = temp.GetRange(2, 2).ToArray();
HexActualVoltage = BitConverter.ToString(vontageBytes);
Array.Reverse(vontageBytes);
byte[] currentBytes = temp.GetRange(4, 2).ToArray();
HexActualCurrent = BitConverter.ToString(currentBytes);
Array.Reverse(currentBytes);
var a = (BitConverter.ToInt16(vontageBytes, 0));
ActualVoltage =Math.Round( BitConverter.ToInt16(vontageBytes, 0) * nominalVoltage / 25600.0,2);
ActualCurrent = BitConverter.ToInt16(currentBytes, 0) * nominalCurrent / 25600.0;
}
public void RunTest(string safeFileName,string save)
{
Stopwatch st = new Stopwatch();
List<string> timeMeasure = new List<string>();
List<string> CurrentResults = new List<string>();
List<int> Time = new List<int>();
List<string> Voltage = new List<string>();
FileStream file = new FileStream(safeFileName, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(file);
string strRead = reader.ReadLine();
while (strRead != null)
{
string[] temp = strRead.Split(';');
Voltage.Add(temp[0]);
Time.Add(int.Parse(temp[1]));
strRead = reader.ReadLine();
}
reader.Close();
file.Close();
int n = 0;
st.Start();
for (int i = 0; i < Voltage.Count(); i++)
{
SetVoltage(Voltage[i]);
for (int j = 0; j < Time[i]/300; j++)
{
UpdateStatusInfomationAndActualValue();
CurrentResults.Add(Voltage[i]+";"+0.3*n+";"+ActualCurrent.ToString()+";"+ HexActualCurrent);
n++;
}
}
st.Stop();
FileStream wfile = new FileStream(save +"\\results.txt", FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(wfile);
writer.WriteLine("VOlTAGE;TIME;CURRENT");
foreach (var v in CurrentResults)
writer.WriteLine(v);
writer.WriteLine("TOTAL TIME: "+st.Elapsed);
writer.Close();
wfile.Close();
}
public void SetVoltage(string vol)
{
vol = vol.Replace('.', ',');
ToPowerSupply ToPowerSupply = new ToPowerSupply();
var b = Convert.ToInt16(Single.Parse(vol) * 25600 / nominalVoltage);
var input = BitConverter.GetBytes(b);
Array.Reverse(input);
var temp = ToPowerSupply.SendCommand(0b11, ObjectList.SET_U, input, 2);
ComPort.Write(temp, 0, temp.Count());
Thread.Sleep(150);
int bytes = ComPort.BytesToRead;
byte[] rawData = new byte[bytes];
ComPort.Read(rawData, 0, bytes);
}