簡體   English   中英

SerialPort不讀取消息

[英]SerialPort does not read message

我正在嘗試使用[SerialPort]類運行串行通信。 我做了一個簡單的控制台應用程序項目,在其中使用HyperTerminal測試了此類。

這是我的程序:

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如下:當我在控制台中寫一行時,可以在超級終端GUI中看到我寫了什么,但是當我使用超級終端寫一行時,我的程序沒有讀取總是總是TimeoutException

為什么?
我怎么解決這個問題?
謝謝。

如果Port.ReadLine正在等待換行,請嘗試Port.Read!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM