2012-10-03 69 views
0

我試圖使用[SerialPort]類運行串行通信。我做了一個簡單的控制檯應用程序項目,我使用HyperTerminal來測試這個類。SerialPort不讀取消息

這是我的計劃:

class Program 
{ 
    private static bool _continue = true; 
    private static SerialPort port; 

    static void Main(string[] args) 
    { 
     try 
     { 
      port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One); 
      port.ReadTimeout = port.WriteTimeout = 5000; 
      port.Open(); 

      Thread thread = new Thread(Read); 
      thread.Start(); 

      while (_continue) 
      { 
       string message = Console.ReadLine(); 

       if (message.Equals("quit")) 
        _continue = false; 
       else 
        port.Write(message); 
      } 

      thread.Join(); 
      port.Close(); 
     } 
     catch (Exception ex) 
     { } 
    } 

    private static void Read() 
    { 
     while (_continue) 
     { 
      try 
      { 
       string message = port.ReadLine(); 
       Console.WriteLine(message); 
      } 
      catch (TimeoutException) { } 
     } 
    } 
} 

The problem is如下:當我寫一本線(在控制檯),一個可以在超級終端界面看到我寫的,但是當我使用超級終端不寫一行消息是由我的程序閱讀,thorws總是TimeoutException

爲什麼?
我該如何解決這個問題?
謝謝。

+0

您忘記設置Handshake屬性。 –

回答

1

嘗試Port.Read以防Port.ReadLine正在等待換行!