if __name__ == "__main__": app = QApplication(sys.argv)
mwin = MainWindow() mwin.show()
sys.exit(app.exec_())
扫描当前电脑USB HID设备
1 2 3 4 5 6 7 8 9 10 11 12 13 14
def__init__(self): ...
devices = hid.enumerate()
index = 0 for devinfo in devices: product_str = devinfo['product_string'] manu_str = devinfo['manufacturer_string'] vid = devinfo['vendor_id'] pid = devinfo['product_id']
if product_str != '': self.comboPort.addItem(product_str + f' by {manu_str}')
index = 0 for devinfo in devices: ... self.comboPort.addItem(product_str + f' by {manu_str}') self.comboPort.setItemData(index,pid,Qt.UserRole+1) self.comboPort.setItemData(index,vid,Qt.UserRole+2) index +=1
self.btnConnect.clicked.connect(self.open_device)
defopen_device(self): pid = self.comboPort.itemData(self.comboPort.currentIndex(),Qt.UserRole+1) vid = self.comboPort.itemData(self.comboPort.currentIndex(),Qt.UserRole+2)
if self.btnConnect.text == "连接": self.btnConnect.setText("断开") self.dev.open(vid,pid) else: self.btnConnect.setText("连接") self.dev.close()