C# 控制USB端口的电源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1925237/
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
Control USB port's power?
提问by Kristina Brooks
Does anybody know how to control USB pins on a certain USB port? I think it is definately possible in assembler but what about C++ or C#?
有人知道如何控制某个 USB 端口上的 USB 引脚吗?我认为在汇编程序中绝对有可能,但是 C++ 或 C# 呢?
I want to be able to use USB battery as a power supply for an LED or something like that. So then a program would power it on and power it off making it flash.
我希望能够使用 USB 电池作为 LED 或类似电源的电源。所以然后一个程序会打开它并关闭它使其闪烁。
I know it sounds pointless but I need to do it for something awesome.
我知道这听起来毫无意义,但我需要做一些很棒的事情。
I also know that it might require a custom driver.
我也知道它可能需要自定义驱动程序。
采纳答案by Dejan Stani?
USB is not trivial, so I guess you'll have some problems (mis)using it. You would be /much/ better off (IMHO) with standard serial ports, which have been used for stuff like that for ages, with plenty of examples available. If you don't have serial port available on your target machine, you can use USB->Serial interface cable.
USB 不是微不足道的,所以我想你在使用它时会遇到一些问题(错误)。使用标准串行端口会/很多/更好(恕我直言),这些端口已经用于类似的东西很久了,有很多示例可用。如果您的目标机器上没有可用的串行端口,您可以使用 USB->串行接口电缆。
That being said, you'll probably want to take a look @: http://sourceforge.net/projects/libusbdotnet/
话虽如此,您可能想看看@:http: //sourceforge.net/projects/libusbdotnet/
LP, Dejan
LP, 德扬
回答by kenny
I think that you are looking for an USB device. A simple charger could be made, by putting or boost regulator to bump up the 5v higher or regulator to drive to a lower charging voltage. Turning it on or off would require a USB device.
我认为您正在寻找 USB 设备。可以制作一个简单的充电器,通过放置或升压调节器将 5v 提高到更高或调节器驱动到更低的充电电压。打开或关闭它需要 USB 设备。
回答by Andy Ross
There is no software control over the power and ground pins of a USB port, at least on PC chipsets. That is handled in hardware by the USB controller. Look at getting a USB Parallel Port adapter if you want software control over TTL voltages.
USB 端口的电源和接地引脚没有软件控制,至少在 PC 芯片组上是这样。这由 USB 控制器在硬件中处理。如果您想要对 TTL 电压进行软件控制,请查看获取 USB 并行端口适配器。
回答by rmeador
It maybe possible to turn on and off power to the device by doing whatever happens when you "safely remove hardware" a USB device, but I'm not 100% sure this turns off the power, it may just send a signal to the device tell it it's disconnected, which makes it turn itself off.
它可以是可以通过做一切发生在你“安全删除硬件” USB设备打开和关闭设备电源,但我不是100%肯定这将关闭电源,它可能只是发出一个信号到设备告诉它它已断开连接,这使它自己关闭。
The best bet for what you're trying to do is to build a USB device that actually communicates with the computer (possibly via a custom driver, but you can probably also use a standard driver like a HID device). You would have the computer send "on" or "off" (or anything else you can dream up) to your device, which would interpret the signals as you see fit (probably by turning on and off that LED you were talking about). This isn't as hard as it sounds. You can buy microcontrollers with USB interfaces on board, such as the Arduino, or some of the high-end PICs. Many of the manufacturers of these sorts of microcontrollers have documents available explaining how to do USB interfacing with them.
最好的办法是构建一个实际与计算机通信的 USB 设备(可能通过自定义驱动程序,但您也可以使用标准驱动程序,如 HID 设备)。您可以让计算机向您的设备发送“开”或“关”(或您能想到的任何其他内容),它会根据您认为合适的方式解释信号(可能通过打开和关闭您正在谈论的 LED)。这并不像听起来那么难。您可以购买带有 USB 接口的微控制器,例如 Arduino,或一些高端 PIC。许多此类微控制器的制造商都有可用的文档来解释如何与它们进行 USB 接口。
回答by John D.
回答by NickZoic
I think trying to switch the USB power on and off is not such a great way to do it: it is possible that your USB controller is able to do this, but it wouldn't be a "normal" thing to do.
我认为尝试打开和关闭 USB 电源并不是一个很好的方法:您的 USB 控制器有可能能够做到这一点,但这不是一件“正常”的事情。
Probably the easiest way is to use something like a FTDI FT245 USB->parallel chip. If you're just trying to flash some blinking lights, maybe go out and buy a standard USB -> parallel printer cable and interface to that ... you'd get 8 bits of output with very easy software control, and quite a lot more with only a bit of work.
可能最简单的方法是使用类似 FTDI FT245 USB-> 并行芯片的东西。如果你只是想闪烁一些闪烁的灯,也许出去买一个标准的 USB -> 并行打印机电缆和接口......你会通过非常简单的软件控制获得 8 位输出,而且相当多只需一点工作即可获得更多。
Or go the whole hog and get a microcontroller with built in USB ... the possiblities are endless!
或者全力以赴,获得一个内置 USB 的微控制器......可能性是无穷无尽的!
回答by bluebrother
You can't simply toggle pins on a USB port. Period. USB is a serial protocol. The connector contains
您不能简单地切换 USB 端口上的引脚。时期。USB 是一种串行协议。连接器包含
- Power. The Host can control the power lines as it can cut the power in case of overload. This is something done by the USB host driver, which means the driver of the host adapter in the PC. This does not mean any custom device driver you might need for hardware that doesn't use any of the device classes the OS already ships drivers.
- Data. The data is sent via a serial protocol, so there is no way to control those pins if you're using USB.
- 力量。主机可以控制电源线,因为它可以在过载的情况下切断电源。这是由 USB 主机驱动程序完成的,也就是 PC 中主机适配器的驱动程序。这并不意味着对于不使用操作系统已经提供的任何设备类的硬件,您可能需要任何自定义设备驱动程序。
- 数据。数据通过串行协议发送,因此如果您使用 USB,则无法控制这些引脚。
If you want to get some IO ports you need more logic. You need at least something that follows the USB protocol, which means some kind of microcontroller (or a special USB device controller like the FTDIUSB controllers. The FT232 and FT245 are especially nice to work with). For a low-end microcontroller based solution the V-USB driver for AVR controllersmight be interesting.
如果你想获得一些 IO 端口,你需要更多的逻辑。您至少需要一些遵循 USB 协议的东西,这意味着某种微控制器(或一种特殊的 USB 设备控制器,如FTDIUSB 控制器。FT232 和 FT245 特别适合使用)。对于基于低端微控制器的解决方案,AVR 控制器的V-USB 驱动程序可能会很有趣。
For easy bit-banging IO pins on the PC use the parallel port. USB is really not made nor suited for that.
为了轻松地对 PC 上的 IO 引脚进行位冲击,请使用并行端口。USB真的不是制造出来的,也不适合那个。
回答by Thomas Matthews
What you need is either a development board with programmable I/O pins (a.k.a. PIO) or a UART (RS232).
您需要的是带有可编程 I/O 引脚(又名PIO)或 UART(RS232)的开发板。
The easiest method is to connect the LED to the Clear To Send (CTS) and either Ground or VCC. The output on the CTS line will turn the LED on or off depending on whether the other end is connected to ground or not. The normal I/O pins couldbe used, but they are for serial data transmission in the UART. The UART usually builds an 8-bit character from these pins; but passes the RTS (Request To Send) and CTS values directly through.
最简单的方法是将 LED 连接到清除发送 (CTS) 和接地或 VCC。CTS 线上的输出将根据另一端是否接地来打开或关闭 LED。正常I / O引脚可以被使用,但它们是在UART串行数据传输。UART 通常从这些引脚构建一个 8 位字符;但直接通过 RTS(请求发送)和 CTS 值。
When using a PIO, all you have to do is set a bit in a variable and write that variable to the address of the PIO set. You may also have to latch in the bits. Some PIO pins will remember the value until the next value is written (called latched), others remember the value as long as it is written out.
使用 PIO 时,您所要做的就是在变量中设置一个位并将该变量写入 PIO 集的地址。您可能还需要锁定位。一些 PIO 引脚会记住该值,直到下一个值被写入(称为锁存),其他的只要它被写出就会记住该值。
回答by android.weasel
According to http://www.gniibe.org/development/ac-power-control-by-USB-hub/index
根据http://www.gniibe.org/development/ac-power-control-by-USB-hub/index
USB 2.0 nominally supports per-port power switching:
USB 2.0 名义上支持每端口电源切换:
Hub Descriptor:
[...]
wHubCharacteristic 0x0089
Per-port power switching
[...]
Sadly, the author reports that while may hubs' firmware claims to support this, the manufacturers have cut corners on the circuit board.
可悲的是,作者报告说虽然集线器的固件可能声称支持这一点,但制造商在电路板上偷工减料。
回答by mtelesha
The question is controlling the amperage of USB most answers failed to put into account the varience of mA of USB and it's effect on volts.
问题是控制USB的安培数大多数答案都没有考虑到USB mA的变化及其对电压的影响。
Common USB 1.0 and 2.0 output equals 500 mA or 0.5 A. 500mA times 5 volts equals 2.5 watts of power.
常见的 USB 1.0 和 2.0 输出等于 500 mA 或 0.5 A。500 mA 乘以 5 伏等于 2.5 瓦的功率。
1 A will give you the full 5 watts and USB 2.0 can be manipulated by ASUS Ai Charger to output 1.2 A or 1200 mA to give you a 6 watts out put.
1 A 将为您提供完整的 5 瓦,而 USB 2.0 可由华硕 Ai Charger 控制以输出 1.2 A 或 1200 mA,从而为您提供 6 瓦的输出。