zed graphs 实时绘图速度会随着时间的推移而降低。我是否堵塞了串口? C#
zed graphs real time plotting speed reduces over time. Am i clogging up the serial port? C#
我目前正在探索 C# 世界并创建了我的第一个 windows 应用程序。一切正常,但我相信它的效率让我失望。
我目前正在从 Arduino 读取串行数据。所有数据都以“Letter”(换行符)“Number”(换行符)“letter”等格式发送。从这里我将数据排序到 table 中的相关列中。我有 6 组数据来自 Arduino。然后使用 zed 图表将这些数据绘制到图表上,一次只显示 5 秒的数据。所以一个移动轴。
在将数据绘制到图表上大约 20 秒后,绘图速度变慢,最终我留下了一个没有点的移动图表,因为它们落后了。
我尝试刷新串行缓冲区,但这使一切变得更慢。
private void IncomingDataSort()
{
string IncomingSerial = serialPort1.ReadLine(); // Read incomming serial data
string StrIncomingSerial = IncomingSerial.ToString(); // convert this data to workable string
elapsed_time = (stopwatch.ElapsedMilliseconds); // How many milliseconds since stopwatch (read serial button) started
elapsed_time_sec = elapsed_time / 1000;
Timems.Text = elapsed_time.ToString();
if (StrIncomingSerial.Contains("Z") || StrIncomingSerial.Contains("Y")) // If this string contains a "Z" or "Y"
{
if (StrIncomingSerial.Contains("Z"))
{
string Number = serialPort1.ReadLine(); // Read Serialport
double Num; // Create variable "Num"
bool isNum = double.TryParse(Number, out Num); // Is the incomming serial data a number?
if (isNum) // If it is a number...
{
int NumberInt = Convert.ToInt16(Number); // convert string to int
Heat1Temp.Text = Number;
}
}
if (StrIncomingSerial.Contains("Y"))
{
string Number = serialPort1.ReadLine(); // Read Serialport
double Num; // Create variable "Num"
bool isNum = double.TryParse(Number, out Num); // Is the incomming serial data a number?
if (isNum) // If it is a number...
{
int NumberInt = Convert.ToInt16(Number); // convert string to int
Heat2Temp.Text = Number;
}
}
CreateGraph1(zedGraphControl1, elapsed_time, Convert.ToInt16(Heat1Temp.Text), Convert.ToInt16(Heat2Temp.Text)); // plot gragh
}
}
private void CreateGraph1(ZedGraphControl zgc, long time, int IncomingData, int IncomingData2)
{
GraphPane myPane = zgc.GraphPane; // setup graph
zgc.AutoScroll = true;
myPane.Title = "Graph 1"; // Titles
myPane.XAxis.Title = "Time (s)";
myPane.YAxis.Title = "";
myPane.Legend.IsVisible = false; // remove legend (DONT MAKE TRUE!!)
myPane.XAxis.Min = myPane.XAxis.Max - GraphTimeSpan;
PointPairList GraphInput = new PointPairList(); // create a new list
PointPairList GraphInput2 = new PointPairList(); // create a new list
long x;
int y1;
long x2;
int y2;
x = time; // x axis 1
y1 = IncomingData; // y axis 1
x2 = time; // x axis 2
y2 = IncomingData2; // y axis 2
GraphInput.Add(x, y1); // add to list
GraphInput2.Add(x2, y2); // add to list
LineItem myCurve = myPane.AddCurve("FirstSettings", GraphInput, Color.Red, SymbolType.Diamond); // draw points
LineItem myCurve2 = myPane.AddCurve("SecondSettings", GraphInput2, Color.Blue, SymbolType.Diamond);
zgc.AxisChange(); // update axis
zgc.Refresh();
}
作为我的评论的后续 - 无需每次获取数据时都添加一条新曲线,您只需将新点附加到现有的 PointPairLists - 这样您就只会拥有图形所需的两条曲线处理。将图表的初始化和更新分开是个好主意 - 所以像这样:
PointPairList GraphInput;
PointPairList GraphInput2;
LineItem myCurve;
LineItem myCurve2;
private void InitGraph(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane; // setup graph
zgc.AutoScroll = true;
myPane.Title = "Graph 1"; // Titles
myPane.XAxis.Title = "Time (s)";
myPane.YAxis.Title = "";
myPane.Legend.IsVisible = false; // remove legend (DONT MAKE TRUE!!)
GraphInput = new PointPairList(); // create a new list
GraphInput2 = new PointPairList(); // create a new list
myCurve = myPane.AddCurve("FirstSettings", GraphInput, Color.Red, SymbolType.Diamond); // draw points
myCurve2 = myPane.AddCurve("SecondSettings", GraphInput2, Color.Blue, SymbolType.Diamond);
}
private void UpdateGraph(ZedGraphControl zgc, long time, int IncomingData, int IncomingData2)
{
GraphPane myPane = zgc.GraphPane; // setup graph
myPane.XAxis.Max = time;
myPane.XAxis.Min = myPane.XAxis.Max - GraphTimeSpan;
GraphInput.Add(time, IncomingData); // add to list
GraphInput2.Add(time, IncomingData2); // add to list
// AT THIS POINT YOU COULD SEARCH THROUGH THE POINT LIST & REMOVE ANY POINTS PRIOR TO THE MIN TIME
while (GraphInput[0].X < myPane.XAxis.Min)
{
GraphInput.RemoveAt(0);
}
while (GraphInput2[0].X < myPane.XAxis.Min)
{
GraphInput2.RemoveAt(0);
}
zgc.AxisChange(); // update axis
zgc.Refresh();
}
另一种节省时间的方法可能是独立更新每条曲线 - 因为您正在向 GraphInput 和 GraphInput2 添加点,而不管您实际上是否有要为两者输入的点。使用单独的 UpdateGraph1 和 UpdateGraph2 方法可能会将要绘制的点减半。
代码中还有其他几项内容也可以整理 - 但它们不会导致严重的时间问题(您将数字从字符串转换,然后转换回字符串,然后再转换回数字 - 因为例如)。
非常感谢您的帮助,抱歉我花了这么长时间才回复,但我想在重新发布之前确保我有一段有效的代码。
这是我发现的有效解决方案,它可以帮助其他人。虽然请记住我是初学者所以几乎肯定需要整理!
private void InitGraph1 (ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane; // setup graph
zgc.AutoScroll = true;
myPane.Title = "Graph 1"; // Titles
myPane.XAxis.Title = "Time (s)";
myPane.YAxis.Title = "";
myPane.Legend.IsVisible = false; // remove legend
GraphInput1 = new PointPairList(); // create a new list
GraphInput2 = new PointPairList(); // create a new list
myCurve = myPane.AddCurve(DataOutputZ, GraphInput1, Color.Red, SymbolType.Diamond); // draw points
myCurve2 = myPane.AddCurve(DataOutputY, GraphInput2, Color.Blue, SymbolType.Diamond);
}
-
private void UpdateGraph(ZedGraphControl zgc, long time, int IncomingData, int IncomingData2)
{
{
GraphPane myPane = zgc.GraphPane;
myPane.XAxis.Min = time - GraphTimeSpan;
myPane.XAxis.Max = time + (GraphTimeSpan/4); // put new points in last quarter of graph
// Get the first CurveItem in the graph
LineItem myCurve = zgc.GraphPane.CurveList[0] as LineItem;
LineItem myCurve2 = zgc.GraphPane.CurveList[1] as LineItem;
PointPairList GraphInput1 = myCurve.Points as PointPairList;
PointPairList GraphInput2 = myCurve2.Points as PointPairList;
// add new data points to the graph
GraphInput1.Add(time, IncomingData);
GraphInput2.Add(time, IncomingData2);
while (GraphInput1[0].X < myPane.XAxis.Min)
{
GraphInput1.RemoveAt(0);
}
while (GraphInput2[0].X < myPane.XAxis.Min)
{
GraphInput2.RemoveAt(0);
}
// force redraw
zgc.Invalidate();
zgc.AxisChange(); // update axis
zgc.Refresh();
}
}
再次感谢!
我目前正在探索 C# 世界并创建了我的第一个 windows 应用程序。一切正常,但我相信它的效率让我失望。
我目前正在从 Arduino 读取串行数据。所有数据都以“Letter”(换行符)“Number”(换行符)“letter”等格式发送。从这里我将数据排序到 table 中的相关列中。我有 6 组数据来自 Arduino。然后使用 zed 图表将这些数据绘制到图表上,一次只显示 5 秒的数据。所以一个移动轴。
在将数据绘制到图表上大约 20 秒后,绘图速度变慢,最终我留下了一个没有点的移动图表,因为它们落后了。
我尝试刷新串行缓冲区,但这使一切变得更慢。
private void IncomingDataSort()
{
string IncomingSerial = serialPort1.ReadLine(); // Read incomming serial data
string StrIncomingSerial = IncomingSerial.ToString(); // convert this data to workable string
elapsed_time = (stopwatch.ElapsedMilliseconds); // How many milliseconds since stopwatch (read serial button) started
elapsed_time_sec = elapsed_time / 1000;
Timems.Text = elapsed_time.ToString();
if (StrIncomingSerial.Contains("Z") || StrIncomingSerial.Contains("Y")) // If this string contains a "Z" or "Y"
{
if (StrIncomingSerial.Contains("Z"))
{
string Number = serialPort1.ReadLine(); // Read Serialport
double Num; // Create variable "Num"
bool isNum = double.TryParse(Number, out Num); // Is the incomming serial data a number?
if (isNum) // If it is a number...
{
int NumberInt = Convert.ToInt16(Number); // convert string to int
Heat1Temp.Text = Number;
}
}
if (StrIncomingSerial.Contains("Y"))
{
string Number = serialPort1.ReadLine(); // Read Serialport
double Num; // Create variable "Num"
bool isNum = double.TryParse(Number, out Num); // Is the incomming serial data a number?
if (isNum) // If it is a number...
{
int NumberInt = Convert.ToInt16(Number); // convert string to int
Heat2Temp.Text = Number;
}
}
CreateGraph1(zedGraphControl1, elapsed_time, Convert.ToInt16(Heat1Temp.Text), Convert.ToInt16(Heat2Temp.Text)); // plot gragh
}
}
private void CreateGraph1(ZedGraphControl zgc, long time, int IncomingData, int IncomingData2)
{
GraphPane myPane = zgc.GraphPane; // setup graph
zgc.AutoScroll = true;
myPane.Title = "Graph 1"; // Titles
myPane.XAxis.Title = "Time (s)";
myPane.YAxis.Title = "";
myPane.Legend.IsVisible = false; // remove legend (DONT MAKE TRUE!!)
myPane.XAxis.Min = myPane.XAxis.Max - GraphTimeSpan;
PointPairList GraphInput = new PointPairList(); // create a new list
PointPairList GraphInput2 = new PointPairList(); // create a new list
long x;
int y1;
long x2;
int y2;
x = time; // x axis 1
y1 = IncomingData; // y axis 1
x2 = time; // x axis 2
y2 = IncomingData2; // y axis 2
GraphInput.Add(x, y1); // add to list
GraphInput2.Add(x2, y2); // add to list
LineItem myCurve = myPane.AddCurve("FirstSettings", GraphInput, Color.Red, SymbolType.Diamond); // draw points
LineItem myCurve2 = myPane.AddCurve("SecondSettings", GraphInput2, Color.Blue, SymbolType.Diamond);
zgc.AxisChange(); // update axis
zgc.Refresh();
}
作为我的评论的后续 - 无需每次获取数据时都添加一条新曲线,您只需将新点附加到现有的 PointPairLists - 这样您就只会拥有图形所需的两条曲线处理。将图表的初始化和更新分开是个好主意 - 所以像这样:
PointPairList GraphInput;
PointPairList GraphInput2;
LineItem myCurve;
LineItem myCurve2;
private void InitGraph(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane; // setup graph
zgc.AutoScroll = true;
myPane.Title = "Graph 1"; // Titles
myPane.XAxis.Title = "Time (s)";
myPane.YAxis.Title = "";
myPane.Legend.IsVisible = false; // remove legend (DONT MAKE TRUE!!)
GraphInput = new PointPairList(); // create a new list
GraphInput2 = new PointPairList(); // create a new list
myCurve = myPane.AddCurve("FirstSettings", GraphInput, Color.Red, SymbolType.Diamond); // draw points
myCurve2 = myPane.AddCurve("SecondSettings", GraphInput2, Color.Blue, SymbolType.Diamond);
}
private void UpdateGraph(ZedGraphControl zgc, long time, int IncomingData, int IncomingData2)
{
GraphPane myPane = zgc.GraphPane; // setup graph
myPane.XAxis.Max = time;
myPane.XAxis.Min = myPane.XAxis.Max - GraphTimeSpan;
GraphInput.Add(time, IncomingData); // add to list
GraphInput2.Add(time, IncomingData2); // add to list
// AT THIS POINT YOU COULD SEARCH THROUGH THE POINT LIST & REMOVE ANY POINTS PRIOR TO THE MIN TIME
while (GraphInput[0].X < myPane.XAxis.Min)
{
GraphInput.RemoveAt(0);
}
while (GraphInput2[0].X < myPane.XAxis.Min)
{
GraphInput2.RemoveAt(0);
}
zgc.AxisChange(); // update axis
zgc.Refresh();
}
另一种节省时间的方法可能是独立更新每条曲线 - 因为您正在向 GraphInput 和 GraphInput2 添加点,而不管您实际上是否有要为两者输入的点。使用单独的 UpdateGraph1 和 UpdateGraph2 方法可能会将要绘制的点减半。
代码中还有其他几项内容也可以整理 - 但它们不会导致严重的时间问题(您将数字从字符串转换,然后转换回字符串,然后再转换回数字 - 因为例如)。
非常感谢您的帮助,抱歉我花了这么长时间才回复,但我想在重新发布之前确保我有一段有效的代码。
这是我发现的有效解决方案,它可以帮助其他人。虽然请记住我是初学者所以几乎肯定需要整理!
private void InitGraph1 (ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane; // setup graph
zgc.AutoScroll = true;
myPane.Title = "Graph 1"; // Titles
myPane.XAxis.Title = "Time (s)";
myPane.YAxis.Title = "";
myPane.Legend.IsVisible = false; // remove legend
GraphInput1 = new PointPairList(); // create a new list
GraphInput2 = new PointPairList(); // create a new list
myCurve = myPane.AddCurve(DataOutputZ, GraphInput1, Color.Red, SymbolType.Diamond); // draw points
myCurve2 = myPane.AddCurve(DataOutputY, GraphInput2, Color.Blue, SymbolType.Diamond);
}
-
private void UpdateGraph(ZedGraphControl zgc, long time, int IncomingData, int IncomingData2)
{
{
GraphPane myPane = zgc.GraphPane;
myPane.XAxis.Min = time - GraphTimeSpan;
myPane.XAxis.Max = time + (GraphTimeSpan/4); // put new points in last quarter of graph
// Get the first CurveItem in the graph
LineItem myCurve = zgc.GraphPane.CurveList[0] as LineItem;
LineItem myCurve2 = zgc.GraphPane.CurveList[1] as LineItem;
PointPairList GraphInput1 = myCurve.Points as PointPairList;
PointPairList GraphInput2 = myCurve2.Points as PointPairList;
// add new data points to the graph
GraphInput1.Add(time, IncomingData);
GraphInput2.Add(time, IncomingData2);
while (GraphInput1[0].X < myPane.XAxis.Min)
{
GraphInput1.RemoveAt(0);
}
while (GraphInput2[0].X < myPane.XAxis.Min)
{
GraphInput2.RemoveAt(0);
}
// force redraw
zgc.Invalidate();
zgc.AxisChange(); // update axis
zgc.Refresh();
}
}
再次感谢!