none
Write content from string in datatable RRS feed

  • Question

  • Hi, i have information saved in a string now i want to extract that info from the string and put it in a table my code is:

    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 WindowsFormsApplication8
    {
        public partial class Form1 : Form
        {
            SerialPort comPort = new SerialPort();
            private string buffer;
    
            public Form1()
            {
                InitializeComponent();
                SetDefault();
                comPort.Open();
                System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
                timer.Interval = 200;
                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 GetVal()
            {
                if (!(comPort.IsOpen == true)) comPort.Open();
                if (comPort.IsOpen == true)
                {
                    comPort.WriteLine("VA VV_IN");
                    comPort.WriteLine("VA VV_OUT");
                    comPort.WriteLine("VA VV_ACCU");
                    comPort.WriteLine("VA VV_UPS");
                    comPort.WriteLine("VA VV_CELL1");
                    comPort.WriteLine("VA VV_CELL2");
                    comPort.WriteLine("VA VV_CELL3");
                }
                Thread.Sleep(200);
                buffer = comPort.ReadExisting();
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                
            }
            private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
            {
                GetVal();
    
            }

    now i want to put the values i read form serial port in a table. The information i get looks like this:

    I want table with 2 columns.

    example:

    Variable            Value

    VV_IN               11.83

    Monday, December 18, 2017 1:07 PM

All replies

  • The examples you put in don't line up with the data you want out. For example the first 2 lines don't have any numeric values. You also didn't specify if the format is consistent - string string number. Assuming the data is consistent then you'll first need to split the data, then grab what you want.

    //Assuming the following table structure
    var table = new DataTable();
    table.Columns.Add("Variable", typeof(string));
    table.Columns.Add("Value", typeof(double));
    
    //Given a single line, assume format is:
    // string string number
    
    DataRow ParseLine ( DataTable table, string line )
    {
       var tokens = line.Split(' ');   
    
       //We're looking for an exact line match here, if you want
       //to support lines without numbers then you'll need to be
       //less precise
       if (tokens.Length < 3)
          return null;
    
       var row = table.NewRow();
       row["Variable"] = token[1];
       
       //Not doing any sort of error checking here, you should
       row["Value"] = Double.Parse(token[2]);
    
       table.Rows.Add(row);
       return row;
    }
    


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Fei HuModerator Wednesday, December 20, 2017 10:06 AM
    Monday, December 18, 2017 2:50 PM
    Moderator