简体   繁体   English

将人们在 C# 中的 DataTable 中输入的值发送到微控制器

[英]Sending the value people input in DataTable in C# to microcontroller

I'm trying to send the value which people input from DataTable to microcontroller, but when I check again by TeraTerm the value in microcontr., the result I get is wrong.我正在尝试将人们从 DataTable 输入的值发送到微控制器,但是当我通过 TeraTerm 再次检查微控制器中的值时,我得到的结果是错误的。

The image:图片:在此处输入图像描述

Here my code for grid init:这是我的网格初始化代码:

private void Form1_Load(object sender, EventArgs e)
{
    //Data table
    DataTable table = new DataTable();

    //Column
    table.Columns.Add("TPS");
    table.Columns.Add("500", typeof(float));
    table.Columns.Add("750", typeof(float));
    //Row
    table.Rows.Add("100%");
    table.Rows.Add("95%");
    table.Rows.Add("90%");

    //Data Grid View
    dtGridView.DataSource = table;

    //Set width
    dtGridView.Columns[0].Width = 40;
    dtGridView.Columns[1].Width = 40;
    dtGridView.Columns[2].Width = 40;

}

And the sending:和发送:

private void btnSend_Click(object sender, EventArgs e)
{
    try
    {
        for (int i = 0; i < dtGridView.Rows.Count; i++)
            for (int j = 0; j < dtGridView.Columns.Count; j++)
            {
                object o = dtGridView.Rows[i].Cells[j].Value;

                if (P.IsOpen == true)
                {
                    P.WriteLine(o.ToString());
                    MessageBox.Show("Write succesfully!");
                    P.Close();
                }
            }
    }
    catch(Exception ex)
    {
        MessageBox.Show("Unable to write to COM port");
    }
}

When you run this loops:当您运行此循环时:

for (int i = 0; i < dtGridView.Rows.Count; i++)
    for (int j = 0; j < dtGridView.Columns.Count; j++)
    {
        object o = dtGridView.Rows[i].Cells[j].Value;
        if (P.IsOpen == true)
        {
            P.WriteLine(o.ToString());
            MessageBox.Show("Write succesfully!");
            P.Close(); // <<<<<<<<<<<<<<<<<<< this close the connection immediately after 1st val.
        }
    }

Instead try to close it after the loop.而是尝试在循环后关闭它。

for (int i = 0; i < dtGridView.Rows.Count; i++)
    for (int j = 0; j < dtGridView.Columns.Count; j++)
    {
        object o = dtGridView.Rows[i].Cells[j].Value;
        if (P.IsOpen == true)
        {
            P.WriteLine(o.ToString());
            MessageBox.Show("Write succesfully!");
        }
    }

if (P.IsOpen == true)
   P.Close();             // like this

The issue if you're calling P.Close() after the first iteration as others have noted.正如其他人所指出的那样,如果您在第一次迭代后调用P.Close()会出现问题。 Generally with serial communications after connect I would just keep the connection open until the user explicitly disconnected.通常在连接后进行串行通信,我只会保持连接打开,直到用户明确断开连接。 I would recommend removing P.Close();我建议删除P.Close(); from the loop and instead adding a disconnect button that calls P.Close();从循环中取而代之的是添加一个调用P.Close(); . .

This should simplify your code as well as the send button will only be concerned with writing data and not also managing the state of the serial communications.这应该简化您的代码,并且发送按钮将只关注写入数据,而不是管理串行通信的 state。

Tks everyone,i sent value successfully.谢谢大家,我成功发送了价值。 But if i do as the way i did, it will send each value of the table.但是如果我按照我的方式做,它会发送表格的每个值。 Now i want to click the button send, all the values i input will send in one time.现在我想点击按钮发送,我输入的所有值都会一次性发送。 Is the anyway to do it?无论如何都要这样做吗?

Here the image attached: enter image description here这里附上图片:在此处输入图片描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM