2013-03-11 1 views
2

직렬 포트에서 오는 데이터 그래프를 표시하고 싶습니다.이 데이터는 그 사이에 계속해서 나타납니다. 그래프 그리기 위해 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(); 
     } 
    } 
} 

그러나 연속 그래프를 표시하지 않습니다. 내 코드에서 어디에 문제가 있는지 모르겠습니다. 이 문제를 해결하는 데 도움을주십시오.

도움이 될 것입니다.

답변

0

거기

그것은 당신이 당신의 방법 당신이 곡선을 그릴 때마다 목록을 삭제하는 것을 할 수있다. 매번 ZedGraph는 단일 점을 그려서 나머지 곡선을 지우고 있습니다. 아마도?