Control Usb Ports

Control USB port's power?

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

Control the power of a usb port in Python

Look into the subprocess module in the standard library:

What commands you need will depend on the OS.

Windows

For windows you will want to look into devcon

This has been answered in previous posts

import subprocess
# Fetches the list of all usb devices:
result = subprocess.run(['devcon', 'hwids', '=usb'],
capture_output=True, text=True)

# ... add code to parse the result and get the hwid of the device you want ...

subprocess.run(['devcon', 'disable', parsed_hwid]) # to disable
subprocess.run(['devcon', 'enable', parsed_hwid]) # to enable

Linux

See posts on shell comands

import subprocess

# determine desired usb device

# to disable
subprocess.run(['echo', '0', '>' '/sys/bus/usb/devices/usbX/power/autosuspend_delay_ms'])
subprocess.run(['echo', 'auto', '>' '/sys/bus/usb/devices/usbX/power/control'])
# to enable
subprocess.run(['echo', 'on', '>' '/sys/bus/usb/devices/usbX/power/control'])

How to control a specific USB port in windows with python?

Well this is the solution i was able to use and it is working fine with me.

import subprocess

# find all devices command
Find_command = 'C:/Windows/SysWOW64/devcon.exe find *'

#list HW ids of devices
hwIds_command = 'C:/devcon.exe hwids *'

#Enable
Enable_command = 'C:/devcon.exe enable *PID_1016'

#Disable
Disable_command = 'C:/devcon.exe disable *mouse*'

#Find Device
Find_SpecificDevice_command = 'C:/devcon.exe find *PID_1016 '
find= True
try:
if find:
result = subprocess.check_output(Find_SpecificDevice_command,shell=True ,stderr=subprocess.STDOUT)
print(result)
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

If you have any questions regarding the solution feel free to ask.

Is it possible to control the power in a USB port?

I believe the USB spec allows for different power load states. Initially a device is supplied 100mA. Once a device can handshake with the USB controller and perform a certain request, the controller can supply a "high load" current of 500mA. Check this related SO question for more details.

How to control my USB port using C#

It isn't possible to change the power status of a USB port from User mode code. It's not a .NET limitation, no user application can change the power status of a USB port. Actually, I think that the power state is controlled by the chipset itself and you can't turn it off without using a specialized chipset.

Besides, if your device short-circuited, you could burn your chipset and your motherboard.

The only (safe) solution is to use an external device that will control a relay to turn power on or off for your devices.

There are multiple answers to similar questions, eg Windows- Power off a USB device in Software

A good option is to use an external device like Arduino (or Netduino), .NET Gadgeteer or any number of external boards. Prices and capabilities vary of course.

If you want to build the device yourself, you can find numerous USB relays with a simple Google search

EDIT

Regarding serial ports, there is almost no relation at all between the two port types. Serial ports are not designed to provide power. You can use tricks to use a pin's voltage to power some external device, although this will lead to problems because there is not enough current to power most devices. Burning the chipset is another likely outcome.

A USB port on the other hand, is explicitly designed to provide power through specific wires which can't be controlled normally. This is why you can charge your phone from a desktop's USB port even if the machine has shut down.

EDIT 2: Sleep-and-charge ports
As Ben Voigt noted, not all USB ports allow charging while shutdown.

There are multiple port types that support charging. Charging ports provide more current than plain ports but shut down when the computer is switched off. Sleep-and-charge ports on the other hand can provide power even when the computer is off.



Related Topics



Leave a reply



Submit