How to unlock COM port
Asked Answered
S

2

3

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# .

Smacker answered 13/1, 2009 at 14:39 Comment(0)
L
7

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.
Leola answered 13/1, 2009 at 15:53 Comment(0)
T
0

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);
}
Tinytinya answered 4/8, 2017 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.