Plotting serial port data using zedgraph (data vs time)

You can post your problem related to Microsoft Visual Studio Projects here. We will try our best to help you out.
Post Reply
ramv
Posts: 4
Joined: Tue Mar 14, 2017 10:50 am

Plotting serial port data using zedgraph (data vs time)

Post by ramv » Tue Mar 14, 2017 10:58 am

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.
To open and read from a serial port, I use a combox and a couple of buttons (and then later I try to save it to a text file).
The plot does not function! I can't quite guess why. Please tell me if there's mistakes in my approach or the code.

Code: Select all

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

}
//serial port data collection
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();
        }
    }
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 is too much for me right now. I'm thinking of replacing it with charts if I still struggle. But I'd love to hear your advise.
Thank you all for your time. Good day.

Cheers,
Ram.

theenggprojects
Site Admin
Posts: 25
Joined: Tue Feb 07, 2017 12:51 pm

Re: Plotting serial port data using zedgraph (data vs time)

Post by theenggprojects » Tue Mar 14, 2017 12:50 pm

I think the problem here is that you are trying to plot the string. Instead you should first convert your string into integer and then plot it.

Moreover, if you ask me then I would suggest you to use simple Charts.
  • First of all, receive the complete string from your Serial Port and then also make sure that you have received the complete data. If you are having problem in serial communication then you should read How to use Serial Port in VB 2010
  • Once you got the data from Serial Port then you need to convert it into int which is quite simple, you can use strtoInt() and finally plot it on your Chart.
Let me know if you still have issues.

ramv
Posts: 4
Joined: Tue Mar 14, 2017 10:50 am

Re: Plotting serial port data using zedgraph (data vs time)

Post by ramv » Wed Mar 15, 2017 9:18 pm

@theenggprojects thank you for the reply. I did not realize that I was trying to plot the strings! I'm shifting my interest to chart now. This is really good advise. I will post the progress. Good day :)

theenggprojects
Site Admin
Posts: 25
Joined: Tue Feb 07, 2017 12:51 pm

Re: Plotting serial port data using zedgraph (data vs time)

Post by theenggprojects » Fri Mar 17, 2017 6:48 pm

Yeah it happens :D
Let me know whats the outcome of chart ....

ramv
Posts: 4
Joined: Tue Mar 14, 2017 10:50 am

Re: Plotting serial port data using zedgraph (data vs time)

Post by ramv » Sat Mar 18, 2017 3:51 pm

Hi @theenggprojects, this is quite the challenge! I've been trying that for sometime in these past few days, but upto no progress! I still haven't managed to connect the serial port to the chart. Does this forum have a chat or message box? Let me know, or I will post the progress here today evening. Good day :)

Junaid_Shahid
Posts: 117
Joined: Tue Feb 14, 2017 9:00 am

Re: Plotting serial port data using zedgraph (data vs time)

Post by Junaid_Shahid » Mon Mar 20, 2017 12:36 pm

ramv wrote:
Sat Mar 18, 2017 3:51 pm
Hi @theenggprojects, this is quite the challenge! I've been trying that for sometime in these past few days, but upto no progress! I still haven't managed to connect the serial port to the chart. Does this forum have a chat or message box? Let me know, or I will post the progress here today evening. Good day :)
Hey dear,

Yes we have the chat system please check mChat page to start chatting :)
Have a good day :)

Post Reply