C#:SerialPort.Open 超时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1696238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: Timeout on SerialPort.Open?
提问by gdario
I have an autodetect thread that tries to open the ports in order and match the received data, thus detecting the port where the relevant device sends the data. Now, there are some ports where the SerialPort.Open simply hangs the thread for ~30 secs. How can I set a timeout on the SerialPort.Open function?
我有一个自动检测线程,它尝试按顺序打开端口并匹配接收到的数据,从而检测相关设备发送数据的端口。现在,有一些端口 SerialPort.Open 只是将线程挂起约 30 秒。如何在 SerialPort.Open 函数上设置超时?
回答by Nathan Campos
Add this in your code:
在您的代码中添加:
commPort = new SerialPort();
commPort.ReadTimeout = 1000000;
commPort.WriteTimeout = 1000000;
And I suggest you to see SerialPort.Open Method
我建议你看看SerialPort.Open 方法
回答by Tzury Bar Yochay
If I understood you correctly, you wish to read data from the serial port even after timeout occurred.
如果我理解正确,即使发生超时,您也希望从串行端口读取数据。
If so, then you should catch the TimeoutException and continue your loop. e.g. MSDN CODE
如果是这样,那么您应该捕获 TimeoutException 并继续您的循环。例如MSDN 代码
public static void Read()
{
while (_continue)
{
try
{
string message = _serialPort.ReadLine();
Console.WriteLine(message);
}
catch (TimeoutException) { }
}
}
回答by SwDevMan81
From MSDN
Only one open connection can exist per SerialPort object.
从MSDN
每个 SerialPort 对象只能存在一个打开的连接。
The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.
任何应用程序的最佳做法是在调用 Close 方法后等待一段时间,然后再尝试调用 Open 方法,因为端口可能不会立即关闭。
When you call Close(), this worker thread needs time to spin down and exit. The amount of time needed is not specified and you can't verify that it was done. All you can do is wait at least one second before you call Open() again.
当您调用 Close() 时,此工作线程需要时间来减速并退出。所需的时间量未指定,您无法验证它是否已完成。您所能做的就是等待至少一秒钟,然后再次调用 Open()。
回答by Qing Xie
I encountered the same problem and I hope my solution can help you.
我遇到了同样的问题,希望我的解决方案可以帮助你。
You can detect the Serial Ports in a separate thread, which will be aborted in 500 ms.
您可以在单独的线程中检测串行端口,该线程将在 500 毫秒内中止。
// the Serial Port detection routine
private void testSerialPort(object obj)
{
if (! (obj is string) )
return;
string spName = obj as string;
SerialPort sp = new SerialPort(spName);
try
{
sp.Open();
}
catch (Exception)
{
// users don't want to experience this
return;
}
if (sp.IsOpen)
{
if ( You can recieve the data you neeed)
{
isSerialPortValid = true;
}
}
sp.Close();
}
// validity of serial port
private bool isSerialPortValid;
// the callback function of button checks the serial ports
private void btCheck(object sender, RoutedEventArgs e)
{
foreach (string s in SerialPort.GetPortNames())
{
isSpValid = false;
Thread t = new Thread(new ParameterizedThreadStart(testSerialPort));
t.Start(s);
Thread.Sleep(500); // wait and trink a tee for 500 ms
t.Abort();
// check wether the port was successfully opened
if (isSpValid)
{
textBlock1.Text = "Serial Port " + s + " is OK !";
}
else
{
textBlock1.Text = "Serial Port " + s + " retards !";
}
}
}
}
Possible improvements could be added into the solution. You can use multi-Thread to accelerate the process and use ProgressBar
to display the progress clearly.
可能的改进可以添加到解决方案中。您可以使用多线程来加速进程并使用它ProgressBar
来清晰地显示进度。