none
Filter string RRS feed

  • Question

  • Hi,

    My application communicates with device via serial comm, i send command, read from device and write the values in a string, i want the values are in real time so i refresh the comm within 500 ms interval. Like in the image bellow. I am looking for an effective way to store that data in a table with 2 columns first for the variable which is "VV_IN" for  example and another column the value "11.83" in this case. Is there a way to extract and sort that information in a table? Im not asking from you to write the code for me i just need advice and an example. 

    My code is following : 

    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.Text.RegularExpressions;
    using System.Threading;
    using System.Timers;
    
    
    namespace ReadVal
    {
        public partial class Form1 : Form
        {
    
            SerialPort comPort = new SerialPort();
    
            public string buffer;
            public string value;
            public Form1()
            {
                InitializeComponent();
                SetDefault();
                comPort.Open();
                System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
                timer.Interval = 500;
                timer.Tick += new EventHandler(TimerEventProcessor);
                timer.Start();
                
            }
    
            private void SetDefault()
            {
                comPort.BaudRate = 9600;
                comPort.DataBits = 8;
                comPort.StopBits = StopBits.One;
                comPort.Parity = Parity.None;
                comPort.PortName = "COM4";
            }
            private void ReadFrom()
            {
                if (!(comPort.IsOpen == true)) comPort.Open();
                if (comPort.IsOpen == true)
                {
                    comPort.WriteLine("VA VV_IN");
                    comPort.WriteLine("VA VV_OUT");
                    comPort.WriteLine("VA VV_UPS");
                    comPort.WriteLine("VA VV_ACCU");
                    comPort.WriteLine("VA VV_CELL1");
                    comPort.WriteLine("VA VV_CELL2");
                    comPort.WriteLine("VA VV_CELL3");
                    comPort.WriteLine("VA CS_BATTERY_IN");
                    comPort.WriteLine("VA CS_BATTERY_OUT");
    
                }
                Thread.Sleep(200);
                buffer = comPort.ReadExisting();
                Thread.Sleep(200);
                richTextBox1.Text = buffer;
    
    
            }
            private void Form1_Load(object sender, EventArgs e)
            {
            }
            private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
            {
                ReadFrom();
            }
        }
    }
    

    Thank you upfront

    Regards, Marin

    Friday, December 15, 2017 1:33 PM

Answers

  • Hi Marin,

    You can traverse each of the lines in RichTextBox, then separated by a space character and stored in a dataTable:

            private void button1_Click(object sender, EventArgs e)
            {
                string[] str = new string[] { };
                DataTable dt = new DataTable();
                dt.Columns.Add("Column1", typeof(string));
                dt.Columns.Add("Column2", typeof(string));
                int i = 0;
                foreach (string s in richTextBox1.Lines)
                {
                    str = s.Split(' ');
                    //dt.Rows.Add(str);
                    dt.Rows.Add();
                    dt.Rows[i][0] = str[1];
                    dt.Rows[i][1] = str[2];
                    if (i < richTextBox1.Lines.Length - 1)
                    {
                        i++;
                    }
                }
    
                dataGridView1.DataSource = dt;
            }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by xBehemoth Monday, December 18, 2017 9:17 AM
    Monday, December 18, 2017 6:32 AM

All replies

  • Hi Marin,

    You can traverse each of the lines in RichTextBox, then separated by a space character and stored in a dataTable:

            private void button1_Click(object sender, EventArgs e)
            {
                string[] str = new string[] { };
                DataTable dt = new DataTable();
                dt.Columns.Add("Column1", typeof(string));
                dt.Columns.Add("Column2", typeof(string));
                int i = 0;
                foreach (string s in richTextBox1.Lines)
                {
                    str = s.Split(' ');
                    //dt.Rows.Add(str);
                    dt.Rows.Add();
                    dt.Rows[i][0] = str[1];
                    dt.Rows[i][1] = str[2];
                    if (i < richTextBox1.Lines.Length - 1)
                    {
                        i++;
                    }
                }
    
                dataGridView1.DataSource = dt;
            }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by xBehemoth Monday, December 18, 2017 9:17 AM
    Monday, December 18, 2017 6:32 AM
  • Hi Marin,

    You can traverse each of the lines in RichTextBox, then separated by a space character and stored in a dataTable:

            private void button1_Click(object sender, EventArgs e)
            {
                string[] str = new string[] { };
                DataTable dt = new DataTable();
                dt.Columns.Add("Column1", typeof(string));
                dt.Columns.Add("Column2", typeof(string));
                int i = 0;
                foreach (string s in richTextBox1.Lines)
                {
                    str = s.Split(' ');
                    //dt.Rows.Add(str);
                    dt.Rows.Add();
                    dt.Rows[i][0] = str[1];
                    dt.Rows[i][1] = str[2];
                    if (i < richTextBox1.Lines.Length - 1)
                    {
                        i++;
                    }
                }
    
                dataGridView1.DataSource = dt;
            }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    I get this error.. 
    Monday, December 18, 2017 9:35 AM
  • Hi,

    Make sure the cursor does not move to the next line:

    If move to the next line, you need to change the code to:

    foreach (string s in richTextBox1.Lines) {

    //... dt.Rows[i][1] = str[2]; if (i < richTextBox1.Lines.Length - 2) { i++; } }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Monday, December 18, 2017 9:50 AM
    Monday, December 18, 2017 9:49 AM
  • The cursor is at the beginning of the line..
    Monday, December 18, 2017 10:42 AM
  • when i write 

     dt.Rows[i][0] = str[0];

    i get "VA" in the first column when i try 
     dt.Rows[i][0] = str[1];

    to get the "VV_IN" i get the error..

    Monday, December 18, 2017 11:47 AM
  • Hi,

    Please try to set some break points to debug the code if you have some mistakes when you run the project, to determine why this error occurred, Learning to debug code is a process of growth.

    Do you mean that you want to get three columns?

    If so, please do it as this:

            private void button1_Click(object sender, EventArgs e)
            {
                string[] str = new string[] { };
                DataTable dt = new DataTable();
                dt.Columns.Add("Column1", typeof(string));
                dt.Columns.Add("Column2", typeof(string));
                dt.Columns.Add("Column3", typeof(string));//add three columns
                int i = 0;
                foreach (string s in richTextBox1.Lines)
                {
                    str = s.Split(' ');
                    //dt.Rows.Add(str);
                    dt.Rows.Add();
                    dt.Rows[i][0] = str[0];//set the corresponding position
                    dt.Rows[i][1] = str[1];
                    dt.Rows[i][2] = str[2];
                    if (i < richTextBox1.Lines.Length - 1)//or -2
                    {
                        i++;
                    }
                }
    
                dataGridView1.DataSource = dt;
            }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 19, 2017 2:31 AM