在菜单选项卡python中枚举串行端口列表

2024-09-25 10:34:55 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从qtmenuBar()子菜单中获取串行端口列表

Select Port| debug | help
  |           
list comports -com1
               com2
               com3 

我代码的一部分

^{pr2}$

我需要在“选择端口”选项卡的子菜单中获取可用com端口的列表,或者打开一个列出串行端口的新对话框


Tags: 端口代码debug列表port菜单helpselect
1条回答
网友
1楼 · 发布于 2024-09-25 10:34:55

基本上遍历一个可用端口的列表,并将它们作为一种单选按钮可检查操作或作为QComboBox添加。然后,当一个被选中时,更改您的self.portName以反映新的。在

pure Qt中用于执行此操作的示例位于终端Qt串行端口示例下的SettingsDialog。在

http://doc.qt.io/qt-5/qtserialport-terminal-settingsdialog-cpp.html

void SettingsDialog::fillPortsInfo()
{
    ui->serialPortInfoListBox->clear();
    static const QString blankString = QObject::tr("N/A");
    QString description;
    QString manufacturer;
    QString serialNumber;
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        QStringList list;
        description = info.description();
        manufacturer = info.manufacturer();
        serialNumber = info.serialNumber();
        list << info.portName()
             << (!description.isEmpty() ? description : blankString)
             << (!manufacturer.isEmpty() ? manufacturer : blankString)
             << (!serialNumber.isEmpty() ? serialNumber : blankString)
             << info.systemLocation()
             << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)
             << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);

        ui->serialPortInfoListBox->addItem(list.first(), list);
    }
}

看起来PyQt没有QtSerialPort库(可能是因为pySerial和类似的库已经可用)。在

希望有帮助。在

相关问题 更多 >