windows 如果没有使用 CloseHandle 正确关闭,重新打开串口会失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2948428/
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
Reopening serial port fails if not closed properly with CloseHandle
提问by superg
I am working with USB device on Windows that is seen as a virtual serial port. I can communicate with the device using CreateFile and ReadFile functions, but in some cases my application does not call CloseHandle (when my application in development crashes). After that all calls to CreateFile fail (ERROR_ACCESS_DENIED), and the only solution is to log in to my computer again. Is there any way to force closing the open handle (or reopening) programmatically?
我正在 Windows 上使用 USB 设备,该设备被视为虚拟串行端口。我可以使用 CreateFile 和 ReadFile 函数与设备通信,但在某些情况下,我的应用程序不会调用 CloseHandle(当我的应用程序在开发中崩溃时)。之后对 CreateFile 的所有调用都失败 (ERROR_ACCESS_DENIED),唯一的解决方案是再次登录我的计算机。有没有办法以编程方式强制关闭打开的句柄(或重新打开)?
回答by valdo
I agree with both previous posts.
我同意之前的两个帖子。
- This is not a normal situation.
- Unplugging the USB device usually helps.
- 这不是正常情况。
- 拔下 USB 设备通常会有所帮助。
This problem is related to the glitches in the FTDI driver, which is responsible for implementing a virtual COM port. On the other hand those "glitches" are related to various malfunctions of the USB devices. (Of course this doesn't justify the FTDI driver).
此问题与负责实现虚拟 COM 端口的 FTDI 驱动程序中的故障有关。另一方面,这些“故障”与 USB 设备的各种故障有关。(当然,这并不能证明 FTDI 驱动程序的合理性)。
BTW there're several other known problems with some FTDI drivers:
顺便说一句,某些 FTDI 驱动程序还有其他几个已知问题:
- Sometimes call to
CloseHandle
just hangs the calling thread. - Sometimes also the application is still "visible" in the task manager, even after it's closed. Task manager can't terminate the application, and the debugger can't be attached to it. Its EXE file is locked (can't be erased).
- 有时调用
CloseHandle
只是挂起调用线程。 - 有时应用程序在任务管理器中仍然“可见”,即使在它关闭之后也是如此。任务管理器不能终止应用程序,调试器不能附加到它。它的 EXE 文件被锁定(无法删除)。
Usually unplugging the USB device immediately helps in those situations. The FTDI driver, which seems to be "waiting for something" awakes.
通常,在这些情况下立即拔下 USB 设备会有所帮助。似乎在“等待某事”的 FTDI 驱动程序苏醒了。
回答by Hans Passant
This is certainly not normal. Windows automatically closes any handles that are left open after the process terminates. This must be a flaw in your USB device driver, although it is hard to see how it could mess this up. The ones that emulate serial ports are however notoriously lousy. Well, nothing much you can do but hope for a driver update from the manufacturer. Or a device from another manufacturer.
这当然不正常。在进程终止后,Windows 会自动关闭所有保持打开状态的句柄。这一定是您的 USB 设备驱动程序中的一个缺陷,尽管很难看出它是如何搞砸的。然而,模拟串行端口的那些是出了名的糟糕。好吧,除了希望制造商更新驱动程序外,您无能为力。或者其他制造商的设备。
回答by Jeremy Friesner
Is there any possibility that some threads or child processes of your crashed program are still running and holding a copy of the file handle? Perhaps a debugger process is still open? If so, those might be keeping the device open. I'd check Task Manager just to be sure; if so, force-killing the leftover processes might fix the problem.
是否有可能崩溃程序的某些线程或子进程仍在运行并持有文件句柄的副本?也许调试器进程仍然打开?如果是这样,那些可能会使设备保持打开状态。我会检查任务管理器只是为了确定;如果是这样,强制终止剩余的进程可能会解决问题。
回答by dbasnett
One other thing that you don't want to happen is to have an open usb serial port and the user pull the usb to serial adapter. That bug has (is?) been around for a long time. Here was an answer to the bug
您不希望发生的另一件事是打开 USB 串行端口,并且用户将 USB 连接到串行适配器。这个错误已经(是?)存在了很长时间。这是错误的答案
"Posted by Microsoft on 3/27/2009 at 4:03 PM Hi, Thank you for reporting this issue. We are aware of this problem and have fixed it for the next major release of the .NET framework.
“Microsoft 于 2009 年 3 月 27 日下午 4:03 发布 您好,感谢您报告此问题。我们已注意到此问题,并已在 .NET 框架的下一个主要版本中修复了该问题。
If you have any concerns, please reactivate this issue and I'll respond asap. Thanks, Kim Hamilton Base Class Libraries"
如果您有任何疑虑,请重新激活此问题,我会尽快回复。谢谢,金汉密尔顿基类库”
Don't know if the problems are related. Microsoft Connect has more than a few USB Serial bugs reported.
不知道问题是否相关。Microsoft Connect 报告了多个 USB 串行错误。
回答by Sylvain
Maybe you can add a Try catch close around your main code, and call CloseHandle in the catch close. Then even if the program craches, CloseHandle will be called.
也许您可以在主代码周围添加 Try catch close,并在 catch close 中调用 CloseHandle。那么即使程序崩溃了,CloseHandle 也会被调用。
try
{
HANDLE hPort = NULL;
hPort = CreateFile(...);
// You code...
}
catch (...)
{
if (hPort != NULL)
CloseHandle(hPort);
}
回答by mtrw
Try unplugging the device and plugging it back in. Sometimes Windows needs to be reminded that nobody's connected to that port anymore.
尝试拔下设备并重新插入。有时需要提醒 Windows 没有人再连接到该端口。