0
Answer

C# - Zedgraph, plotting serial data too much delay and time

hoang chuong

hoang chuong

11y
2.9k
1

I'm new to C# programming and trying to write an application which is part of my final thesis.

I have a microprocessor that continuously send data from a sensor to my computer via serial port. All I want is to plotting this data using Zedgraph.

The problem is that the graph got too much delay and time lag. It seems the problem happens because I continuously update the whole graph at a very high rate. I have stucked on this problem in a week and still dont find out a solution. I'll be more than happy if someone can help me out.

This is my code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using ZedGraph;

using System.IO.Ports;

using System.Threading;


namespace DynamicData

{

public partial class Form1 : Form

{

        private SerialPort port;

        private string buffer = "";


        private void connect()

        {

            port = new SerialPort("COM8", 115200, Parity.None, 8, StopBits.One);

            port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);

            if (!port.IsOpen) port.Open();

        }


public Form1()

{

InitializeComponent();

}



private void Form1_Load( object sender, EventArgs e )

{

            connect();

GraphPane myPane = zedGraphControl1.GraphPane;

            RollingPointPairList list = new RollingPointPairList(500);

            LineItem curve = myPane.AddCurve( "Sensor", list, Color.Blue, SymbolType.None );


            myPane.XAxis.Scale.Min = 0;

            myPane.XAxis.Scale.Max = 10;

            myPane.YAxis.Scale.Min = 0;

            myPane.YAxis.Scale.Max = 300;

            myPane.XAxis.Scale.MinorStep = 0.5;

            myPane.XAxis.Scale.MajorStep = 1;

          

            zedGraphControl1.AxisChange();


}



        private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

        {

            //sample data: ;100*100000:

            //sampling rate ~100Hz

            buffer += port.ReadExisting();


            //flush incomplete package

            while (buffer[0] != ';')

            {

                buffer = buffer.Remove(0, 1);

                if (buffer.Length < 1) break;

            }


            //got a complete package, go to data handling

            while (buffer.Contains(":"))

            {

                DataHandling();

            }

        }



        private void DataHandling()

        {

            string[] nameArray = buffer.Split(new[] { ";", ":", "*" }, StringSplitOptions.RemoveEmptyEntries);

            

            //plot sensor data vs. time

            draw(Convert.ToInt32(nameArray[0]), Convert.ToInt32(nameArray[1]));

            

            //remove handled package in buffer

            var index = buffer.IndexOf(":");

            buffer = buffer.Remove(0, index + 1);


        }


        double time = 0;

        private void draw(int sensor, int t)

        {

            //convert tick to sec (uP clock rate = 16MHZ)

            time = time + (t / 16000000.0);


            // Get the first CurveItem in the graph

            LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;

           

            // Get the PointPairList

            IPointListEdit list = curve.Points as IPointListEdit;

            list.Add(time, sensor);



            //Keep the X scale at a rolling 10 second interval, with one

            //major step between the max X value and the end of the axis

            Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;

            if (time > xScale.Max - xScale.MajorStep)

            {

                xScale.Max = time + xScale.MajorStep;

                xScale.Min = xScale.Max - 10.0;

            }


            //Display sensor data

            this.Invoke(new Action(() => { textBox1.Text = byte1.ToString(); }));

            

            axisChangeZedGraph(zedGraphControl1);

            

        }


        delegate void axisChangeZedGraphCallBack(ZedGraphControl zg);

        private void axisChangeZedGraph(ZedGraphControl zg)

        {

            if (zg.InvokeRequired)

            {

                axisChangeZedGraphCallBack ad = new axisChangeZedGraphCallBack(axisChangeZedGraph);

                zg.Invoke(ad, new object[] { zg });

            }

            else

            {

              //  zg.AxisChange();

                zg.Invalidate();

                zg.Refresh();

            }

        }


}

}