2017-03-08 60 views
0

我想绘制使用zedgraph从串行端口读取的数据。我仍然在学习代码,所以我不能推断为什么情节不起作用。请查看代码和建议;使用zedgraph绘制串行端口数据(数据VS时间)

namespace WindowsApplication2 
{ 
    public partial class Form1 : Form 
    { 
     string t; 
     SerialPort sp; 

Thread m_thread; 
bool m_running = false; 
ManualResetEvent m_event = new ManualResetEvent(true); 
bool m_pause = false; 
private GraphPane myPane; 

public Form1() 
{ 
    InitializeComponent(); 
    Control.CheckForIllegalCrossThreadCalls = false; 
    // User can already search for ports when the constructor of the FORM1 is calling 
    // And let the user search ports again with a click 
    // Searching for ports function 

    SearchPorts(); 

    CreateZedGraph(); //error : Severity Code Description Project File Line Suppression State 
         //Error CS7036 There is no argument given that corresponds to the required formal parameter 
         //'w' of 'Form1.DrawPoint(ZedGraphControl, int, PointPair)' 
} 
// start button 
private void btnStart_Click(object sender, EventArgs e) 
{ 
    if (m_thread == null || m_thread.IsAlive == false) 
    { 
     ClearGraph(); 
     m_thread = new Thread(Process); 
     m_thread.Start(); 
    } 
} 
void Process() 
{  
    PointPair point = new PointPair(); 
    btnStart.Enabled = false; 
    btnStop.Enabled = true; 
    m_running = true; 
    while (m_running == true) 
    { 
     m_event.WaitOne(); 

     point.Y = Convert.ToDouble(serialPort1); 
     point.X++; //time instance of measurement?? 
     DrawPoint(zed1, point); 
     ssData.Value = point.Y.ToString(); 
     RefresheZedGraphs(zed1); 
     Thread.Sleep(700); 
    } 
    btnStart.Enabled = true; 
} 

private void CreateZedGraph(object sender, SerialDataReceivedEventArgs e, ZedGraphControl zgc) 
{ 
    myPane = zgc.GraphPane; 
    // axes stuff 
    myPane.Title.Text = "FRDM-KW40z serial Test"; 
    myPane.XAxis.Title.Text = "Time"; 
    myPane.YAxis.Title.Text = "Voltage"; 
    myPane.XAxis.MajorGrid.IsVisible = true; 
    myPane.YAxis.MajorGrid.IsVisible = true; 
    myPane.XAxis.MinorGrid.IsVisible = true; 
    myPane.YAxis.MinorGrid.IsVisible = true; 

    // data from serial port 

    PointPairList list = new PointPairList(); 
    zed1.GraphPane.AddCurve("Test", list, Color.Red); 

} 

要打开并从串口读取,我用一个COMBOX和几个按钮(再后来我尝试将其保存到一个文本文件);

private void button2_Click(object sender, EventArgs e) 
    { 
     comboBox1.Items.Clear(); 
     SearchPorts(); 
    } 
    void SearchPorts() 
    { 
     string[] ports = SerialPort.GetPortNames(); 
     foreach (string port in ports) 
     { 
      comboBox1.Items.Add(port); 
     } 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     // Catch exception if it will be thrown so the user will see it in a message box 
     OpenCloseSerial(); 
    }  
    void OpenCloseSerial() 
    { 
     try 
     { 
      if (sp == null || sp.IsOpen == false) 
      { 
       t = comboBox1.Text.ToString(); 
       sErial(t); 
       button3.Text = "Close Serial port"; // button text 
      } 
      else 
      { 
       sp.Close(); 
       button3.Text = "Connect and wait for inputs"; // button text 

      } 
     } 
     catch (Exception err) // catching error message 
     { 
      MessageBox.Show(err.Message); // displaying error message 
     }   
    } 

    void sErial(string Port_name) 
    { 
     try 
     { 
      sp = new SerialPort(Port_name, 115200, Parity.None, 8, StopBits.One); // serial port parameters 
      sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); 
      sp.Open(); 
     } 
     catch (Exception err) 
     { 
      throw (new SystemException(err.Message)); 
     } 
    } 
private void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e) 
    { 

     // This below line is not need , sp is global (belongs to the class!!) 
     //SerialPort sp = (SerialPort)sender; 
     if (e.EventType == SerialData.Chars) 
     { 
      if (sp.IsOpen) 
      { 
       string w = sp.ReadExisting(); 
       if (w != String.Empty) 
       { 
        Invoke(new Action(() => Control.Update(w))); 
       } 
      } 
     } 
    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (sp == null || sp.IsOpen == false) 
     { 
      OpenCloseSerial(); 
     } 
    } 

该地块不更新!我完全猜不出原因。请告诉我,如果我的方法或代码存在错误。我得到一个错误:Invoke(new Action(()=> Control.Update(w)));当试图更新图表,以便我可以保存之后。

我再次出现以下错误:DrawPoint(zed1,point); 谢谢大家的时间。美好的一天。

干杯, Ram。

回答

0

要更新图表,您需要调用Invalidate方法。

当您从串口收到数据时,您需要将其转换为double[]并将这些点添加到您的PointPairList

PointPairList list = new PointPairList(); 
zed1.GraphPane.AddCurve("Test", list, Color.Red); 

//Convert the received data to double array 
double[] serialPortData = .... 

//You may want to clear list first, by list.Clear(); 
list.Add(serialPortData); 

//Invalidate the ZedGraphControl to update 
zed1.Invalidate(); 
+0

谢谢你的回复。我完全不能理解,你能解释一下吗?或者你有一个可以帮助我的例子吗?美好的一天。 –

+0

@ Ram.V哪部分你不明白?你是否能够获得'serialPortData'? – HebeleHododo

+0

我现在不能。之前我有一个richtextbox而不是zedgraph,我能够从com端口读取数据。但现在我开始尝试用zedgraph替换richtextbox,这很复杂。我试图更好地理解并完成这件事,但几天后我可能会转向图表。让我再次了解你的建议。美好的一天。 –