C#从串口arduino读取

时间:2014-08-31 23:49:29

标签: c# serial-port arduino

我做了一个简单的项目,从串口(arduino)读取,我接收数据,我可以把这些数据放在文本框,richtex但我不能把它们放在数据网格中,实际上我需要把这些数据放在MSSQL中表也​​可以在数据网格中显示。这是我尝试过的代码:

void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
        {
            if (this.InvokeRequired)
            {
                // Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
                this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
                return;
            }

            int maxTextLength = 1000; // maximum text length in text box
            if (tbData.TextLength > maxTextLength)
                tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength);

            // This application is connected to a GPS sending ASCCI characters, so data is converted to text
            string str = Encoding.ASCII.GetString(e.Data);
            tbData.AppendText(str);
            tbData.ScrollToCaret();

             richD.AppendText(str);
                richD.ScrollToCaret();

             dataGridView1.DataSource = str;

            }

你能帮我填写datagridbiew吗?

这是我获得e.Data的地方:

public class SerialDataEventArgs : EventArgs
{
    public SerialDataEventArgs(byte[] dataInByteArray)
    {
        Data = dataInByteArray;
    }

    /// <summary>
    /// Byte array containing data from serial port
    /// </summary>
    public byte[] Data;
}

1 个答案:

答案 0 :(得分:0)

这是你的答案:

string[] array = imput.split();

当你在str处读取数据时,你在str中有错误是一个数据而不是字符串。

更改此代码:

if (tbData.TextLength > maxTextLength)
                tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength);

            // This application is connected to a GPS sending ASCCI characters, so data is converted to text
            string str = Encoding.ASCII.GetString(e.Data);