pyqt5 implements hid hotplug auto detect in Windows OS
before using this code should install below package:
pip --install hidapi
pip --install PyQt5
# author: yifei.su
# date: 2023-11-13
import sys
import ctypes
from ctypes.wintypes import MSG
import ctypes.wintypes as wintypes
import hid
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
NULL = 0
INVALID_HANDLE_VALUE = -1
DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000
WM_DEVICECHANGE = 0x0219
DBT_DEVTYP_DEVICEINTERFACE = 5
DBT_DEVICEREMOVECOMPLETE = 0x8004
DBT_DEVICEARRIVAL = 0x8000
user32 = ctypes.windll.user32
RegisterDeviceNotification = user32.RegisterDeviceNotificationW
UnregisterDeviceNotification = user32.UnregisterDeviceNotification
class GUID(ctypes.Structure):
_pack_ = 1
_fields_ = [("Data1", ctypes.c_ulong),
("Data2", ctypes.c_ushort),
("Data3", ctypes.c_ushort),

本文介绍了如何使用Python的PyQt5库结合hidapi在Windows系统中实现实时监控USB设备的插入和移除事件。作者通过注册设备通知,监听WM_DEVICECHANGE消息来检测特定设备的连接状态。
最低0.47元/天 解锁文章
664

被折叠的 条评论
为什么被折叠?



