简体   繁体   中英

PyUSB: detect device unplug

I want my program to exit, whenever the device gets unplugged. My code:

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 ....

How can I detect an unplug of the device.

Hmmm as I looked over code on github, I would recomend to take code from this issue (I assume that You know what callbacks are :) )

PYUSB Pull request- Hotplug

In this code you use register_callback with passed event LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT

EDIT 5.12.2018 :

There is a link to hotplug branch at the forked repository of the pyusb :

https://github.com/roberthartung/pyusb/tree/hotplug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM