Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Reading Serial Port and Plotting with Chart (C#)- textbox and chart!

Status
Not open for further replies.

ramv

Newbie level 4
Newbie level 4
Joined
Mar 9, 2017
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Germany
Activity points
65
Hello,

I'm trying to read a serial port data from a controller and plot it using chart. I could read the data from the controller and see the data in a textbox earlier. Now I'm trying to use a chart along with the textbox. There are complication when I'm trying to use both together. I get an exception because I'm trying "data = sp.ReadLine();" for chart. I'd like to keep the textbox just to verify what I'm plotting, but I understand that I can also do that using putty. There's no response even when I use only the chart to plot. And I cannot pin-point the problem. Because to my understanding I think I have followed the steps to establish it, but then I'm failing anyway! Please do have a look at what I have tried so far and please do let me know if I'm doing something wong.

Chart and Form1

Code:
namespace Serial_receive
{
    public partial class Form1 : Form
    {
        string t;
        SerialPort sp;
        double rt = 0;
        //Boolean i = false;
        private string data;
        private DateTime datetime;

        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;

            // User can already search for ports when the constructor of the FORM1 is calling 
            // And let the user search ports again with a click
            // Searching for ports function

            SearchPorts();
        }     

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = SerialPort.GetPortNames();
            timer1.Start();
            Text = "Serial Channel to FRDM-KW40Z";
        }
        
        private void TextBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            rt = rt + 0.1;
        }

        private void chart1_Click_1(object sender, EventArgs e)
        {
            ChartArea CA = chart1.ChartAreas[0];  // quick reference
            CA.AxisX.ScaleView.Zoomable = true;   // see small button on the horizontal scroll for reset??
            CA.CursorX.AutoScroll = true;
            CA.CursorX.IsUserSelectionEnabled = true;
            //
            var dataBlocks = data.Split('\n');

            foreach(var block in dataBlocks)
            {
                var numbers = block.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                rt += 100; //some time interval
                for (int i = 0; i<numbers.Length; i++)
                {
                    double n = double.NaN;  //represents a value that is Not a Number. Field is constant
                    bool ok = double.TryParse(numbers[i], out n);
                    if (ok) chart1.Series[i].Points.AddXY(rt, n);
                    else
                    {
                        int p = chart1.Series[i].Points.AddXY(rt, n);
                        chart1.Series[i].Points[p].IsEmpty = true;
                        Console.WriteLine("well shoot! try again..");
                    }

                }

            }

        }
Combox1 for listing available ports and connecting

Code:
private void button1_Click_1(object sender, EventArgs e)
        {
            //comboBox1.Items.Clear();
            SearchPorts();
        }

        void SearchPorts()
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
            }
        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (sp == null || sp.IsOpen == false)
            {
                OpenCloseSerial();
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            // Catch exception if it will be thrown so the user will see it in a message box
            OpenCloseSerial();
        }
        void OpenCloseSerial()
        {
            try
            {
                if (sp == null || sp.IsOpen == false)
                {
                    t = comboBox1.Text.ToString();
                    sErial(t);
                    button2.Text = "Close Serial port"; // button text
                }
                else
                {
                    sp.Close();
                    button2.Text = "Connect and wait for inputs";   // button text
                }
            }
            catch (Exception err)   // catching error message
            {
                MessageBox.Show(err.Message);   // displaying error message
            }
        }

        void sErial(string Port_name)
        {
            try
            {
                sp = new SerialPort(Port_name, 115200, Parity.None, 8, StopBits.One);   // serial port parameters
                sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                sp.Open();
            }
            catch (Exception err)
            {
                throw (new SystemException(err.Message));
            }
        }
Textbox and chart data and saving data as text file;

Code:
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            if (e.EventType == SerialData.Chars)
            {
                if (sp.IsOpen)
                {
                    string w = sp.ReadExisting();
                    if (w != String.Empty)
                    {
                        Text += "\r\n";
                        Invoke(new Action(() => TextBox1.AppendText(w)));
                    }
                }
            }
            data = sp.ReadLine();
            chart1.Series["Data1"].Points.AddXY(rt, data);
            Invoke(new EventHandler(displayData_Event));
        }

        private void displayData_Event(object sender, EventArgs e)
        {
            datetime = DateTime.Now;
            string time = datetime.Day + "/" + datetime.Month + "/" + datetime.Year + "\t" + datetime.Hour + ":" + datetime.Minute + ":" + datetime.Second;
            //data.AppendText(time + "\t" + data + "\n");
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = @"C:\Users\varman\Documents\";
            saveFileDialog1.Title = "Save text Files";
            saveFileDialog1.CheckFileExists = true;
            saveFileDialog1.CheckPathExists = true;
            saveFileDialog1.DefaultExt = "txt";
            saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(saveFileDialog1.FileName, TextBox1.Text);
                TextBox1.Text = saveFileDialog1.FileName;
            }
        }
    }
}
Thank you very much for your time. Good day.

Cheers
 

Hi,

I assume there is a problem with the data format.

What is the dataformat and protocol used for RS232?
..The text box usually needs pritable ASCII strings.
and what data format does the chart need?

Klaus
 

Hi,

data comes from a 16 bit ADC. Earlier it had text with the values, for instance something like " raw adc value: 65535. But now I've replaced it to just the numerics.
 

Shouldn't you instantiate the "data = sp.ReadLine();" assignment just when the "if (sp.IsOpen)" condition is true ?
 

Hi,

data comes from a 16 bit ADC. Earlier it had text with the values, for instance something like " raw adc value: 65535. But now I've replaced it to just the numerics.
This doesn´t tell anything about the dataformat (via RS232), nor the protocol.

Klaus
 

Shouldn't you instantiate the "data = sp.ReadLine();" assignment just when the "if (sp.IsOpen)" condition is true ?

Hi, Okay! I thought it doesn't matter, but it does. I've done just like u have said. But wouldn'tit just skip if sp.IsOpen is false?

- - - Updated - - -

Hi,


This doesn´t tell anything about the dataformat (via RS232), nor the protocol.

Klaus

Hi, It's an uart over usb interface (16 bits resolution integers) and not rs232.
 

Hi,

It seems you don´t know what data format it is.
At least I´v never seen an RS232 transferrring 16bit binary data. Most usual is 8 bits.

Then the dataformat or the parsing and conversion of data may be the problem.

Please learn about data format and how to convert them.


Klaus
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top