[英]C# - Zedgraph, plotting serial data too much delay and time lag
我是 C# 编程的新手,正在尝试编写一个应用程序,这是我最终论文的一部分。
我有一个微处理器,它通过串行端口不断地将数据从传感器发送到我的计算机。 我想要的只是使用 Zedgraph 绘制这些数据。
问题是图形有太多的延迟和时间滞后。 似乎问题发生是因为我以非常高的速度不断更新整个图表。 我已经在一周内解决了这个问题,但仍然没有找到解决方案。 如果有人可以帮助我,我会非常高兴。
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using System.IO.Ports;
using System.Threading;
namespace DynamicData
{
public partial class Form1 : Form
{
private SerialPort port;
private string buffer = "";
private void connect()
{
port = new SerialPort("COM8", 115200, Parity.None, 8, StopBits.One);
port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
if (!port.IsOpen) port.Open();
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e )
{
connect();
GraphPane myPane = zedGraphControl1.GraphPane;
RollingPointPairList list = new RollingPointPairList(500);
LineItem curve = myPane.AddCurve( "Sensor", list, Color.Blue, SymbolType.None );
myPane.XAxis.Scale.Min = 0;
myPane.XAxis.Scale.Max = 10;
myPane.YAxis.Scale.Min = 0;
myPane.YAxis.Scale.Max = 300;
myPane.XAxis.Scale.MinorStep = 0.5;
myPane.XAxis.Scale.MajorStep = 1;
zedGraphControl1.AxisChange();
}
private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
//sample data: ;100*100000:
//sampling rate ~100Hz
buffer += port.ReadExisting();
//flush incomplete package
while (buffer[0] != ';')
{
buffer = buffer.Remove(0, 1);
if (buffer.Length < 1) break;
}
//got a complete package, go to data handling
while (buffer.Contains(":"))
{
DataHandling();
}
}
private void DataHandling()
{
string[] nameArray = buffer.Split(new[] { ";", ":", "*" }, StringSplitOptions.RemoveEmptyEntries);
//plot sensor data vs. time
draw(Convert.ToInt32(nameArray[0]), Convert.ToInt32(nameArray[1]));
//remove handled package in buffer
var index = buffer.IndexOf(":");
buffer = buffer.Remove(0, index + 1);
}
double time = 0;
private void draw(int sensor, int t)
{
//convert tick to sec (uP clock rate = 16MHZ)
time = time + (t / 16000000.0);
// Get the first CurveItem in the graph
LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
// Get the PointPairList
IPointListEdit list = curve.Points as IPointListEdit;
list.Add(time, sensor);
//Keep the X scale at a rolling 10 second interval, with one
//major step between the max X value and the end of the axis
Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
if (time > xScale.Max - xScale.MajorStep)
{
xScale.Max = time + xScale.MajorStep;
xScale.Min = xScale.Max - 10.0;
}
//Display sensor data
this.Invoke(new Action(() => { textBox1.Text = byte1.ToString(); }));
axisChangeZedGraph(zedGraphControl1);
}
delegate void axisChangeZedGraphCallBack(ZedGraphControl zg);
private void axisChangeZedGraph(ZedGraphControl zg)
{
if (zg.InvokeRequired)
{
axisChangeZedGraphCallBack ad = new axisChangeZedGraphCallBack(axisChangeZedGraph);
zg.Invoke(ad, new object[] { zg });
}
else
{
// zg.AxisChange();
zg.Invalidate();
zg.Refresh();
}
}
}
}
感谢您的阅读!
问题是你对你绘制的每个点都调用了invalidate
。 这会产生非常高的处理器负载。 我正在做一个非常相似的项目,USB 设备,实时绘图数据。 我使用单独的线程进行数据采集。 该线程每次接收数据包时都会创建一个事件。 数据被放入队列中。 该图用计时器更新。 您可以在此处找到示例。 在这段代码中,图形每 50 毫秒更新一次,实际上没有必要画得更快。 在计时器中,我检查队列的大小并绘制更多或更少的点,然后调用invalidate
。 我不知道这是否是一个很好的解决方案(只有 6 个月的 C# 经验),但它在 7500 分上运行良好。 您应该首先尝试使用计时器来刷新图形。
1)。 仅创建和格式化曲线一次。 将其存储在模块级别。
2)。 添加点时使用curve.AddPoint();
然后zg.Refresh();
//this is just to add a point to the plot, the curve object should have already been created
private void draw(int sensor, int t)
{
//convert tick to sec (uP clock rate = 16MHZ)
time = time + (t / 16000000.0);
//curve should be a module-level variable already set up with proper formatting,
//just no points yet
curve.AddPoint(time, sensor);
//Display sensor data
this.Invoke(new Action(() => { textBox1.Text = byte1.ToString(); }));
zg.AxisChange();
zg.Refresh();
}
您还可以使用 backgroundworker,因为它对于实时处理连续数据绘图非常有效。 另外,我不知道 zedgraph 是否可以处理高速率的数据绘图。
如果改变框架不是一个选项,那么只需限制绘制图形的速度。 最有可能以该波特率绘制的数据为每秒 2000,这是 zedgraph 无法处理的。
以下是后台工作人员的参考: https : //docs.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=net-5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.