.net 强制关闭 COM 端口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7208094/
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
Force a COM port to close
提问by Clone
I'm working on an application that uses COM port for communication with external controller. When I reboot the PC with communication cable connected, Windows (7) opens the port, without any application running, so I can't access it any more. I tried to close it programatically, but it just stays taken.
我正在开发一个使用 COM 端口与外部控制器通信的应用程序。当我在连接了通信电缆的情况下重新启动 PC 时,Windows (7) 打开了端口,没有运行任何应用程序,因此我无法再访问它。我试图以编程方式关闭它,但它只是保持不变。
Anyone got an idea how can I force it to close, any program I can run before my app, or a way to programatically close it no matter what?
任何人都知道如何强制关闭它,我可以在我的应用程序之前运行的任何程序,或者无论如何以编程方式关闭它的方法?
回答by Clone
I didn't manage to close the port, but after a lot of searching and testing I found what was the source of my problems. I'm posting the solution to help anyone who has the same problem.
我没有设法关闭端口,但经过大量搜索和测试后,我发现了问题的根源。我正在发布解决方案以帮助遇到相同问题的任何人。
Windows identifies devices with constant data stream connected to serial port as serial mouse (in this system it was Microsoft Serial Ballpoint), and tries to install them, taking over the control over the port without any process using it. The solution is to wait for Windows to install the device (check Device Manager under Mouses And Other Pointing Devices) and then DISABLING it. If you uninstall the device it will be installed again, and the problem is back.
Windows 将连接到串行端口的具有恒定数据流的设备识别为串行鼠标(在此系统中为 Microsoft Serial Ballpoint),并尝试安装它们,接管对端口的控制,而无需任何进程使用它。解决方法是等待 Windows 安装设备(检查鼠标和其他指针设备下的设备管理器),然后禁用它。如果您卸载设备,它将再次安装,问题又回来了。
Thanks to everyone who tried to help, and I hope others will find this solution useful.
感谢所有试图提供帮助的人,我希望其他人会发现这个解决方案很有用。
回答by shf301
No there's no way to force the port to close (see this question). There is no way to force another process to close the port so you can take it.
不,无法强制关闭端口(请参阅此问题)。没有办法强制另一个进程关闭端口,以便您可以使用它。
But Windows isn't opening up the port by itself - some other application running in the background is probably doing it. Download Process Explorerand use the "Find Handle or DLL" on the Find menu to find the process with the com port open. If it is a physical COM port on the machine it will be named Serial0, Serial1, ..., or SerialN where COM1 maps to Serial0. If it's a USB to COM adapter I'm not sure what the device ID will be.
但是 Windows 不会自己打开端口 - 在后台运行的其他一些应用程序可能正在这样做。下载进程资源管理器并使用“查找”菜单上的“查找句柄或 DLL”来查找打开 com 端口的进程。如果它是机器上的物理 COM 端口,它将被命名为 Serial0、Serial1、...或 SerialN,其中 COM1 映射到 Serial0。如果是 USB 转 COM 适配器,我不确定设备 ID 是什么。
You could also try using PortMonto watch for activity on the port. PortMon will show you the process that has the port open.
您还可以尝试使用PortMon来监视端口上的活动。PortMon 将显示打开端口的进程。
回答by Jianhong
how i resolved was turned off the device with the continuous output, unplug the usb, plug it in again to let windows to detect the com number, then turn the device on again.
我的解决方法是关闭设备并连续输出,拔下 USB,再次插入以让 Windows 检测 com 号,然后再次打开设备。
回答by Andy
My guess is that when you close your app and reboot the PC, the external controller is not closing it's side of things gracefully and is left in an undefined state.
我的猜测是,当您关闭应用程序并重新启动 PC 时,外部控制器并没有优雅地关闭它的一侧,而是处于未定义状态。
When the PC is rebooted the controller is still in this illegal state and it gives the appearance that you cannot open the port on the PC because comms cannot be re-established correctly.
当 PC 重新启动时,控制器仍处于这种非法状态,并且看起来您无法打开 PC 上的端口,因为无法正确重新建立通信。
Perhaps the external controller is not handling changes of state of the DTR/DSR lines correctly?
也许外部控制器没有正确处理 DTR/DSR 线路的状态变化?
How is the cable between PC and controller wired?
PC和控制器之间的电缆如何接线?
Hard to say much more without knowing any specifics.
在不知道任何细节的情况下,很难说更多。
回答by Juan Fer Montufar
What I did was to disable the controller, turn it off, restart my computer, turn it on (as soon as windows loads) and go back to enable it. It worked for me with two microcontrollers with this problem.
我所做的是禁用控制器,将其关闭,重新启动我的计算机,将其打开(一旦 Windows 加载)并返回启用它。它对我有两个有这个问题的微控制器。
回答by Hymannad
Something like this?
像这样的东西?
if (serialPort.IsOpen) serialPort.Close();

