在datatable中写入字符串中的内容 [英] Write content from string in datatable

查看:73
本文介绍了在datatable中写入字符串中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有信息保存在字符串中现在我想从字符串中提取该信息并将其放在表中我的代码是:

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:

我想要有2列的表格。

例如:

变量           价值

Variable            Value

VV_IN              11.83

VV_IN               11.83

推荐答案

您输入的示例与您想要的数据不符。例如,前两行没有任何数值。您也没有指定格式是否一致 - 字符串字符串编号。假设数据是一致的,那么你首先需要
来分割数据,然后抓住你想要的东西。

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;
}


这篇关于在datatable中写入字符串中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆