本文介绍了使用zedgraph绘制串口数据.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在尝试使用zedgraph绘制从串行端口读取的数据.我仍然在学习代码,因此无法推断出该图为何不起作用.请查看代码和建议;

I'm trying to plot the data read from a serial port using zedgraph. I'm still learing to code so I couldn't deduce why the plot does not work. Please have a look at the code and advice;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        // All members variables should be placed here
        // make it more readable..

        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);

        }

这是我打开串行端口并从中读取数据的方式;

This is how I open a serial port and read from it;

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();
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = @"C:\Users\varman\Documents\";
            saveFileDialog1.Title = "Save text Files";
            saveFileDialog1.CheckFileExists = true;
            saveFileDialog1.CheckPathExists = true;
            saveFileDialog1.DefaultExt = "txt";
            saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(saveFileDialog1.FileName, zed1.Text);
                zed1.Text = saveFileDialog1.FileName;
            }
        }

我在:Invoke(new Action(()=> Control.Update(w)))处收到错误消息;尝试更新图形时可以保存之后.

I get an error at : Invoke(new Action(() => Control.Update(w))); when trying to update the graph so that I can save after that.

我再次在以下位置出错:  DrawPoint(zed1,point); 

I again have an error at: DrawPoint(zed1, point); 

-RV

推荐答案

谢谢您在这里发布.

如果您的问题与zedgraph更相关,则可以在中发布新线程zedgraph支持.

For your question is more related to zedgraph, you could post a new thread in zedgraph support.

或者您可以使用图表在winform中.这是有关 

Or you could use chart in winform. Here is the MSDN article about 

教程:创建基本图表.

Visual C#讨论并询问C#编程语言,库,示例和工具.

The Visual C# discuss and ask the C# programming language, libraries, samples and tools.

如果您有一些语法或代码错误,请随时与我们联系.我们将尽力为您提供解决方案.

If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

感谢您的理解与合作.

最好的问候,

温迪


这篇关于使用zedgraph绘制串口数据.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 04:13