none
Displaying values t-wise (x-Axis) arriving from serial port in MS-Chart

    Question

  • I am getting via the serial port some values from an aquisition board. I want to display the values on Y-axis and also have the time on the x-Axis. The values come from the serial port in batches that are NOT of equal size. I therefore created a time-stamp of my own. It doesn't work as seen in the picture below. However, if I set it to x-coordinate to 0 in AddXY, i get my result but of course without the time on the x-axis. I want to have it in seconds. I use a timer set to get data every 500 ms.
      timer = new Timer();
                timer.Tick += Timer_Tick;
                timer.Interval = 500;
                chart1.ChartAreas[0].AxisY.Maximum = 1024;
                chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false;
                chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
                chart1.Series[0].Name = "Semnal EKG";
    
    private void Timer_Tick(object sender, EventArgs e)
            {
                string data;
              
                    data = _port.ReadExisting();
    
                    Axis ax = chart1.ChartAreas[0].AxisX;
                    ax.IntervalType = DateTimeIntervalType.Milliseconds;
                    ax.Interval = 500;
                    chart1.Series[0].XValueType = ChartValueType.DateTime;
                    ax.LabelStyle.Format = "ss";
    
                    var dataBlocks = data.Split('\n');
                    int counter = 0;
                    double delta = (double)dataBlocks.Length / (double)500;
                    foreach (var block in dataBlocks)
                    {
                        var numbers = block.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < numbers.Length; i++)
                        {
                            double n = double.NaN;
                            bool ok = double.TryParse(numbers[i], out n);
                            if (ok)
                            {
                                if (n > 100)
                                {
                                    double temp = counter * delta;
                                   // double temp = 0;
                                    chart1.Series[i].Points.AddXY(temp, n);
                                    counter++;
                                    if (chart1.Series[0].Points.Count > 1000)
                                        chart1.Series[0].Points.RemoveAt(0);
                                    //chart1.ResetAutoValues();
                                }
                            }
    
                        }
                    }
                }

    Thursday, April 20, 2017 6:13 PM

All replies