python - 在 Python 中控制 USB 端口的电源
问题描述
我想知道是否可以使用供应商 ID 和产品 ID 在 Python 中控制 USB 端口的功能。它应该控制电源,而不仅仅是启用和禁用端口。如果您能提供一些示例,我们将不胜感激。
解决方案
查看标准库中的subprocess模块:
您需要什么命令取决于操作系统。
视窗
对于 Windows,您将需要查看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
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'])
推荐阅读
- javascript - 在 Storybook 和 Angular 中加载 scss 文件:预期 1 个选择器或规则,是“var content = requi”
- r - 从单个字符串中提取两年
- jquery - Bootstrap Alignment - 与搜索栏问题
- r - 无法在 R 中重新组织嵌套 foreach 循环的结果
- angular - 在 ASP.NET Core 2.1 SPA 模板中找不到 webpack.config.js 或 webpack.config.vendor.js
- sql - 组中排名靠前的结果的 SQL 优化
- c# - C# 将 combobox.value 获取到路径
- css - Windows 操作系统中的 Roboto-Regular 字体大小 14px 问题
- swift - 我的 Scrollview 不会滚动 ios
- angular - 在处理组件中的 nativeElement.setAttribute 的函数中升级到 Angular6 后出现 TypeError