[英]C# read from serial port arduino
I have done a simple project that reads from serial port (arduino) and I receive the data and I can put those data in textbox, richtex but I cant put them in data grid in fact I need to put those data in MSSQL table but also display in data grid. 我做了一个简单的项目,该项目从串行端口(arduino)读取,我接收到数据,可以将这些数据放入文本框richtex中,但实际上我无法将其放入数据网格中,我需要将这些数据放入MSSQL表中,显示在数据网格中。 here is the code that I have tried: 这是我尝试过的代码:
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;
}
Can you help me fill the datagridbiew? 您能帮我填一下datagridbiew吗?
This is where I get the e.Data: 这是我获取e.Data的地方:
public class SerialDataEventArgs : EventArgs
{
public SerialDataEventArgs(byte[] dataInByteArray)
{
Data = dataInByteArray;
}
/// <summary>
/// Byte array containing data from serial port
/// </summary>
public byte[] Data;
}
this is your answer: 这是你的答案:
string[] array = imput.split();
you have error at str in fact when you recive data at str is a bit data not string. 实际上,当您在str接收数据是位数据而不是字符串时,您在str上就会出错。
change this code: 更改此代码:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.