Search code examples
c#visual-studiozedgraph

zed graphs real time plotting speed reduces over time. Am i clogging up the serial port? C#


I’m currently venturing into the world of c# and have created my first windows application. Everything is working however I believe its efficiency here that is letting me down.

I am currently reading serial data from an Arduino. All the data is send in the format of “Letter” (newline) “Number” (newline) “letter” etc. From here I sort the data into relevant columns in a table. I have 6 sets of data coming from the Arduino. This data is then plotted onto a graph using zed graphs, with only 5 seconds of data being shown at once. So a moving axis.

After about 20s of plotting data to the graphs the plotting speed slows and eventually I am left with a moving graph with no points as they are trailing behind.

I tried flushing the serial buffer but this slowed everything down even more.

private void IncomingDataSort()
{ 

        string IncomingSerial = serialPort1.ReadLine(); // Read incomming serial data
        string StrIncomingSerial = IncomingSerial.ToString(); // convert this data to workable string
        elapsed_time = (stopwatch.ElapsedMilliseconds); // How many milliseconds since stopwatch (read serial button) started
        elapsed_time_sec = elapsed_time / 1000;
        Timems.Text = elapsed_time.ToString();

        if (StrIncomingSerial.Contains("Z") || StrIncomingSerial.Contains("Y")) // If this string contains a "Z" or "Y"
        {
            if (StrIncomingSerial.Contains("Z"))
            {
                string Number = serialPort1.ReadLine(); // Read Serialport
                double Num; // Create variable "Num"
                bool isNum = double.TryParse(Number, out Num); // Is the incomming serial data a number?
                if (isNum) // If it is a number...
                {
                    int NumberInt = Convert.ToInt16(Number); // convert string to int
                    Heat1Temp.Text = Number;
                }
            }
            if (StrIncomingSerial.Contains("Y"))
            {
                string Number = serialPort1.ReadLine(); // Read Serialport
                double Num; // Create variable "Num"
                bool isNum = double.TryParse(Number, out Num); // Is the incomming serial data a number?
                if (isNum) // If it is a number...
                {
                    int NumberInt = Convert.ToInt16(Number); // convert string to int
                    Heat2Temp.Text = Number;
                }
            }
            CreateGraph1(zedGraphControl1, elapsed_time, Convert.ToInt16(Heat1Temp.Text), Convert.ToInt16(Heat2Temp.Text)); // plot gragh
        }
       }

    private void CreateGraph1(ZedGraphControl zgc, long time, int IncomingData, int IncomingData2)
    {
        GraphPane myPane = zgc.GraphPane; // setup graph

        zgc.AutoScroll = true;
        myPane.Title = "Graph 1"; // Titles 
        myPane.XAxis.Title = "Time (s)"; 
        myPane.YAxis.Title = "";
        myPane.Legend.IsVisible = false; // remove legend (DONT MAKE TRUE!!)
        myPane.XAxis.Min = myPane.XAxis.Max - GraphTimeSpan;

        PointPairList GraphInput = new PointPairList(); // create a new list
        PointPairList GraphInput2 = new PointPairList(); // create a new list

        long x;
        int y1;
        long x2;
        int y2;
        x = time; // x axis 1
        y1 = IncomingData; // y axis 1
        x2 = time; // x axis 2
        y2 = IncomingData2; // y axis 2

        GraphInput.Add(x, y1); // add to list
        GraphInput2.Add(x2, y2); // add to list

        LineItem myCurve = myPane.AddCurve("FirstSettings", GraphInput, Color.Red, SymbolType.Diamond); // draw points
        LineItem myCurve2 = myPane.AddCurve("SecondSettings", GraphInput2, Color.Blue, SymbolType.Diamond);

        zgc.AxisChange(); // update axis
        zgc.Refresh();
    } 

Solution

  • As a follow on to my comment - rather than adding a new curve each time you get data you only need to append the new points to the existing PointPairLists - that way you will only ever have the two curves the graph needs to handle. Separating the initialisation & update of the graph would be a good idea - so something like this :

    PointPairList GraphInput;
    PointPairList GraphInput2;
    LineItem myCurve;
    LineItem myCurve2;
    
    private void InitGraph(ZedGraphControl zgc)
    {
        GraphPane myPane = zgc.GraphPane; // setup graph
    
        zgc.AutoScroll = true;
        myPane.Title = "Graph 1"; // Titles 
        myPane.XAxis.Title = "Time (s)"; 
        myPane.YAxis.Title = "";
        myPane.Legend.IsVisible = false; // remove legend (DONT MAKE TRUE!!)
    
        GraphInput = new PointPairList(); // create a new list
        GraphInput2 = new PointPairList(); // create a new list
        myCurve = myPane.AddCurve("FirstSettings", GraphInput, Color.Red, SymbolType.Diamond); // draw points
        myCurve2 = myPane.AddCurve("SecondSettings", GraphInput2, Color.Blue, SymbolType.Diamond);
    }
    
    private void UpdateGraph(ZedGraphControl zgc, long time, int IncomingData, int IncomingData2)
    {
        GraphPane myPane = zgc.GraphPane; // setup graph
    
        myPane.XAxis.Max = time;
        myPane.XAxis.Min = myPane.XAxis.Max - GraphTimeSpan;
    
        GraphInput.Add(time, IncomingData); // add to list
        GraphInput2.Add(time, IncomingData2); // add to list
    
        // AT THIS POINT YOU COULD SEARCH THROUGH THE POINT LIST & REMOVE ANY POINTS PRIOR TO THE MIN TIME
        while (GraphInput[0].X < myPane.XAxis.Min)
        {
          GraphInput.RemoveAt(0);
        }
        while (GraphInput2[0].X < myPane.XAxis.Min)
        {
          GraphInput2.RemoveAt(0);
        }
    
        zgc.AxisChange(); // update axis
        zgc.Refresh();
    } 
    

    Another time saving may be to update each curve independently - as you are adding points to GraphInput & GraphInput2 regardless of whether you actually had points to enter for both. Having separate UpdateGraph1 & UpdateGraph2 methods may cut the points to be plotted in half.

    There are several other things that could be tidied up in the code too - but they are not causing you severe time issues (you convert the numbers from strings, then convert back to strings, then convert back to numbers - for example).