C#从串口读取arduino
问题描述:
我已经做了一个简单的项目,从串口(arduino)读取,我收到的数据,我可以把这些数据放入文本框,richtex,但我不能把它们放在数据网格中,实际上我需要把这些数据放在MSSQL表中,但也显示在数据网格中。这里是我试过的代码:C#从串口读取arduino
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;
}
答
这就是你的答案:
string[] array = imput.split();
你其实海峡有错误,当您在STR recive数据是位数据不是字符串。
改变这样的代码:
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);