Control USB port's power?
Asked Answered
N

11

33

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#?

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.

I know it sounds pointless but I need to do it for something awesome.

I also know that it might require a custom driver.

Niblick answered 17/12, 2009 at 23:8 Comment(1)
+1 for ... "I need to do it for something awesome"Martens
P
6

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.

That being said, you'll probably want to take a look @: http://sourceforge.net/projects/libusbdotnet/

LP, Dejan

Puce answered 17/12, 2009 at 23:18 Comment(4)
Nick, Do you mean an Ethernet or phone jack or an RS232 port?Burka
RS232 port very easily, and you can flip various status bits to drive LEDs.Embellish
With SerialPort component, which is part of .NET since v2.0 (IIRC). Straightforward example is @ #667305. LP, DejanFredenburg
Use the System.IO.Ports.SerialPort class and set the DtrEnable property to true to turn on the DTR pin on the RS232 connector.Assuntaassur
F
9

You can't simply toggle pins on a USB port. Period. USB is a serial protocol. The connector contains

  1. 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.
  2. Data. The data is sent via a serial protocol, so there is no way to control those pins if you're using 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 FTDI USB controllers. The FT232 and FT245 are especially nice to work with). For a low-end microcontroller based solution the V-USB driver for AVR controllers might be interesting.

For easy bit-banging IO pins on the PC use the parallel port. USB is really not made nor suited for that.

Falgoust answered 17/12, 2009 at 23:35 Comment(0)
O
8

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

USB 2.0 nominally supports per-port power switching:

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.

Oxysalt answered 8/2, 2012 at 13:17 Comment(0)
P
6

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.

That being said, you'll probably want to take a look @: http://sourceforge.net/projects/libusbdotnet/

LP, Dejan

Puce answered 17/12, 2009 at 23:18 Comment(4)
Nick, Do you mean an Ethernet or phone jack or an RS232 port?Burka
RS232 port very easily, and you can flip various status bits to drive LEDs.Embellish
With SerialPort component, which is part of .NET since v2.0 (IIRC). Straightforward example is @ #667305. LP, DejanFredenburg
Use the System.IO.Ports.SerialPort class and set the DtrEnable property to true to turn on the DTR pin on the RS232 connector.Assuntaassur
E
3

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.

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.

Or go the whole hog and get a microcontroller with built in USB ... the possiblities are endless!

Embellish answered 17/12, 2009 at 23:23 Comment(1)
Yes, look up Arduino if you want a programmable microcontroller with USB support, open source, different versions exist, some very small. Very easy to get started & good fun.Ondrea
L
2

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.

Logistic answered 17/12, 2009 at 23:14 Comment(0)
C
1

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.

Cormier answered 17/12, 2009 at 23:12 Comment(1)
@Nick. I'm not sure I understand the question. A device will require a controller (e.g. PIC18F4550 or similar). I'm sure you can wire an always on LED from 5V to GND with the right resistor to limit the current.Cormier
S
1

It may be 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.

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.

Sceptic answered 17/12, 2009 at 23:15 Comment(2)
No, I don't believe this turns off power. Both my USB sticks remain glowing after "safely remove hardware". All Windows does when you select this is flush the file cache to the device, remove the drive letter and inform you when it has completed.Elitism
And this is also a 1-shot deal for 99% of devices. You have to reinsert the device to get it re-enumerated.Assuntaassur
S
1

What you need is either a development board with programmable I/O pins (a.k.a. PIO) or a 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 could be 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.

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.

Spindry answered 18/12, 2009 at 0:29 Comment(0)
F
1

If you are lucky enough to have the right USB hub, per-port power control is possible. See https://github.com/mvp/uhubctl for a command line application written in C. It is supported in the USB specification. See Section 11.5.1.2 for an introduction. One example of a USB hub chip that supports per-port power switching is the Microchip USB2422, see page 24 of the data sheet for the register control. A hub using this chip doesn't guarantee a hub with per-port power control as the hardware may not provide the high current switching in the hub. I have several USB hubs that the hub chip indicates per-port power control but the hub in fact does not offer any power control.

Fact answered 15/9, 2023 at 0:42 Comment(2)
Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since another user already posted that solution. If a previous answer was helpful to you, you should vote it up instead of repeating the same information.Ade
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Tessin
B
0

There is no way to do it for the actual power and ground pins. And you should probably read this portion of the Wikipedia USB article.

Burka answered 17/12, 2009 at 23:22 Comment(0)
N
-1

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.

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.

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.

Nodose answered 20/6, 2012 at 19:8 Comment(1)
Edited: lookup the difference between current and power.Hyperbole

© 2022 - 2024 — McMap. All rights reserved.