Displaying Chart for serial data received through serial port.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sakura17
    New Member
    • Oct 2014
    • 1

    Displaying Chart for serial data received through serial port.

    I am working on serial port programming. Here in my case, continuous set of data is coming through a serial port and i want to display that data in chart. means each time a new set of data is received the chart should change according to the value.
    My problem is when i am using message box it is working. but when i remove the message box, the chart is not appearing. What should i do? here is my code which is working...
    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.IO.Ports;
    using System.Threading;
    
    namespace MaintainGraph
    {
        public partial class Form1 : Form
        {
         
            public Form1()
            {
                InitializeComponent();
                //ResizeRedraw = true;
            }
    
            private void rcv_btn_Click(object sender, EventArgs e)
            {
                SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
                try
                {
                    if (!port.IsOpen)
                        port.Open();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Thread.Sleep(2000);
                port.ReadTimeout = 1000;
                while (true)
                {
                    double[] arr = new double[6];
                    try
                    {
                        arr[0] = Convert.ToDouble(port.ReadLine());
                        arr[1] = Convert.ToDouble(port.ReadLine());
                        arr[2] = Convert.ToDouble(port.ReadLine());
                        arr[3] = Convert.ToDouble(port.ReadLine());
                        arr[4] = Convert.ToDouble(port.ReadLine());
                        arr[5] = Convert.ToDouble(port.ReadLine());
                    }
                    
                    catch (System.TimeoutException ex)
                    {
                    }
                   
                    
                    MessageBox.Show("Hello");
    
                    txt.Text = arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3] + " " + arr[4] + " " + arr[5];
                    this.chart1.Series["Values"].Points.AddXY("Depth", arr[0]);
                    this.chart1.Series["Values"].Points.AddXY("PositionX", arr[1]);
                    this.chart1.Series["Values"].Points.AddXY("PositionY", arr[2]);
                    this.chart1.Series["Values"].Points.AddXY("Heading", arr[3]);
                    this.chart1.Series["Values"].Points.AddXY("Surge", arr[4]);
                    this.chart1.Series["Values"].Points.AddXY("Alimetry", arr[5]);
                    MessageBox.Show("Bye");
                    Thread.Sleep(1000);
                    try
                    {
                        chart1.Series["Values"].Points.Clear();
                    }
                    catch (System.ArgumentException ex)
                    { }
                }
               
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
           }
    }
    }
    Last edited by Frinavale; Oct 29 '14, 01:05 PM. Reason: Added code tags.
Working...