如何在 PyUsb 中 bind/unbind usb 设备?

How to bind/unbind usb device in PyUsb?

我的 python 脚本需要 on/off 多个 USB 设备。 我可以使用 PyUsb 绑定和取消绑定 USB 设备吗?

我可以用 shell 命令来完成:
关机:
echo "device_nuber" > /sys/bus/usb/drivers/usb/unbind
开机:
echo "device_nuber" > /sys/bus/usb/drivers/usb/bind

如何在 python 脚本中执行同样的操作?

您可以使用 attach_kernel_driverdetach_kernel_driver 来做到这一点。


import usb.core
dev = usb.core.find(idVendor=0x1234,idProduct=0x5678)
# unbind interface 0
dev.detach_kernel_driver(0)
# bind interface 0
dev.attach_kernel_driver(0)