Skip to content

Commit

Permalink
Added real-time plot to view readings.
Browse files Browse the repository at this point in the history
  • Loading branch information
yasithHarischandra committed May 1, 2022
1 parent e655fe2 commit 10a4fdd
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 6 deletions.
44 changes: 39 additions & 5 deletions PZEM004T_PC/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 63 additions & 1 deletion PZEM004T_PC/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;


namespace PZEM004T_PC
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -48,14 +66,15 @@ 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);
}
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))
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -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;
}
}
}
3 changes: 3 additions & 0 deletions PZEM004T_PC/Form1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@
<metadata name="timerRefreshSensorReading.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
</root>
24 changes: 24 additions & 0 deletions PZEM004T_PC/PZEM004T_PC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,29 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="OxyPlot, Version=2.1.0.0, Culture=neutral, PublicKeyToken=638079a8f0bd61e9, processorArchitecture=MSIL">
<HintPath>..\packages\OxyPlot.Core.2.1.0\lib\net45\OxyPlot.dll</HintPath>
</Reference>
<Reference Include="OxyPlot.WindowsForms, Version=2.1.0.0, Culture=neutral, PublicKeyToken=245eacd6b5d2d338, processorArchitecture=MSIL">
<HintPath>..\packages\OxyPlot.WindowsForms.2.1.0\lib\net45\OxyPlot.WindowsForms.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing.Common, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Drawing.Common.4.6.1\lib\net461\System.Drawing.Common.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms.DataVisualization" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -44,6 +65,8 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
Expand Down Expand Up @@ -77,6 +100,7 @@
<EmbeddedResource Include="Settings.resx">
<DependentUpon>Settings.cs</DependentUpon>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
8 changes: 8 additions & 0 deletions PZEM004T_PC/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OxyPlot.Core" version="2.1.0" targetFramework="net472" />
<package id="OxyPlot.WindowsForms" version="2.1.0" targetFramework="net472" />
<package id="System.Drawing.Common" version="4.6.1" targetFramework="net472" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
</packages>

0 comments on commit 10a4fdd

Please sign in to comment.