views:

1911

answers:

8

alt text

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.

+1  A: 

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.

kenny
Could two wires soldered to an LED pass out as an USB Device?
Nick Brooks
@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.
kenny
+2  A: 

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.

Andy Ross
+1  A: 

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.

rmeador
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.
Paul Lammertsma
And this is also a 1-shot deal for 99% of devices. You have to reinsert the device to get it re-enumerated.
devstuff
+5  A: 

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

Dejan Stanič
Can LAN or MODEM ports be manipulated through software?
Nick Brooks
Nick, Do you mean an Ethernet or phone jack or an RS232 port?
John D.
Yes , I meant those two
Nick Brooks
RS232 port very easily, and you can flip various status bits to drive LEDs.
Sharkey
And how do I do that?
Nick Brooks
With SerialPort component, which is part of .NET since v2.0 (IIRC). Straightforward example is @ http://stackoverflow.com/questions/666805/driving-dtr-with-system-io-ports-serialport-in-net.LP,Dejan
Dejan Stanič
Use the `System.IO.Ports.SerialPort` class and set the `DtrEnable` property to true to turn on the DTR pin on the RS232 connector.
devstuff
+1  A: 

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.

John D.
+3  A: 

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!

Sharkey
Dale Halliwell
+6  A: 

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.

bluebrother
+1  A: 

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.

Thomas Matthews