7

We have systems that connect through devices that are connected to PCs through USB. In some cases there is an adapter in between so it is a RS-232 to USB conversion. Is it possible to tell the Device Manager to remove a port and then add it back in? I would like to do this so the drivers for the various devices (which in some cases are more than 1 such as the device through the USB Serial adapter) all reset. It seems removing the port and then adding it back in would ensure all the drivers reconnect. This is important as some third party drivers can get "stuck" and due to threading it is not possible to use it anymore. This means it would require a physical removal of the connection, or a full system reset.

So I am hoping someone knows if there is a simple way to connect to the device manager find Ports remove them and then reconnect them. Something like this:

public class DeviceManagerWrapper
{
    public void ResetAllPorts()
    {
        using(var deviceManager = new System....DeviceManager()
        {
            foreach(var port in deviceManager.GetPorts())
            {
                 port.Disconnect();
                 port.Reconnect();
            }
        }
    }
}

Obviously this doesn't work. Just hoping for someone to point me in the right direction.

Cœur
  • 37,241
  • 25
  • 195
  • 267
N_tro_P
  • 673
  • 5
  • 15
  • @x... how is that at all the same? First of all, they are reseting the display devices NOT the USB. Secondly they are using a pre-packaged tool from Microsoft. I need to do this in C# so a system can be automated. I guess some other language is OK, but I did tag it C#. But a pre-packaged tool will most certainly not work. There are too many variances in the devices and the drivers. – N_tro_P Aug 08 '16 at 15:06
  • Possible duplicate of [How to get information from device manager?](http://stackoverflow.com/questions/6942339/how-to-get-information-from-device-manager) – djv Aug 08 '16 at 15:14
  • 1
    @Verdolino reading is not the same as controlling and you just suggested the exact same "Tool" as the previous commenter. – N_tro_P Aug 08 '16 at 15:29
  • 1
    The accepted answer states *Microsoft provides a sample: http://support.microsoft.com/kb/311272*, if you follow the link and read more about the tool, it can: **Change device settings** DevCon can change the status or configuration of Plug and Play (PnP) devices on the local computer in the following ways: **Enable a device**, **Disable a device**, Update drivers (interactive and noninteractive), Install a device (create a devnode and install software), Remove a device from the device tree and delete its device stack... This will be your best bet. Any reason you don't want to use it? – djv Aug 08 '16 at 15:33
  • @Verdolino Are you asking why I don't want to build a C# OO API design around a tool that MS built and is also not planning on building .Net OO integration with for system automation? You can't seriously be suggesting those "examples" as a good source for what I am asking are you? – N_tro_P Aug 09 '16 at 13:38
  • I've used it with c# and it works. Good luck – djv Aug 09 '16 at 13:46
  • Possible duplicate of [Win32 API function to programmatically enable/disable device](https://stackoverflow.com/questions/1438371/win32-api-function-to-programmatically-enable-disable-device) – Ian Kemp Jan 22 '19 at 11:14
  • apparently related (from a link-only answer): https://superuser.com/questions/1544131/windows-10-programmatically-reconnect-usb-ftdi – starball May 08 '23 at 06:22
  • I found a similar (not identical) question here: [Windows 10 programmatically reconnect USB FTDI](https://superuser.com/questions/1544131/windows-10-programmatically-reconnect-usb-ftdi). I think the answer is a viable solution. – DarkLight May 08 '23 at 06:04

0 Answers0