2014-08-31 190 views
0

我已經做了一個簡單的項目,從串口(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; 
} 

回答

0

這就是你的答案:

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