如何检测在python上连接了新的USB设备 [英] How to detect a new usb device is connected on python
问题描述
我想做一些在后台运行的东西,只有在计算机检测到新设备连接后,其余的代码才会运行,有没有什么优雅的方法来做这样的事情?
这取决于操作系统
在 linux 中你可以使用 pyudev
:
几乎公开了完整的 libudev 功能.您可以:
- 枚举设备,按特定条件过滤 (pyudev.Context)
- 查询设备信息、属性和属性,
- 监控设备,与后台线程同步和异步,或在 Qt 的事件循环内(pyudev.pyqt4,pyudev.pyside)、glib (pyudev.glib) 和 wxPython (pyudev.wx).
https://pyudev.readthedocs.io/en/latest/>
源代码在http://pyudev.readthedocs.io/en/v0.14/api/monitor.html,查看receive_device()
函数
在 Windows 中,您可以使用 WMI(Windows 管理规范),例如 https://blogs.msdn.microsoft.com/powershell/2007/02/24/displaying-usb-devices-using-wmi/ ( Python 读取设备管理器信息 ) 或像 https://pypi.python.org/pypi/infi.devicemanager
I want to make something which will run on the background and only after the computer detect new device is connected the rest of the code will run, is there any elegant way to do such a thing?
this is operating system dependent
in linux you can use pyudev
for this :
Almost the complete libudev functionality is exposed. You can:
- Enumerate devices, filtered by specific criteria (pyudev.Context)
- Query device information, properties and attributes,
- Monitor devices, both synchronously and asynchronously with background threads, or within the event loops of Qt (pyudev.pyqt4, pyudev.pyside), glib (pyudev.glib) and wxPython (pyudev.wx).
https://pyudev.readthedocs.io/en/latest/
source code is in http://pyudev.readthedocs.io/en/v0.14/api/monitor.html, see the receive_device()
function
in windows you can use the WMI ( Windows Management Instrumentation ) like in https://blogs.msdn.microsoft.com/powershell/2007/02/24/displaying-usb-devices-using-wmi/ ( Python Read the Device Manager Information ) or a python binding like in https://pypi.python.org/pypi/infi.devicemanager
这篇关于如何检测在python上连接了新的USB设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!