2012-06-25 67 views
1

Visual Studio 2010中的C#代碼:Visual Studio 2010的C#DataReceived似乎沒有觸發

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace YamanPonics 
{ 
    public partial class Form1 : Form 
    { 
     string RxString; 

     //Default SerialPortStatus equals TRUE when first starting up 
     Boolean serialPortDisconnected = true; 

     public Form1() 
     { 
      InitializeComponent(); 

      //Add available Serial COM ports to combobox 
      foreach (string ports in System.IO.Ports.SerialPort.GetPortNames()) 
      { 
       //MessageBox.Show("Serial port avialible" + " " + ports); 
       comPortCmbBox.Items.Add(ports); 
      } 
     } 

     private void DisplayText(object sender, EventArgs e) 
     { 
      serialMsgViewerRchTxt.AppendText(RxString); 
      MessageBox.Show("Displayed Serial Text!"); 
     } 

     private void serialPort1_DataReceived (object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
     { 
      RxString = serialPort1.ReadExisting(); 
      this.Invoke(new EventHandler(DisplayText)); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      //If SerialPort1 IsOpen 
      if (serialPort1.IsOpen) 
      { 
       //Close SerialPort1 communication 
       serialPort1.Close(); 
      } 
     } 


     private void connectDisconnectBtn_Click(object sender, EventArgs e) 
     { 
      //Set arduinoComPort value to COM Port value 
      string arduinoComPort = comPortCmbBox.Text; 

      //if SerialPortStatus boolean equals FALSE then 
      if (serialPortDisconnected && (arduinoComPort != "")) 
      { 

       //Set serialPort1 BaudRate value to default value of 38400(required for atlas-scientific sensors) 
       serialPort1.BaudRate = 38400; 

       //Set serialPort1 Read and Write timeout values 
       serialPort1.ReadTimeout = 250; 
       serialPort1.WriteTimeout = 250; 

       //Set serialPort1 DataBits value 
       serialPort1.DataBits = 8; 

       //Open serialPort1 communication 
       serialPort1.Open(); 

       //Change connectDisconnectBtn text to Disconnect 
       connectDisconnectBtn.Text = "Disconnect"; 

       //Set serialPortDisconnected to FALSE 
       serialPortDisconnected = false; 

      } 
      else //if SerialPortStatus bollean equals TRUE 
      { 
       //Close SerialPort1 communication 
       serialPort1.Close(); 

       //Set connectDisconnectBtn text to Connect 
       connectDisconnectBtn.Text = "Connect"; 

       //Set serialPortDisconnected to TRUE 
       serialPortDisconnected = true; 
      } 
     } 

     private void SendBtn_Click(object sender, EventArgs e) 
     { 
      //if serialPort1 IsOpen then 
      if (serialPort1.IsOpen) 
      { 
       serialPort1.Write("{ph}"); 
      } 
     } 
    } 
} 

該代碼是一個簡單的串行連接/斷開和發送/接收申請。連接,斷開併發送正常工作。當我的arduino收到命令時,它會發回一個響應。我的2010 C#應用程序沒有收到richtextbox中的響應,不明白爲什麼。當我使用另一個串行終端程序時,我可以收到響應,所以我確實知道數據正在發送。我沒有做什麼來成功地獲得迴應?

回答

2

你確定serialPort1.DataReceived事件連接正確嗎?我看到你有處理程序方法,但我沒有看到你訂閱該事件。

你需要有這樣的話:

serialPort1.DataReceived += serialPort1_DataReceived; 
+0

謝謝!那就是訣竅。我是C#的新手。 – dottedquad