sackratte
sackratte

Reputation: 11

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.

Upvotes: 1

Views: 2579

Answers (1)

Take_Care_
Take_Care_

Reputation: 2154

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

Upvotes: 2

Related Questions