diff --git a/PZEM004T_PC/Form1.Designer.cs b/PZEM004T_PC/Form1.Designer.cs index 5220985..92d14a7 100644 --- a/PZEM004T_PC/Form1.Designer.cs +++ b/PZEM004T_PC/Form1.Designer.cs @@ -47,11 +47,13 @@ private void InitializeComponent() this.buttonSettings = new System.Windows.Forms.Button(); this.labelOverloadAlarm = new System.Windows.Forms.Label(); this.OverloadAlarmDisplay = new System.Windows.Forms.TextBox(); + this.plotView1 = new OxyPlot.WindowsForms.PlotView(); + this.comboBoxSelectPlotVariable = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // buttonRefreshPorts // - this.buttonRefreshPorts.Location = new System.Drawing.Point(281, 12); + this.buttonRefreshPorts.Location = new System.Drawing.Point(246, 15); this.buttonRefreshPorts.Name = "buttonRefreshPorts"; this.buttonRefreshPorts.Size = new System.Drawing.Size(75, 23); this.buttonRefreshPorts.TabIndex = 8; @@ -62,7 +64,7 @@ private void InitializeComponent() // this.comboBoxComPorts.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxComPorts.FormattingEnabled = true; - this.comboBoxComPorts.Location = new System.Drawing.Point(135, 12); + this.comboBoxComPorts.Location = new System.Drawing.Point(109, 16); this.comboBoxComPorts.Name = "comboBoxComPorts"; this.comboBoxComPorts.Size = new System.Drawing.Size(121, 21); this.comboBoxComPorts.TabIndex = 7; @@ -78,7 +80,7 @@ private void InitializeComponent() // // buttonConnect // - this.buttonConnect.Location = new System.Drawing.Point(373, 12); + this.buttonConnect.Location = new System.Drawing.Point(337, 15); this.buttonConnect.Name = "buttonConnect"; this.buttonConnect.Size = new System.Drawing.Size(75, 23); this.buttonConnect.TabIndex = 11; @@ -208,7 +210,7 @@ private void InitializeComponent() // // buttonSettings // - this.buttonSettings.Location = new System.Drawing.Point(15, 220); + this.buttonSettings.Location = new System.Drawing.Point(428, 15); this.buttonSettings.Name = "buttonSettings"; this.buttonSettings.Size = new System.Drawing.Size(75, 23); this.buttonSettings.TabIndex = 25; @@ -236,11 +238,41 @@ private void InitializeComponent() this.OverloadAlarmDisplay.TabIndex = 27; this.OverloadAlarmDisplay.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; // + // plotView1 + // + this.plotView1.Location = new System.Drawing.Point(28, 222); + this.plotView1.Name = "plotView1"; + this.plotView1.PanCursor = System.Windows.Forms.Cursors.Hand; + this.plotView1.Size = new System.Drawing.Size(596, 254); + this.plotView1.TabIndex = 28; + this.plotView1.Text = "plotView1"; + this.plotView1.ZoomHorizontalCursor = System.Windows.Forms.Cursors.SizeWE; + this.plotView1.ZoomRectangleCursor = System.Windows.Forms.Cursors.SizeNWSE; + this.plotView1.ZoomVerticalCursor = System.Windows.Forms.Cursors.SizeNS; + // + // comboBoxSelectPlotVariable + // + this.comboBoxSelectPlotVariable.FormattingEnabled = true; + this.comboBoxSelectPlotVariable.Items.AddRange(new object[] { + "Voltage", + "Current", + "Power", + "Frequency", + "Power Factor"}); + this.comboBoxSelectPlotVariable.Location = new System.Drawing.Point(18, 222); + this.comboBoxSelectPlotVariable.Name = "comboBoxSelectPlotVariable"; + this.comboBoxSelectPlotVariable.Size = new System.Drawing.Size(121, 21); + this.comboBoxSelectPlotVariable.TabIndex = 29; + this.comboBoxSelectPlotVariable.Text = "Voltage"; + this.comboBoxSelectPlotVariable.SelectedIndexChanged += new System.EventHandler(this.comboBoxSelectPlotVariable_SelectedIndexChanged); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(609, 450); + this.ClientSize = new System.Drawing.Size(662, 558); + this.Controls.Add(this.comboBoxSelectPlotVariable); + this.Controls.Add(this.plotView1); this.Controls.Add(this.OverloadAlarmDisplay); this.Controls.Add(this.labelOverloadAlarm); this.Controls.Add(this.buttonSettings); @@ -284,6 +316,8 @@ private void InitializeComponent() private System.Windows.Forms.Button buttonSettings; private System.Windows.Forms.Label labelOverloadAlarm; private System.Windows.Forms.TextBox OverloadAlarmDisplay; + private OxyPlot.WindowsForms.PlotView plotView1; + private System.Windows.Forms.ComboBox comboBoxSelectPlotVariable; } } diff --git a/PZEM004T_PC/Form1.cs b/PZEM004T_PC/Form1.cs index 2d069b8..3ca8271 100644 --- a/PZEM004T_PC/Form1.cs +++ b/PZEM004T_PC/Form1.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; +using System.Diagnostics; namespace PZEM004T_PC @@ -16,6 +17,7 @@ public partial class Form1 : Form { public PZEM_004T_v3 energyMeter; public System.Windows.Forms.Timer timerRefreshSensorReading; + private readonly Stopwatch watch = new Stopwatch(); public Form1() { InitializeComponent(); @@ -29,6 +31,22 @@ private void Form1_Load(object sender, EventArgs e) comboBoxComPorts.Items.AddRange(ports); comboBoxComPorts.SelectedIndex = 0; + var line = new OxyPlot.Series.LineSeries() + { + Color = OxyPlot.OxyColors.Blue, + StrokeThickness = 1, + MarkerSize = 2, + MarkerType = OxyPlot.MarkerType.Circle + }; + + var model = new OxyPlot.PlotModel + { + Title = $"{comboBoxSelectPlotVariable.SelectedItem:N0}" + }; + model.Series.Add(line); + + // load the model into the user control + plotView1.Model = model; } private void button1_Click(object sender, EventArgs e) @@ -48,7 +66,7 @@ private void button1_Click(object sender, EventArgs e) MessageBox.Show("Error reading power overload alarm threshold from sensor!\n" + error); timerRefreshSensorReading.Start(); - Cursor.Current = Cursors.Default; + watch.Start(); } else MessageBox.Show(error); @@ -56,6 +74,7 @@ private void button1_Click(object sender, EventArgs e) else { timerRefreshSensorReading.Stop(); + watch.Stop(); Cursor.Current = Cursors.WaitCursor; Thread.Sleep(timerRefreshSensorReading.Interval + 1000); //wait until auto sensor refresh is done if (Program.CloseSerialPort(ref error)) @@ -93,6 +112,7 @@ private void timerRefreshSensorReading_Tick(object sender, EventArgs e) backgroundThread.Start(); if (energyMeter == null) return; + backgroundThread.Join(); VoltageDisplay.Text = energyMeter.readings.GetVoltage().ToString(); FreqDisplay.Text = energyMeter.readings.GetFrequency().ToString(); @@ -103,6 +123,14 @@ private void timerRefreshSensorReading_Tick(object sender, EventArgs e) OverloadAlarmDisplay.BackColor = Color.Red; else OverloadAlarmDisplay.BackColor = Color.Green; + + + var s = (OxyPlot.Series.LineSeries)plotView1.Model.Series[0]; + if (s.Points.Count >= 200) + s.Points.RemoveAt(0); + + s.Points.Add(GetCorrectDataPoint()); + this.plotView1.Model.InvalidatePlot(true); } private void buttonSettings_Click(object sender, EventArgs e) @@ -111,5 +139,39 @@ private void buttonSettings_Click(object sender, EventArgs e) timerRefreshSensorReading.Stop(); newSettingsWindow.Show(); } + + private void comboBoxSelectPlotVariable_SelectedIndexChanged(object sender, EventArgs e) + { + plotView1.Model.Title = comboBoxSelectPlotVariable.Text; + + var s = (OxyPlot.Series.LineSeries)plotView1.Model.Series[0]; + s.Points.Clear(); + + if(energyMeter != null) + s.Points.Add(GetCorrectDataPoint()); + + this.plotView1.Model.InvalidatePlot(true); + } + + private OxyPlot.DataPoint GetCorrectDataPoint() + { + double t = this.watch.ElapsedMilliseconds * 0.001; + OxyPlot.DataPoint point; + + if (comboBoxSelectPlotVariable.Text == "Voltage") + point = new OxyPlot.DataPoint(t, (double)energyMeter.readings.GetVoltage()); + else if (comboBoxSelectPlotVariable.Text == "Current") + point = new OxyPlot.DataPoint(t, (double)energyMeter.readings.GetCurrent()); + else if (comboBoxSelectPlotVariable.Text == "Power") + point = new OxyPlot.DataPoint(t, (double)energyMeter.readings.GetPower()); + else if (comboBoxSelectPlotVariable.Text == "Frequency") + point = new OxyPlot.DataPoint(t, (double)energyMeter.readings.GetFrequency()); + else if (comboBoxSelectPlotVariable.Text == "Power Factor") + point = new OxyPlot.DataPoint(t, (double)energyMeter.readings.GetPowerFactor()); + else + point = new OxyPlot.DataPoint(t, 0.0); + + return point; + } } } diff --git a/PZEM004T_PC/Form1.resx b/PZEM004T_PC/Form1.resx index da7d51e..a243191 100644 --- a/PZEM004T_PC/Form1.resx +++ b/PZEM004T_PC/Form1.resx @@ -120,4 +120,7 @@ 17, 17 + + 25 + \ No newline at end of file diff --git a/PZEM004T_PC/PZEM004T_PC.csproj b/PZEM004T_PC/PZEM004T_PC.csproj index 4a7fe07..2e2191d 100644 --- a/PZEM004T_PC/PZEM004T_PC.csproj +++ b/PZEM004T_PC/PZEM004T_PC.csproj @@ -33,8 +33,29 @@ 4 + + ..\packages\OxyPlot.Core.2.1.0\lib\net45\OxyPlot.dll + + + ..\packages\OxyPlot.WindowsForms.2.1.0\lib\net45\OxyPlot.WindowsForms.dll + + + + + ..\packages\System.Drawing.Common.4.6.1\lib\net461\System.Drawing.Common.dll + + + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + True + True + + + ..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll + + + @@ -44,6 +65,8 @@ + + @@ -77,6 +100,7 @@ Settings.cs + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/PZEM004T_PC/packages.config b/PZEM004T_PC/packages.config new file mode 100644 index 0000000..a58a0e9 --- /dev/null +++ b/PZEM004T_PC/packages.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file