Turn off power to USB Port programmatically
If you have admin privileges, then run the following:
Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4, Microsoft.Win32.RegistryValueKind.DWord);
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 turn USB port power on and off in Raspberry PI 4
Yes, uhubctl supports RPi4B, I have recently added support for it - you need to use uhubctl version 2.4.0 or later (or build it from master branch). It is also necessary to update USB firmware using sudo rpi-eeprom-update
to make power switching actually work.
Note that you are missing out by using sysfs method to turn USB off on RPi3B+ - using uhubctl you can control either all 4 ports, or 2 of them independently. RPi4B only supports turning off all ports at once.
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
Controlling a USB power supply (on/off) with Linux
Note. The information in this answer is relevant for the older kernels (up to 2.6.32). See tlwhitec's answer for the information on the newer kernels.
# disable external wake-up; do this only once
echo disabled > /sys/bus/usb/devices/usb1/power/wakeup
echo on > /sys/bus/usb/devices/usb1/power/level # turn on
echo suspend > /sys/bus/usb/devices/usb1/power/level # turn off
(You may need to change usb1 to usb n)
Source: Documentation/usb/power-management.txt.gz
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.
Related Topics
Read Performance Counters Periodically in Linux
How to Make Library Installed from Opam Available to Ocaml
Unshare User Namespace and Set UId Mapping with Newuidmap
How to Determine a Tar Archive's Format
Howto Use Sed to Remove Only Triple Empty Lines
How Does The Os Know Disk Address of an Absent Page
Why Does High-Memory Not Exist for 64-Bit Cpu
Can 'Vim' Open a Large File in Read Only Mode as Fast as 'Less'
Reusing Compression Dictionary
How Does Iwlist() Command Scans The Wireless Networks
How to Reset Ipython/Jupyter Profile After Upgrade to 'Factory' Defaults
How to Show Printk() Message in Console
Moving a Git Repo to Another Server
Listening for New Processes in Linux Kernel Module