C# SerialPort不读取消息
我正在尝试使用[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
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事件
您忘记设置握手属性。您忘记设置握手属性。