private void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { ReloadReceiveData rd = new ReloadReceiveData(ComPort.ReadLine()); ReadMessageQueue.Enqueue(rd); LastMAmpere = rd.CurrentMA; LastMVoltage = rd.VoltageMV; LastResistance = rd.Resistance; CumMWh += GetHourlyValues(rd.PowerMW); CumMAh += GetHourlyValues(rd.CurrentMA); if (doLog == true) { log.toFile(rd.TimeStamp, rd.CurrentMA, rd.VoltageMV, rd.Resistance, rd.PowerMW, this.CumulatedMWh, this.CumulatedMAh); } if (rd.MessageType == MsgType.Read) { double newValue = Math.Round(CalcSetValue(), 0); //check next Current Value to Set if (newValue != OldInputValue) // Check if user Changed Input Value { InputValue = newValue; OldInputValue = InputValue; ComPort.WriteLine("set " + Math.Round(newValue, 0)); } } }
private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { while (!bgWorker.CancellationPending) { while (Logic.ReadMessageQueue.Count > 0) { ReloadReceiveData RMessage = Logic.ReadMessageQueue.Dequeue(); if (Logic.InputValue != Convert.ToInt32(tbSetValue.Text)) { Logic.InputValue = Convert.ToInt32(tbSetValue.Text); } bgWorker.ReportProgress(0, RMessage); //FillForm(RMessage); } System.Threading.Thread.Sleep(100); } }
private void FillForm(ReloadReceiveData rd) { if (rd.MessageType == MsgType.Read) { tbCRead.Text = rd.CurrentMA.ToString(); tbVRead.Text = rd.VoltageMV.ToString(); tbResCalc.Text = rd.Resistance.ToString(); if (rd.PowerMW >= 1000) { if (lblUnitPCalc.Text != "W") { lblUnitPCalc.Text = "W"; } tbPowerCalc.Text = rd.PowerW.ToString(); } else { if (lblUnitPCalc.Text != "mW") { lblUnitPCalc.Text = "mW"; } tbPowerCalc.Text = rd.PowerMW.ToString(); } if (Logic.CumulatedMWh >= 1000) { if (lblUnitWhCalc.Text != "Wh") { lblUnitWhCalc.Text = "Wh"; } tbWhCalc.Text = Logic.CumulatedWh.ToString(); } else { if (lblUnitWhCalc.Text != "mWh") { lblUnitWhCalc.Text = "mWh"; } tbWhCalc.Text = Logic.CumulatedMAh.ToString(); } if (Logic.CumulatedMAh >= 1000) { if (lblUnitAhCalc.Text != "Ah") { lblUnitAhCalc.Text = "Ah"; } tbAhCalc.Text = Logic.CumulatedAh.ToString(); } else { if (lblUnitWhCalc.Text != "mAh") { lblUnitAhCalc.Text = "mAh"; } tbAhCalc.Text = Logic.CumulatedMAh.ToString(); } tbAhCalc.Text = Logic.CumulatedMAh.ToString(); } //Log Windows if (rd.MessageType == MsgType.Read) { tbMessage.AppendText("Read: " + rd.CurrentMA + ", " + rd.VoltageMV + Environment.NewLine); } else if (rd.MessageType == MsgType.Set) { tbMessage.AppendText("Set: " + rd.CurrentMA + Environment.NewLine); } else if (rd.MessageType == MsgType.Error) { tbError.AppendText("Error: " + rd.Error + Environment.NewLine); } else { tbError.AppendText("Unknown: " + rd.Error); } }