Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SerialPort不读取消息_C#_Serial Port_Serial Communication - Fatal编程技术网

C# SerialPort不读取消息

C# SerialPort不读取消息,c#,serial-port,serial-communication,C#,Serial Port,Serial Communication,我正在尝试使用[SerialPort]类运行串行通信。我制作了一个简单的控制台应用程序项目,在其中使用 这是我的节目: class Program { private static bool _continue = true; private static SerialPort port; static void Main(string[] args) { try { port = new SerialPo

我正在尝试使用[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) { }
        }
    }
}
问题在于
以下几点:当我写一行(在控制台中)时,a可以在超级终端GUI中看到我写的内容,但当我使用超级终端写一行时,我的程序不会读取任何消息,这些消息始终是
TimeoutException

为什么?
我如何解决这个问题?

谢谢。

尝试Port.Read,以防Port.ReadLine正在等待新行

尝试Port.Read,以防Port.ReadLine正在等待新行

如何使用SerialPort.DataReceived事件


如何使用SerialPort.DataReceived事件


您忘记设置握手属性。您忘记设置握手属性。