How to Plot All the data Points Real Time

Budhikakgsl

New member
Joined
Mar 10, 2021
Messages
4
Programming Experience
Beginner
I want to plot all the data points, I get from the TCP server. But I could not figure out a way to plot all the data points. Instead currently I print the string to the text box. From the text box only the first line is printed. This is a real time data plotting for an oscilloscope GUI. How can I plot all the values.

I tested with a sine wave with a I2S mic, it gave a distorted signal when plotted with the following code.
C#:
int t;

        private void Timer1_Tick(object sender, EventArgs e)
        {
            one = new Thread(test);
            one.Start();
            t++;
        }

            public void test()
        {
            byte[] bytes = new byte[client.ReceiveBufferSize];
            var readCount = stream.Read(bytes, 0, bytes.Length);

            string datastring = Encoding.UTF8.GetString(bytes);
            txtdata.Invoke((MethodInvoker)(() =>
            
            txtdata.Text = datastring.Substring(0, 100)));
            
            txtno.Invoke((MethodInvoker)(() =>
            txtno.Text = ("\nnumber of bytes read: " + readCount)));

            String ch1 = txtdata.Text; ;
            String[] ch1y = ch1.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            for (int a = 1; a < ch1y.Length - 1; a++)
            {
                chart1.Invoke((MethodInvoker)(() =>
            
            chart1.Series[0].Points.AddXY(t, Convert.ToDouble(ch1y[a]))));
            }

        }
 
One of the biggest reasons that beginners are unable to solve problems is that they expect to be able to go from a complex idea to code in one fell swoop. That's not how it works. What you need to do is break the problem down into parts and tackle each part individually. If one part relies on another part, don't even try to tackle the second part until you have addressed the first part. Each part might involved creating a separate test project that isolates just the functionality involved in that part.

In this case, I would start by creating some static data and displaying it in a Chart. If that was successful, I'd try updating the data and displaying the result on the Click of a Button. If that was successful then you know that you can update a Chart on demand, so you can swap out the Button.Click for a Timer.Tick.

So, can you do the first and second parts? If not, you shouldn't even be asking about the third part. If you can then your question should be specifically about adapting what you already have from the second part to what you need in the third part. This is how software development works. It's far more than just writing code.
 
Last edited:
I edited the code as below. Instead of using the thread I plot points at each timer.tick event. But still not getting a smooth sine wave.
C#:
private void Timer1_Tick(object sender, EventArgs e)
        {
            byte[] bytes = new byte[client.ReceiveBufferSize];
            var readCount = stream.Read(bytes, 0, bytes.Length);

            string datastring = Encoding.UTF8.GetString(bytes);
            txtdata.Invoke((MethodInvoker)(() =>
                        txtdata.Text = datastring.Substring(0, 100)));

            txtno.Invoke((MethodInvoker)(() =>
            txtno.Text = ("\nnumber of bytes read: " + readCount)));

            String ch1 = txtdata.Text; ;
            String[] ch1y = ch1.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            double[] val = new double[ch1y.Length];

            for (int a = 1; a < ch1y.Length - 1; a++)
            {
                val[a] = double.Parse(ch1y[a]);


                chart1.Series[0].Points.Add(val[a]);
            }
            if (chart1.Series[0].Points.Count > 5)
                chart1.Series[0].Points.RemoveAt(0);
            chart1.ChartAreas[0].AxisX.Minimum = double.NaN;
            chart1.ChartAreas[0].AxisX.Maximum = double.NaN;
        }
 
Back
Top Bottom