在 Windows 上的软件中关闭 USB 设备

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

Power off an USB device in software on Windows

windowswinapiembeddedusbmicrocontroller

提问by

I would like to power cycle an USB device through software on Windows.

我想通过 Windows 上的软件重启 USB 设备。

I am doing development on a small USB power microcontroller. This chip will revert to native behavior on a power cycle and allow a code download. Since my code will crash the device when things go wrong -- making it ignore all USB commands -- I have to physically unplug the device from the system.

我正在开发一个小型 USB 电源微控制器。该芯片将在电源循环时恢复到本机行为并允许下载代码。由于我的代码会在出现问题时使设备崩溃——使其忽略所有 USB 命令——我必须从系统中拔出设备。

I want to do development remotely, and not have to be physically present.

我想远程进行开发,而不必亲自到场。

So far I have tried using "devcon" to disable portions of the USB stack. While this takes the hubs into D3 (should be powered off!), there is still power being supplied to the device.

到目前为止,我已经尝试使用“devcon”来禁用部分 USB 堆栈。虽然这会将集线器带入 D3(应该关闭电源!),但仍然为设备供电。

Are there any preexisting solutions or SetupAPI tricks that might help?

是否有任何预先存在的解决方案或 SetupAPI 技巧可能会有所帮助?

回答by devstuff

Since it's for development/test instead of production, I'd recommend having a solid-state_relayas part of a second device and using that to cycle the power on the USB connection.

由于它用于开发/测试而不是生产,我建议将一个solid-state_relay作为第二个设备的一部分,并使用它来循环 USB 连接上的电源。

回答by Carles

You could do with a hardware relay and controlling it via software: http://pintant.cat/2012/05/12/power-off-usb-device/(of course, will use 2 USB ports :-) one for the device, one for the relay).

您可以使用硬件继电器并通过软件控制它:http: //pintant.cat/2012/05/12/power-off-usb-device/(当然,将使用 2 个 USB 端口 :-) 一个用于设备,一个用于继电器)。

回答by irdroid3

Some USB hubs have the ability to turn power off to its downstream devices.

某些 USB 集线器能够关闭其下游设备的电源。

"Is it possible to power up ports on a USB hub from Ubuntu?" https://askubuntu.com/questions/149242/is-it-possible-to-power-up-ports-on-a-usb-hub-from-ubuntuWhich points to some c source for hub-ctrl.c

“是否可以从 Ubuntu 为 USB 集线器上的端口供电?” https://askubuntu.com/questions/149242/is-it-possible-to-power-up-ports-on-a-usb-hub-from-ubuntu其中指向 hub-ctrl.c 的一些 c 源

See: http://www.gniibe.org/development/ac-power-control-by-USB-hub/index

请参阅:http: //www.gniibe.org/development/ac-power-control-by-USB-hub/index

I tested this on Ubuntu with a Dream-Cheeky USB LED unit, and it did seem to turn the power off - although when turned on again, the device had remembered (!) its LED on/off state ??? - This did not happen when I unplugged and repluged the USB lead to the LED...

我用 Dream-Cheeky USB LED 单元在 Ubuntu 上测试了这个,它似乎确实关闭了电源 - 尽管再次打开时,设备记住了(!)它的 LED 开/关状态???- 当我拔下并重新插入 LED 的 USB 导线时,这并没有发生......

It looks like someone has looked at compiling this for Windows, but I could not find the result, sorry.

看起来有人已经考虑过为 Windows 编译这个,但我找不到结果,抱歉。

回答by Fanblade

There is a simple solution to this on Windows if you buy this external USB hub. It has an undocumented feature that cycles power to all connected USB devices whenever the hub is "enabled" in Windows Device Manager. You can do this manually:

如果您购买此外部USB 集线器,则在 Windows 上有一个简单的解决方案。它有一个未公开的功能,只要在 Windows 设备管理器中“启用”集线器,它就会为所有连接的 USB 设备循环供电。您可以手动执行此操作:

  1. Run "Device Manager"
  2. Expand "Universal Serial Bus controllers"
  3. Open Properties for the appropriate "Generic USB Hub"
  4. Select the "Driver" tab
  5. Click the "Disable" button
  6. Click the "Enable" button. The hub will briefly disable power output to all 4 of its USB ports, and then enable it again.
  1. 运行“设备管理器”
  2. 展开“通用串行总线控制器”
  3. 打开相应“通用 USB 集线器”的属性
  4. 选择“驱动程序”选项卡
  5. 单击“禁用”按钮
  6. 单击“启用”按钮。集线器将暂时禁用所有 4 个 USB 端口的电源输出,然后再次启用它。

You can also do this programmatically from any Windows application that is running with administrator priveledges. Here is an example in C#.

您还可以从任何以管理员权限运行的 Windows 应用程序以编程方式执行此操作。这是C# 中的示例

I have successfully used this technique to cycle power to USB devices that were "locked up". It sure beats physically unplugging devices, especially on remote or unmanned PCs.

我已经成功地使用这种技术为“锁定”的 USB 设备循环供电。它确实胜过物理拔掉设备,尤其是在远程或无人电脑上。

回答by Mark Maxham

Unfortunately, it's not possible on stock PCs.

不幸的是,这在库存 PC 上是不可能的。

USB host chipsets do not have the capability to fully power-down. There is some signaling that you can do in USB 2.0 or 3.0 to ask the device to go into a sleep-state, but the port will provide power even when the PC is turned off. (That's so devices like iPods and whatnot can still charge if needed.)

USB 主机芯片组没有完全断电的能力。您可以在 USB 2.0 或 3.0 中执行一些信号来要求设备进入睡眠状态,但即使 PC 关闭,该端口也会供电。(这样的话 iPod 之类的设备仍然可以在需要时充电。)