2012-09-30 35 views
2

我试图做一个应用程序,读取从Arduino传出的信号,但我不能让它在C#Windows Forms,只在控制台中工作。我的C#Windows窗体代码错了吗?我在调试时没有遇到任何错误,但这并不意味着我没有忘记一些东西。C#读Arduino

这里是我的代码:

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; 
using System.IO.Ports; 
using System.Threading; 

namespace CommunicateWithArduino 
{ 
    public partial class Form1 : Form 
    { 
     public static System.IO.Ports.SerialPort port; 

     delegate void SetTextCallback(string text); 

     private BackgroundWorker hardWorker; 

     private Thread readThread = null; 


     public Form1() 
     { 
      InitializeComponent(); 

      hardWorker = new BackgroundWorker(); 
      sendBtn.Enabled = false; 
     } 

     private void btnConnect_Click(object sender, EventArgs e) 
     { 
      System.ComponentModel.IContainer components = 
       new System.ComponentModel.Container(); 
      port = new System.IO.Ports.SerialPort(components); 
      port.PortName = comPort.SelectedItem.ToString(); 
      port.BaudRate = Int32.Parse(baudRate.SelectedItem.ToString()); 
      port.DtrEnable = true; 
      port.ReadTimeout = 5000; 
      port.WriteTimeout = 500; 
      port.Open(); 

      readThread = new Thread(new ThreadStart(this.Read)); 
      readThread.Start(); 
      this.hardWorker.RunWorkerAsync(); 

      btnConnect.Text = "<Connected>"; 

      btnConnect.Enabled = false; 
      comPort.Enabled = false; 
      sendBtn.Enabled = true; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      foreach (string s in SerialPort.GetPortNames()) 
      { 
       comPort.Items.Add(s); 
      } 
      if (comPort.Items.Count > 0) 
       comPort.SelectedIndex = comPort.Items.Count-1; 
      else 
       comPort.SelectedIndex = 0; 

      baudRate.Items.Add("2400"); 
      baudRate.Items.Add("4800"); 
      baudRate.Items.Add("9600"); 
      baudRate.Items.Add("14400"); 
      baudRate.Items.Add("19200"); 
      baudRate.Items.Add("28800"); 
      baudRate.Items.Add("38400"); 
      baudRate.Items.Add("57600"); 
      baudRate.Items.Add("115200"); 

      baudRate.SelectedIndex = 2; 
     } 

     private void sendBtn_Click(object sender, EventArgs e) 
     { 
      if (port.IsOpen) 
      { 
       port.Write(sendText.Text); 
      } 
     } 

     private void SetText(string text) 
     { 

      if (this.receiveText.InvokeRequired) 
      { 
       SetTextCallback d = new SetTextCallback(SetText); 
       this.Invoke(d, new object[] { text }); 
      } 
      else 
      { 
       this.receiveText.Text += "Text: "; 
       this.receiveText.Text += text; 
       this.receiveText.Text += Environment.NewLine; 
      } 
     } 

     public void Read() 
     { 
      while (port.IsOpen) 
      { 
       try 
       { 
        if (port.BytesToRead > 0) 
        { 
         string message = port.ReadLine(); 
         this.SetText(message); 
        } 
       } 
       catch (TimeoutException) { } 
      } 
     } 

     private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
     { 
      try 
      { 
       if(!(readThread == null)) 
        readThread.Abort(); 
      } 
      catch (NullReferenceException) 
      { 
      } 

      try 
      { 
       port.Close(); 
      } 
      catch (NullReferenceException) 
      { 
      } 
     } 
    } 
} 
+0

什么不工作?你是否收到有关串口的数据,或者你无法从线程获取数据到gui? – erikH

+0

如果你不知道,你可以:1,将串行数据记录到一个文件中,2,创建一个线程,每5秒左右创建一个调用“SetText”的线程。 – erikH

+0

我不能在C#winform应用程序的串行端口上获取数据。但在Arduino程序串行监视器我得到的数据,我可以在我的C#控制台应用程序的数据,所以没有错误的Arduino。但我无法在我的winform中获取数据,这让我疯狂的cus我不认为我发布的winform代码有任何问题。我写了应用程序,所以我可以发送数据并在文本框中收回数据。 – user1680862

回答

1

默认情况下,ReadLine方法将阻塞,直到接收到一条线。你的Arduino程序是否发送一行?运行程序时你关闭了Arduino串口监视器程序吗?

我会更改为port.ReadChar,直到您确认您正在接收字符。

+0

是的arduino它发送一条线。我正在使用arduino程序员应用程序附带的示例草图“串行调用和响应”。当我运行我的程序时,我无法运行Arduino串行监视器程序。我需要关闭一个来运行另一个。我不明白是什么错,因为它在C#consloe和arduino串行监视器工作,而不是在C#winform我贴的代码。 – user1680862

+0

你可以debug.print你收到的任何字符,以验证程序正确配置端口? – Jeff

+0

我刚才看到的串行调用和响应示例并未发送一行。它发送一个字符“A”(Serial.print('A');)并等待你的回应。 – Jeff