简体   繁体   中英

Control the power of a usb port in Python

I was wondering if it could be possible to control the power of usb ports in Python, using vendor ids and product ids. It should be controlling powers instead of just enabling and disabling the ports. It would be appreciated if you could provide some examples.

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']) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM