2014-12-06 89 views
0

我想從我的傳感器的數據讀取從串口,我可以讀取端口的名稱,但我無法從它們接收數據...我在#C和串口編碼新的,以前〜無法從串口讀取數據(在C#中)

感謝和這裏是我的嘗試:

using System; 
using System.ComponentModel; 
using System.Collections; 
using System.Diagnostics; 
using System.IO; 
using System.IO.Ports; 
using System.Text; 
using System.Security; 
using System.Security.Permissions; 
using Microsoft.Win32; 
using System.Runtime.InteropServices; 
using System.Runtime.Versioning; 
using System.Collections.Generic; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     //----------------------------------------------------- GLOBAL PARAMETER ------------------------------------------------------ 
     //---serial port objects 
     static private List <SerialPort> _serialPort = new List<SerialPort>(); 
     //---serial port read buffer 
     static private List <byte[]> _buffer = new List<byte[]>(); 
     //---length of the port list 
     static private int _length; 

     //--------------------------------------------------- INIT FUNCTIONS ---------------------------------------------------------- 
     //---init main function 
     static private void initFunction(){ 
      //create the serial port objs 
      createPortObj(); 
      //create the buffers 
      createBuffer(); 
      //init a clock 
      //init the ports' name 
      return; 
     } 

     //---Set the port objs 
     static private void setPortOption(int i) { 
      SerialPort tPort = _serialPort[i]; 

      //set options 
      tPort.BaudRate = 9600; 
      tPort.Parity = Parity.None; 
      tPort.StopBits = StopBits.One; 
      tPort.DataBits = 8; 
      tPort.Handshake = Handshake.None; 

      //set the datareceived function 
      //tPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); 
      tPort.DataReceived += DataReceivedHandler; 

      tPort.Open(); 

      return; 
     } 

     //---Create the port objs 
     static private void createPortObj(){ 
      // Get a list of serial port names. 
      string[] _names = SerialPort.GetPortNames(); 
      _length = _names.Length; 

      //create the objs 
      for(int i=0; i < _length; i++) 
      { 
       Console.WriteLine(_names[i]); 
       //create the port objects 
       _serialPort.Add(new SerialPort(_names[i])); 
       //init the port object 
       setPortOption(i); 
      } 

      return; 
     } 

     //---Create the buffer 
     static private void createBuffer() { 
      //create buffer for each port obj 
      for (int i = 0; i < _length; i++){ 
       byte[] bufferOne = new Byte[_serialPort[i].BytesToRead]; 
       _buffer.Add(bufferOne); 
      } 
      return; 
     } 

     //-------------------------------------------------- FEATURED FUNCTION ---------------------------------------------------- 
     //---Transmit the code 
     //---Data received handler 
     static public void DataReceivedHandler(Object sender, SerialDataReceivedEventArgs e){ 
      SerialPort sp = (SerialPort)sender; 
      string indata = sp.ReadExisting(); 
      Console.WriteLine("Data Received:"); 
      Console.Write(indata); 


      //Receive the data from serial ports 
      for (int i=0; i<_length; i++){ 
       int temp; 
       temp = _serialPort[i].Read(_buffer[i], 100, _buffer[i].Length); 
       Console.WriteLine(temp); 

      } 

      return; 
     } 


     //-------------------------------------------------- RETRIVE FUNCTION ---------------------------------------------------- 
     //---Retrive the source 
     static private void retriveFunction(){ 
      //close the serial ports 
      foreach (SerialPort port in _serialPort){ 
       port.Close(); 
      } 

      return; 
     } 

     //-------------------------------------------------- MAIN ----------------------------------------------------------------- 
     //---main function 
     static void Main(string[] args){ 
      //init function, and open the 
      initFunction(); 

      int[] num = new int[_length]; 
      for (int i = 0; i < _length; i++){ 
       num[i] = _serialPort[i].Read(_buffer[i], 0, _buffer[i].Length); 
      } 

      //check the result of the read function 
      Console.Write(num[0]); 

      //on serial port receiving 

      //retrive the source 
      retriveFunction(); 

      Console.ReadLine(); 
     } 
    } 
} 

再次感謝〜

+0

調試。調試。調試。 – 2014-12-06 06:20:02

+0

您是否能夠通過串口獲取數據,而不是通過C#應用程序?從一個串口開始,確保你能夠從一個接收數據,讀取的結果是0,你沒有把我的變種.. – ilansch 2014-12-06 07:13:55

+0

@ilansch我敢肯定,數據可以被其他應用程序接收,如arduino客戶。此外,我不認爲這是陣列的事情導致問題 – tristan 2014-12-06 08:44:22

回答

0

我覺得你的代碼是不是等待數據receive.Before你的傳感器發回數據,你的代碼完成,程序結束,你應該處理數據w線程或DataReceived事件中。

+0

哦,你的意思是「SerialDataReceivedEventHandler」函數?我已經將它添加到上面的代碼中,奇怪的是,事件偵聽器函數沒有被調用(我已經設置了中斷,只是看到程序忽略了代碼) – tristan 2014-12-06 08:55:02