[英]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
。
為什么?
我怎么解決這個問題?
謝謝。
如何使用SerialPort.DataRecieved事件?
http://msdn.microsoft.com/zh-CN/library/system.io.ports.serialport.datareceived.aspx
如果Port.ReadLine正在等待換行,請嘗試Port.Read!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.