【问题标题】:Control the power of a usb port in Python在 Python 中控制 USB 端口的电源
【发布时间】:2021-03-07 11:35:22
【问题描述】:

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

【问题讨论】:

  • “权力”是什么意思
  • 我的意思是总线供电的电源
  • 您的硬件和操作系统是什么?我很确定它在 Raspberry Pi 上是可能的,在 PC/Mac 上可能更难。
  • 我正在使用 Linux

标签: python usb port libusb pyusb


【解决方案1】:

查看标准库中的subprocess 模块:

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

窗口

对于 Windows,您需要查看 devcon

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

【讨论】:

  • 我正在使用 Linux。如何将供应商 ID 和产品 ID 应用于此?
  • 您可以使用lsusb 命令列出USB 设备。这将列出 USB 总线和供应商 ID。然后,您可以使用 cat 命令检查每个 USBX 的供应商 ID 和/或产品 ID。示例:“cat /sys/bus/usb/devices/usb1/idVendor”
猜你喜欢
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多