如何解锁COM端口

发布于 2024-07-11 08:34:46 字数 189 浏览 6 评论 0原文

我有一个应用程序必须在另一个应用程序之后才能运行。 第二个应用程序有一个错误,导致 COM 端口在特定情况下无法关闭。

我想在我的应用程序中以编程方式关闭所有 COM 端口,以确保不会报告有关关闭端口的错误。 如果我不拥有打开 COM 端口的对象,是否可以?

我需要 .NET Framework、C# 的解决方案。

I've a application that must work after another application. This second application has a bug that causes COM ports not to be closed in specific circumstances.

I would like to close all COM ports programmatically in my application to ensure, that there will be no bugs about close ports reported. Is it possible if I do not own objects that opened COM ports?

I need a solution on .NET Framework, C# .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你搞怪i 2024-07-18 08:34:46

没有好的、简单的方法:

  • 好的方法是修复其他应用程序; 或者如果这是不可能的,则编写一个过滤设备驱动程序(类似于 parport 驱动程序,但用于串行端口而不是并行端口),该驱动程序位于串行端口硬件驱动程序之上,并且会暴露多个连接点(由另一个应用程序,它传递到底层的真实驱动程序,以及您自己的应用程序使用的另一个“后门”...真正的驱动程序只会看到一个客户端,即位于其之上的过滤器驱动程序)
  • 简单的方法是用核武器攻击(强制终止)另一个进程。

There is no good, easy way:

  • A good way is to fix the other application; or if that's impossible, to write a filter device driver (similar to the parport driver but for serial ports instead of parallel ports) which would sit on top of the serial port hardware driver and which would expose more than one connection point (one used by the other application, which passes through to the underlying real driver, and another 'back door' used by your own application ... the real driver would only see one client, i.e. the filter driver wich sits on top of it)
  • An easy way is to nuke (forcibly terminate) the other process.
早茶月光 2024-07-18 08:34:46

我最近遇到了这个问题,需要补充一点...大多数 COM 端口驱动程序在设备管理器中启用和禁用设备时“解锁”端口。 这意味着解决方案中描述了完成此任务的 (C#) 方法:

Win32 API 函数以编程方式启用/禁用设备

使用该 COM 端口解决方案需要了解的信息是:

  1. COM 端口的 GUID:{4d36e978-e325-11ce-bfc1- 08002be10318} (CLSID_Ports)
  2. 您希望重置的端口的“实例路径”

由于您说您想要重置所有端口,因此您需要修改该示例中的库以循环所有端口端口更改

// Find the index of our instance. i.e. the touchpad mouse - I have 3 mice attached...
int index = GetIndexOfInstance(diSetHandle, diData, instanceId);
// Disable...
EnableDevice(diSetHandle, diData[index], enable);

为:

for (int index = 0; index < diData.Length; index++)
{
    EnableDevice(diSetHandle, diData[index], enable);
}

I came across this recently and have a little bit to add... Most COM port drivers "unlock" the port when the device is enabled and disabled in the device manager. This means that the (C#) way to accomplish this task is described in the solution:

Win32 API function to programmatically enable/disable device

The information that you need to know to use that solution for COM ports is:

  1. the GUID for COM ports: {4d36e978-e325-11ce-bfc1-08002be10318} (CLSID_Ports)
  2. the "instance path" of the port you wish to reset

Since you say that you want to reset all the ports, you would want to modify the library in that example to loop over all the ports by changing:

// Find the index of our instance. i.e. the touchpad mouse - I have 3 mice attached...
int index = GetIndexOfInstance(diSetHandle, diData, instanceId);
// Disable...
EnableDevice(diSetHandle, diData[index], enable);

to something like this:

for (int index = 0; index < diData.Length; index++)
{
    EnableDevice(diSetHandle, diData[index], enable);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文