Python PyUSB:检测设备拔出

Python PyUSB:检测设备拔出,python,linux,pyusb,Python,Linux,Pyusb,我希望我的程序在设备拔出时退出。 我的代码: device = usb.core.find(idVendor, idProduct) device.detach_kernel_driver(0) usb.util.claim_interface(device, 0) usb_cfg = device.get_active_configuration() usb_interface = usb_cfg[(0,0)] port_in = usb_interface[0] port_out

我希望我的程序在设备拔出时退出。 我的代码:

device = usb.core.find(idVendor, idProduct)

device.detach_kernel_driver(0)

usb.util.claim_interface(device, 0)   
usb_cfg = device.get_active_configuration()
usb_interface = usb_cfg[(0,0)]
port_in = usb_interface[0]
port_out = usb_interface[1]

while True:
    # do read ....

如何检测设备的拔出

Hmmm当我查看github上的代码时,我建议从本期中获取代码(我假设您知道回调是什么:)

在这段代码中,您使用注册\u回调和传递的事件LIBUSB\u热插拔\u事件\u设备\u左

2018年12月5日编辑:

在pyusb的分叉存储库中有一个指向热插拔分支的链接:


是的,我也查看了提交。我认为可能有一个更简单的方法。你的链接指向一个分叉,实际问题/PR在这里-->使用
libudev
pyudev
包装器,如果是
python
)来监视枚举事件。对于实际的设备通信,您只需要
libusb