Frage Problem in Realtime data plotting

  • Saturday, August 11, 2012 5:51 AM
     
     

    Hello, i am really new to Visual studio and C# . i was trying to plot the data from the serial port. Its about my final year project, but um messing up everything. here is my code-

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Windows.Forms.DataVisualization.Charting;
    using System.IO.Ports;


    namespace Real_time_try1
    {
        public partial class Form1 : Form
        {
            // Chart data adding thread
            //private Thread serialPlotter;
            private Thread serialport_thread;
            // Thread Add Data delegate
            
            public delegate void AddDataDelegate();
            public AddDataDelegate addDataDel;


            private DateTime minValue, maxValue;// min and max value for X axis limitation
            public string[] portnames;            //for storing port names
            public int nofport;                //for counting existing no of ports
            
            public Form1()              
            {
                InitializeComponent();
                My_init();

            }
            public void My_init()
            {
                            
                plotstart_but.Enabled = false;
                plotstop_but.Enabled = false;
                connect_but.Enabled = false;
                plotChart.Series["Series1"].ChartType = SeriesChartType.Line;
                plotChart.Series["Series1"].BorderWidth = 1;
                plotChart.Series["Series1"].Color = Color.FromArgb(224, 64, 10);
                plotChart.Series["Series1"].ShadowOffset = 1;
                plotChart.Series["Series1"].XValueType = ChartValueType.DateTime;
                plotChart.Series["Series1"].YValueType = ChartValueType.Double;
                         
            }

            private void Form1_load(object sender, EventArgs e)
            {
                ThreadStart addDataThreadStart = new ThreadStart(AddDataThreadLoop);
                serialport_thread = new Thread(addDataThreadStart);
                addDataDel += new AddDataDelegate(AddData);
                this.plotstart_but_Click(null, EventArgs.Empty);
            }
            private void searchport_but_Click(object sender, EventArgs e)
            {
                portnames = SerialPort.GetPortNames();
                nofport = portnames.Length;
                currentdata_textBox.Text = "search Complete";
                
                if (nofport <= 0)
                {
                    MessageBox.Show("Sorry,\nno available \ndata source found ");
                    connect_but.Enabled = false;
                    return;
                }
                else
                {
                    MessageBox.Show("Serial port items found \n Please select the correct device");
                    connect_but.Enabled = true;
                }
                for (int i = 0; i < nofport; i++)
                {
                    portlist_combo.Items.Add(portnames[i]);
                }
                 
            }

            private void connect_but_Click(object sender, EventArgs e)
            {
                plotstart_but.Enabled = true;
                
                
                string sercomname = (String)portlist_combo.SelectedItem;
                try
                {
                    serialPort1.PortName = sercomname;
                    serialPort1.BaudRate = Convert.ToInt32(baudlist_combo.Text);
                    serialPort1.Open();
                    MessageBox.Show("Port:" + (String)portlist_combo.SelectedItem + "\nBaudrate:" + (String)baudlist_combo.SelectedItem);
                }
                catch (Exception)
                {
                
                }
                
            }

            private void plotstart_but_Click(object sender, EventArgs e)
            {
                plotstart_but.Enabled = false;
                plotstop_but.Enabled = true;                   
                plotChart.Series.Clear();
                Series newSeries = new Series("SeriesX");
                minValue = DateTime.Now;
                maxValue = minValue.AddSeconds(5);
                plotChart.ChartAreas[0].AxisX.Minimum = minValue.ToOADate();
                plotChart.ChartAreas[0].AxisX.Maximum = maxValue.ToOADate();
                newSeries.ChartType = SeriesChartType.Line;
                newSeries.BorderWidth = 1;
                newSeries.Color = Color.FromArgb(224, 64, 100);
                newSeries.ShadowOffset = 1;
                newSeries.XValueType = ChartValueType.DateTime;
                newSeries.YValueType = ChartValueType.Double;
                plotChart.Series.Add(newSeries);
                //newSeries.AxisLabel="ANKON";
                // start worker threads.
                if (serialport_thread.IsAlive == true)
                {
                    serialport_thread.Resume();
                }
                else
                {
                    serialport_thread.Start();
                }

            }

            private void plotstop_but_Click(object sender, EventArgs e)
            {

                // Suspend thread
                if (serialport_thread.IsAlive == true)
                {
                    serialport_thread.Suspend();
                }

                // Enable all controls on the form
                plotstart_but.Enabled = true;

                // Disable the Stop button
                plotstop_but.Enabled = false;
            }
            
            
            public void AddNewPoint(DateTime timeStamp, Series ptSeries)
            {
                //Series nseries= new Series();
                double rad_val = (double)serialPort1.ReadByte();
                // Add new data point to its series.
                ptSeries.Points.AddXY(timeStamp.ToOADate(), rad_val);

                
                // remove all points from the source series older than 5 seconds.
                double removeBefore = timeStamp.AddSeconds((double)(5) * (-1)).ToOADate();
                if (ptSeries.Points[0].XValue < removeBefore)
                {
                    while (ptSeries.Points[0].XValue < removeBefore)
                    {
                        ptSeries.Points.RemoveAt(0);
                    }
                }

                plotChart.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue;
                plotChart.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddSeconds(5).ToOADate();
                plotChart.Invalidate();
            }
            
            private void AddDataThreadLoop()
            {
                try
                {
                    while (true)
                    {
                        // Invoke method must be used to interact with the chart
                        // control on the form!
                        plotChart.Invoke(addDataDel);

                        // Thread is inactive for 200ms
                        Thread.Sleep(200);
                    }
                }
                catch
                {
                    // Thread is aborted
                }
            }

            public void AddData()
            {
                DateTime timeStamp = DateTime.Now;

                foreach (Series ptSeries in plotChart.Series)
                {
                    AddNewPoint(timeStamp, ptSeries);
                }
            }
        }

    }

    the problem is, i get nothing from it... and null exception arguement from som line is thrown. :( it asks me to assign a new operator to my thread, but i think, i did it in the Load event. Please help... :(