我目前正在冒险进入c#世界并创建了我的第一个Windows应用程序。一切都在运作,但我相信它的效率让我失望。
我目前正在阅读Arduino的串行数据。所有数据都以“Letter”(换行符)“Number”(换行符)“letter”等格式发送。从这里我将数据分类到表中的相关列中。我有来自Arduino的6组数据。然后使用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();
}
答案 0 :(得分:0)
作为我的评论的后续内容 - 而不是每次获取数据时添加新曲线,您只需要将新点附加到现有的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&amp;添加点数。 GraphInput2,无论你是否确实有两个点都输入。有单独的UpdateGraph1&amp; UpdateGraph2方法可以将要绘制的点减半。
在代码中还有其他一些东西可以整理 - 但它们不会导致严重的时间问题(您从字符串转换数字,然后转换回字符串,然后转换回数字 - 例如)
答案 1 :(得分:0)
非常感谢你的帮助,对不起,我花了很长时间才回复,但是想在重新发布之前确保我有一段工作代码。
这是我发现工作的解决方案,它可以帮助其他任何人。虽然请记住我是初学者,所以几乎肯定需要整理!
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();
}
}
再次感谢!