Click here to Skip to main content
15,662,668 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have used A WPF Graph Control Library[^]

In my project i have two buttons start and stop . WHen i click on start data should be read and plotted . When i click stop it should stop . I have used background worker to read data and plot graph . Data are coming and its plotted . In my coding measure.clear() function clears the data and plots it . If comment it , Plots rae joined between two datas but the stop button freezes.

Below is my coding

C#
private void DrawGraph()
        {

            DNBSoft.WPF.WPFGraph.WPFGraphSeries series = new DNBSoft.WPF.WPFGraph.WPFGraphSeries();

            //for (int i = 0; i < 16; i++)
            //{
            //    WPFGraphDataPoint f = new WPFGraphDataPoint();
            //    f.X = i * 7;
            //    f.Y = i;
            //    series.Points.Add(f);
            //}

            lock (measures)
            {
                //add each measure into the curve
                for (int i = 0; i < measures.Count; i++)
                {
                    //fill each with what ever you want
                    WPFGraphDataPoint f = new WPFGraphDataPoint();

                    String h=string.Format("{0:hh.mm}", DateTime.Now);
                    double time = Convert.ToDouble(h);


                    f.X = time;

                        f.Y = measures[i];
                series.Points.Add(f);
                graphDisplay.Series.Add(series);

                }
                //all measures have been added
                //we can empty the list

                //measures.Clear();
            }



                //WPFGraphDataPoint f1 = new WPFGraphDataPoint();
                //f1.X = 15;
                //f1.Y = 46.5636356;
                //series.Points.Add(f1);


                //WPFGraphDataPoint f2 = new WPFGraphDataPoint();
                //f2.X = 35;
                //f2.Y = 87.5636356;
                //series.Points.Add(f2);


                //WPFGraphDataPoint f3 = new WPFGraphDataPoint();
                //int i;
                //for (i = 0; i < 10; i++)
                //{
                //    f3.X = 56 + i;
                //    f3.Y = 37.5636356 + i;
                //    series.Points.Add(f3);
                //}


            }


        private void graphDisplay_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            worker.RunWorkerAsync();

        }



        private void button2_Click(object sender, RoutedEventArgs e)
        {
            worker.CancelAsync();
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //the worker has completed
            //do whatever you want here

        }
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            //put all your serial port code here
            sprt = new SerialPort("COM3");
            sprt.BaudRate = 9600;
            sprt.Parity = Parity.None;
            sprt.StopBits = StopBits.One;
            sprt.DataBits = 8;
            sprt.Handshake = Handshake.None;
            try
            {
                sprt.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Check port");
                return;
            }
            //worker.CancellationPending will change to true when CancelAsync is called
            //(so when the user clicks button2).
            //while the worker should still continue, read incoming data
            while (!worker.CancellationPending)
            {
                //wait for data to come...
                System.Threading.Thread.Sleep(100);
                indata = sprt.ReadExisting();


                //MessageBox.Show(indata);
                ////extract the values from the read data
                ////be careful here: make sure the read data is complete...

                if (indata != null)
                {

                    try
                    {
                        //MessageBox.Show(indata);
                        int len = indata.Length;
                        if (len > 40)
                        {
                            string[] splt = indata.Split(':');
                            string chop = splt[2];
                            string final = chop.Remove(5);

                            //MessageBox.Show(final);
                            //float d = Convert.ToSingle(final);
                            d = Convert.ToDouble(final);
                            //string s = Convert.ToString(d);
                            //MessageBox.Show(s);
                        }

                    }
                    catch
                    {
                        //MessageBox.Show("Not received data");
                    }

                }







                //update the measures
                //measures is shared by several threads: you must lock it to access it safely
                lock (measures)
                {
                    measures.Add(d);
                }
                //update the graph
                Dispatcher.Invoke((Action)(() => DrawGraph()));
            }
            //user wants to stop the worker
            sprt.Close();
        }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900