C#:在SerialPort.Open超时? [英] C#: Timeout on SerialPort.Open?

查看:611
本文介绍了C#:在SerialPort.Open超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以打开端口和匹配所接收的数据,从而检测该相关设备发送数据的端口的自动检测线程。现在,有一些端口,其中SerialPort.Open只是挂线程〜30秒。我怎样才能设置超时的SerialPort.Open功能

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?

推荐答案

从的 MSDN 结果
只有一个打开的连接可以为每个对象的SerialPort存在。

From MSDN
Only one open connection can exist per SerialPort object.

适用于任何应用的最佳做法是试图调用Open方法之前调用Close方法之后等待一定的时间,因为端口可能不会被立即关闭

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.

这篇关于C#:在SerialPort.Open超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆