vb.net 如何确定哪个进程正在使用串行端口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42197/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:49:08  来源:igfitidea点击:

How do I determine which process is using a serial port?

vb.netserial-port

提问by Grant

The company I work for makes hardware that communicates to the computer though a serial port. Third party companies write software that communicates with our hardware.

我工作的公司制造通过串行端口与计算机通信的硬件。第三方公司编写与我们的硬件通信的软件。

There are times when I need to diagnose our hardware. However, a third party software app connects to the serial port when Windows starts up, blocking any other connection. I don't know the name of this application/service and it's not always the same one.

有时我需要诊断我们的硬件。但是,当 Windows 启动时,第三方软件应用程序会连接到串行端口,从而阻止任何其他连接。我不知道这个应用程序/服务的名称,而且它并不总是相同的。

Is there any way to either:

有没有办法:

  • Find the name/pid of the app/service that is currently using a given serial port or
  • Steal the serial port connection from another app.
  • 查找当前使用给定串行端口的应用程序/服务的名称/pid 或
  • 从另一个应用程序窃取串行端口连接。

vb.net preferably, but I'll take a language agnostic answer as well.

最好使用 vb.net,但我也会采用与语言无关的答案。

采纳答案by Rob Walker

You can use the process explorertool also from SysInternals to search for open handles. In this case you would want to search for 'Serial' since it uses device names that may not map to com port numbers. (e.g. COM1 is \Device\Serial0 on my system).

您也可以使用来自 SysInternals的进程浏览器工具来搜索打开的句柄。在这种情况下,您可能想要搜索“Serial”,因为它使用的设备名称可能无法映射到 com 端口号。(例如,COM1 在我的系统上是 \Device\Serial0)。

If you want to take control of the serial port from another app I think you would need co-operation of the driver.

如果您想从另一个应用程序控制串行端口,我认为您需要与驱动程序合作。

回答by Don Kirkby

As Rob Walker said, you can find who's using a serial port using Process Explorer. Most of the time, typing Ctrl+Fand searching for "serial" will show you who has a serial port open, but I just ran into a situation where my "COM3" serial port's handle appeared as "\Device\VCP0". It may be strange because it was running under VirtualBox with a USB-to-serial connector.

正如Rob Walker 所说,您可以使用Process Explorer找到谁在使用串行端口。大多数情况下,键入Ctrl+F并搜索“serial”将显示谁打开了串行端口,但我遇到了“COM3”串行端口句柄显示为“\Device\VCP0”的情况。这可能很奇怪,因为它是在带有 USB 转串口连接器的 VirtualBox 下运行的。

If searching for "serial" and "device\vcp" don't get you any results, you may be able to figure out how serial port handles are named by opening one with a known program. In Process Explorer, display the lower pane with each process's open handles by typing Ctrl+L. Click on the process that you used to open the serial port and look through the lower pane to see which handles look like they might be a serial port. You can open and close the port while you're looking, and the file handle should appear and disappear, as well as being highlighted in green or red. Of course, this is only possible if you have more than one serial port or the serial port you're trying to diagnose isn't always locked by some mystery process.

如果搜索 "serial" 和 "device\vcp" 没有得到任何结果,您可以通过打开一个已知程序来弄清楚串行端口句柄是如何命名的。在进程资源管理器中,通过键入Ctrl+显示带有每个进程打开句柄的下部窗格L。单击用于打开串行端口的进程并查看下方窗格以查看哪些句柄看起来像是串行端口。您可以在查看时打开和关闭端口,文件句柄应该出现和消失,并以绿色或红色突出显示。当然,这只有在您有多个串行端口或您尝试诊断的串行端口并不总是被某个神秘进程锁定时才有可能。

回答by Adam Davis

Sysinternalshas a slew of utilities I find very useful and educational for tracking down what processes are doing to the system.

Sysinternals有许多实用程序,我发现它们非常有用且具有教育意义,可用于跟踪进程对系统执行的操作。

They have a utility that does exactly what you need called Portmon, and give some information on how it works near the bottom of the page. That info and a few well-asked questions will probably give you everything you need to implement it yourself if the utility isn't enough.

他们有一个实用程序可以完全满足您的需要,称为Portmon,并在页面底部附近提供有关它如何工作的一些信息。如果实用程序不够用,这些信息和一些问得很好的问题可能会为您提供自己实现它所需的一切。

-Adam

-亚当