2016-11-24 112 views
0

我想,如果設備被移除退出我PROGRAMM。 我的代碼: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 = usb_interface[1] 

while True: 
    # do read .... 

我怎樣才能檢測到設備的拔掉。

+0

使用'libudev'('pyudev'包裝以防'python')監視枚舉事件。你只需要'libusb'來進行實際的設備通信。 –

回答

0

嗯,我看了看代碼在github上,我會建議從這一承諾採取代碼(我假設你知道什麼是回調:))

PYUSB Pull request- Hotplug

在這段代碼中使用register_callback已通過事件LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT

+0

是的,我也查看了提交。我認爲可能有一個更簡單的方法。 – sackratte