2013-03-03 51 views
0

嗨我想從我的Arduino讀取串行輸入,但我沒有運氣。我相信我正確地打開和關閉連接,但似乎沒有太大的成功!C#代碼無法讀取來自Arduino的串行輸入

我確定Arduino正在輸出數據,因爲我可以在串行終端中看到它。

我的C#程序代碼是下面,我想知道是否有人能發現我可能已經錯過了任何錯誤。

而且這是串行數據的一個例子,我應該得到「12.2,1111,332,233」

namespace FridgeProtectionDeviceMonitor 
{ 
    public partial class Online_mode : Form 
    { 

     public Online_mode() 
     { 
      InitializeComponent(); 
     } 

     private void Online_mode_Load(object sender, EventArgs e) 
     { 
      cmbPortSelection.DataSource = SerialPort.GetPortNames(); 
      cmbChartSelection.SelectedIndex = 0; 
     } 

     string x = ""; 
     SerialPort port; 

     private void btnFindPorts_Click(object sender, EventArgs e) 
     { 
      var ports = SerialPort.GetPortNames(); 
      cmbPortSelection.DataSource = ports; 
     } 

     private void btnOpenPort_Click(object sender, EventArgs e) 
     { 
      if (cmbPortSelection.SelectedIndex > -1) 
      { 
       port = new SerialPort(cmbPortSelection.Text); 
       try 
       { 
        if (!port.IsOpen) 
        { 
         port.BaudRate = 9600; 
         port.Open(); 
        } 
       } 
       catch (Exception) 
       { 
        MessageBox.Show("Serial connection request denied: Port is in use!"); 
       } 
      } 
      else 
      { 
       MessageBox.Show("Serial connection request denied: No port selected!"); 
      } 
     } 

     private void btnClosePort_Click(object sender, EventArgs ex) 
     { 

      try 
      { 
       port.Close(); 

      } 
      catch (Exception) 
      { 
       MessageBox.Show("Serial close connection request denied: ", ex.ToString()); 
      } 
     } 

     private void update(object sender, EventArgs e) 
     { 
      txtSaveLocation.Text = x; 
     } 

     private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
     { 
      x = port.ReadLine().ToString(); 
      MessageBox.Show(x); 
      this.Invoke(new EventHandler(update)); 

     } 
    } 
} 

回答

0

,我只是工作在同樣的事情,你,你必須,如果你對其進行修改想用於你的目的,但這裏是我使用的代碼。唯一的問題是,對我來說,我需要它快速閱讀,因爲它是一個速度計,它有點滯後(任何人都知道爲什麼?)但無論如何,這裏是我的代碼適合我。

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 WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     public void Form1_Load(object sender, EventArgs e) 
     { 
      try 
      { 
       if (arduinoCom.IsOpen == false) 
       { 
        arduinoCom.Open(); 
       } 
      } 

      catch 
      { 
       MessageBox.Show("Serial Error: Is your serial plugged in?"); 
      } 

     } 

     private void refresh_Tick(object sender, EventArgs e) 
     {    
      string speedReading = arduinoCom.ReadLine(); 
      speed.Text = speedReading; 
     } 

     private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      arduinoCom.Close(); 
     } 

    } 
} 
0

我有一個USB鏈接到連接到PIC微控制器的的XBee無線網橋系列轉換器和我發現,運行串行端口在一個單獨的線程讀取功能,提高通信速率東西慢性!

readline會阻止代碼,直到接收到換行符爲止,因此會阻塞您的主線程。在我將它移動到另一個線程之前,我的GUI變得無法響應等。 順便說一下,我正在使用ReadByte()。

我不斷地從後臺工作器的串行緩衝區中讀取字節並將它們存儲在線程安全隊列中。然後檢查隊列中的通信起始位並從中分析數據包。使用手動重置事件我正在同步所有內容。

在我的經驗,port_datareceived事件是一個很好的補充SerialPort類,但不幸的是它是一個用於快速通信相對無用。我運行在12900波特。

如果你想要我可以提供的代碼片段。

問候, 皮特

附:我正在使用帶多通道軟件的ATS5004D 4通道差分示波器。多通道軟件可以通過內置的串行分析器模塊將電壓信號分析爲串行數據。這樣你就可以看到串口通信線上實際正在討論的內容了!切割出所有BS其它串行終端加...

0

@邁克爾b口會通過看什麼實際的通信是做即範圍TX接收線路,看看有什麼Arduino板被撲滅啓動。我對Arduino只有很少的經驗,但是它的前身PICAXE系統有一個調試功能,它可以減慢堆內的一切,是這樣的嗎?

你想每秒鐘更新一次速度? SAE J1939汽車串行通信標準規定RPM只需要每100ms更新一次。人眼通常看不到/反應速度超過300毫秒。 9600波特給你一個約8毫比特每毫秒的字節。

個人而言,我不會用計時器來獲取更新,而是從主一個單獨的線程做的一切,並讓它運行的快/慢就是了。

這個線程告訴您大致有我建立了我的通信System.IO.IOException: A device attached to the system is not functioning C# .NET 4.0

希望它能幫助, 皮特