在 Python 中控制 USB 端口的功率 [英] Control the power of a usb port in Python

查看:184
本文介绍了在 Python 中控制 USB 端口的功率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用供应商 ID 和产品 ID 来控制 Python 中 USB 端口的功率.它应该控制电源,而不仅仅是启用和禁用端口.如果您能提供一些示例,我们将不胜感激.

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.

推荐答案

查看 标准库中的子进程模块:

您需要什么命令取决于操作系统.

What commands you need will depend on the OS.

对于 Windows,您需要查看 devcon

For windows you will want to look into devcon

这已在 中得到解答以前的帖子

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

查看有关 shell 命令的帖子

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

这篇关于在 Python 中控制 USB 端口的功率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆