0

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

}
}

Thank you for reading!

chuonghd
  • 103
  • 1
  • 2
  • 8
  • I don't know the ZedGraph component, but it seems to me that it is the bottleneck here. ZedGraph was probably never meant to handle continuous updates. My advise is to use a different graph component that is designed for the task. Either that or add results in chunks (10, 20, 50 or maybe event 100 results at a time) to the ZedGraph. – Casperah May 28 '13 at 18:01
  • Have you tried updating the graph at a slower rate? Perhaps a new thread could be used to sleep for some time, check for data in a buffer, then update the graph? – PeskyGnat May 29 '13 at 16:59

2 Answers2

1

The problem is that you call invalidate with every point you draw. That produces a very high processor load. I'm working on a very similar project, USB device, realtime plotting data. I use a separate thread for data acquisition. This thread creates an event, every time it receives a datapacket. The data is put into a queue. The graph is updated with a timer. You find an example here. In this code the graph is updated every 50 ms, it is not really necessary to draw faster. In the timertick I check the size of the Queue and draw more or less points and then call invalidate. I don`t know if this is a good solution(just 6month experience in C#), but it works quite well with 7500 points. You should try to use a timer for the refresh of the graph first.

HebeleHododo
  • 3,620
  • 1
  • 29
  • 38
Olli_Li
  • 26
  • 1
0

1). Create and format the the curve only once. store it at the module level.

2). When adding a point use curve.AddPoint(); then zg.Refresh();

//this is just to add a point to the plot, the curve object should have already been created
private void draw(int sensor, int t)
    {
        //convert tick to sec (uP clock rate = 16MHZ)
        time = time + (t / 16000000.0);

        //curve should be a module-level variable already set up with proper formatting,
        //just no points yet
        curve.AddPoint(time, sensor);

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

        zg.AxisChange();
        zg.Refresh();

    }
mike
  • 2,149
  • 20
  • 29