Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Plotting serial port data using zedgraph

Status
Not open for further replies.

ramv

Newbie level 4
Newbie level 4
Joined
Mar 9, 2017
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Germany
Activity points
65
Hello,

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;
Code:
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;
Code:
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;
            }
        }

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

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

I think zedgraph right now is probably too much! Maybe I will try a couple of days and switch to chats. Any advice is appreciated.

Regards,
Ram.
 

for reading from serial port other available libraries may be an option.
It is not known whether any license limitation(for the organisation you belong to) for your application.

language limitation (only c++ or c is enough ?)

then other ways can be investigated.
 

Hi @02srizbf,
I'm trying to teach myself, so I am open to any ideas. I'm using c# now. Do you maybe the zedgraph is too much and I could turn to charts perhaps? Good day.

for reading from serial port other available libraries may be an option.
It is not known whether any license limitation(for the organisation you belong to) for your application.

language limitation (only c++ or c is enough ?)

then other ways can be investigated.
 

reading data from serial port is one half of the story.
presenting the data on the PC in required format is another.

If you have already the codes for reading data then a plotting library is the choice.

you can refer the page:

https://sites.google.com/site/brnstin/

for one such easy to use library .
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top