Can I write a program that swiches USB on/off

感情迁移 提交于 2020-01-04 15:53:36

问题


I have a USB modem and it often drops the signal. When this happens I pull unplug and re-plug it from the USB port and it gets the signal right back; Can I write a program that does that without having to physically disconnect the modem from the port?


回答1:


As hinted in a comment, programmatically resetting USB ports is an OS-specific feature.

In Windows the easiest approach is probably by leveraging WMI aka Windows Management Instrumentation. You can get several snippets that "mess" with USB ports, in various languages, by searching the web with "WMI", "USB" and such keywords.

This MSDN article is of broader interest than just USB controllers but provides a good overview of the CIM model and WMI interface.

Edit:
Indeed if this question pertains to Windows, I just discovered it is a duplicate on SO:

  • Connect and disconnect usb programmaticaly without unplug and replug
  • Windows power-off a usb device in software

BTW, both these links were identified automatically by SO, in the "Related" list on the right of the page
  ==> We need to remember to check thislist more systematically
  ==> In many cases, this list is damn good




回答2:


I think the simplest way is the hardware way: if you cut the USB power then you will have a unplug/plug event.

Two solutions:

1/ A powered USB hub: most USB hub have an external power socket which mechanically cut the power from the host when the power plug is inserted. Cut power coming from the power adapter, and all devices under the hub will be disconnected.

2/ Just hack a regular USB cable: you just have to cut the power wire (the red one).

To cut the power you can use an USB relay plugged on a different USB port. You can find USB relays everywhere on the net, but here are some:

http://www.yoctopuce.com/EN/products/category/usb-actuators (driverless)

http://www.phidgets.com/products.php?category=9




回答3:


Depends on the operating system. For example, with FreeBSD you can use "usbconfig ugenX.Y power_off" and then "power_on".



来源:https://stackoverflow.com/questions/2360438/can-i-write-a-program-that-swiches-usb-on-off

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!