Click here to Skip to main content
14,774,504 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
Hello

i Need help for a realtime plot.
What is wrong i dont found.

What is Forget?




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

namespace Graphs
{
    public partial class Form1 : Form
    {
        private string data;
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();   //Programm schliessen
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
            }
        }

        string t;

        private void button2_Click(object sender, EventArgs e)
        {
            t = comboBox1.Text.ToString();

            sErial(t);

        }
        SerialPort sp;
        void sErial(string Port_name)
        {
            sp = new SerialPort(Port_name, 9600, Parity.None, 8, StopBits.One);

            sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            sp.Open();


        }

        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {

            SerialPort sp = (SerialPort)sender;

            string w = sp.ReadLine();

            //string msg = sp.ReadExisting();
            if (w != String.Empty)
            {
                Invoke(new Action(() => richTextBox1.AppendText(w)));
            }

            
                  this.chart1.Series["Data1"].Points.AddXY(rt, data);


        }

        double rt = 0;
        Boolean i = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            rt = rt + 0.1;
        }

    }






}


thanks

What I have tried:

I have no work with Charts .
I am beginner
Posted
Updated 6-Oct-16 3:14am

1 solution

At first :
you add your Variable 'data' as Value to the Chart - so :
1st mistake : 'data' must be a number-variable, perhaps Integer or Single or Double ... but not a string !!!
2nd mistake : where do you assign 'data' (if it is a number-variable) ?

What Kind of data do you receive with sp.Readline inside the method 'DataReceivedHandler' ?
Could you see values inside your RichTextbox which make sense ?
   
Comments
Member 12775240 6-Oct-16 9:22am
   
Yes i can see the values on Richtextbox.
It is Ligtsensor data. Yes it is number

instead string double write?



thanks
Ralf Meier 7-Oct-16 1:52am
   
Yes ... you should convert the string (for example) to a double-variable and put this variable into the codeline :
this.chart1.Series["Data1"].Points.AddXY(rt, myDouble);
Ralf Meier 9-Oct-16 8:20am
   
Every thing allright now ...?

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