serialport没有读取消息
-
13-12-2019 - |
题
我正在尝试使用[serialport] class进行串行通信。我做了一个简单的控制台应用程序项目,我使用 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
以下内容:当我写一行(在控制台中)a可以在超级终端gui中看到我写的东西,但是当我使用hyperterminal编写一条线时,我的程序读取索华总是一个TimeoutException
。
为什么?
如何解决这个问题?
谢谢。
解决方案
try port.read以防万一端口.readline正在等待新的线路!
其他提示
利用SerialPort.DatarecieCaleFife活动?
http://msdn.microsoft。com / en-us / library / system.io.ports.serialport.datareceived.aspx
不隶属于 StackOverflow