我想显示来自串行端口的数据图。这些数据之间继续有 \r。对于绘图,我使用的是 ZedGraph.dll。我的代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using System.IO.Ports;
namespace FilterRealTimeCSharp
{
public partial class Form1 : Form
{
List<double> measures;
/// The ZedGraph curve
LineItem myCurve;
BackgroundWorker worker;
// ZedGraphControl zedGraphControl1;
GraphPane myPane;
public Form1()
{
InitializeComponent();
//create an empty list
measures = new List<double>();
myPane = zedGraphControl2.GraphPane;
//init your zegGraphControl here
//create an empty curve: it will be filled later
myCurve = myPane.AddCurve("Porsche", null, Color.Red, SymbolType.Diamond);
//create the worker
worker = new BackgroundWorker();
// set this to true so that you can cancel the worker
worker.WorkerSupportsCancellation = true;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//the worker has completed
//do whatever you want here
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
//put all your serial port code here
SerialPort sprt = new SerialPort("COM1");
sprt.BaudRate = 9600;
sprt.Parity = Parity.None;
sprt.StopBits = StopBits.One;
sprt.DataBits = 8;
sprt.Handshake = Handshake.None;
try
{
sprt.Open();
}
catch (Exception)
{
MessageBox.Show("Check port");
return;
}
//worker.CancellationPending will change to true when CancelAsync is called
//(so when the user clicks button2).
//while the worker should still continue, read incoming data
while (!worker.CancellationPending)
{
//wait for data to come...
System.Threading.Thread.Sleep(100);
string indata = sprt.ReadExisting();
//extract the values from the read data
//be careful here: make sure the read data is complete...
string[] splt = indata.Split('\r');
// string chop = splt[2];
//// string final = chop.Remove(5);
// float d = Convert.ToSingle(chop);
//update the measures
//measures is shared by several threads: you must lock it to access it safely
lock (measures)
{
for (int i = 1; i < splt.Length-2; i++)
{
string chop = splt[i];
// string final = chop.Remove(5);
float d = Convert.ToSingle(chop);
measures.Add(d);
}
}
//update the graph
BeginInvoke((Action)(() => UpdateGraph()));
}
//user wants to stop the worker
sprt.Close();
}
/// This function is called when the graph should be updated
private void UpdateGraph()
{
//messures is shared by several threads: you must lock it to access it safely
lock (measures)
{
//add each measure into the curve
for (int i = 0; i < measures.Count; i++)
{
//fill each with what ever you want
double x = myCurve.Points.Count;
double y = measures[i];
//add a new point to the curve
myCurve.AddPoint(x, y);
}
//all measures have been added
//we can empty the list
measures.Clear();
}
//the curve has been updated so refresh the graph
zedGraphControl2.AxisChange();
zedGraphControl2.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
worker.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
worker.CancelAsync();
}
}
}
但它没有显示连续图。我不知道我的代码中的问题在哪里。请帮我解决这个问题。
任何帮助将不胜感激。