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:
- the GUID for COM ports: {4d36e978-e325-11ce-bfc1-08002be10318} (CLSID_Ports)
- 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);
}