Qt5列出并检测可用串口号及其他信息

来源:互联网 发布:淘宝可以用黑体吗 编辑:程序博客网 时间:2024/06/05 20:29

首先QT5的项目文件是:

greaterThan(QT_MAJOR_VERSION, 4) {
    QT       += widgets serialport
} else {
    include($$QTSERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf)
}
TARGET = enumerator
TEMPLATE = app
SOURCES += \
    main.cpp


然后main.cpp是:



#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QScrollArea>
#include <QtSerialPort/QSerialPortInfo>


QT_USE_NAMESPACE


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    QVBoxLayout *layout = new QVBoxLayout;


    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        QString s = QObject::tr("Port: ") + info.portName() + "\n"
                    + QObject::tr("Location: ") + info.systemLocation() + "\n"
                    + QObject::tr("Description: ") + info.description() + "\n"
                    + QObject::tr("Manufacturer: ") + info.manufacturer() + "\n"
                    + QObject::tr("Serial number: ") + info.serialNumber() + "\n"
                    + QObject::tr("Vendor Identifier: ") + (info.hasVendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString()) + "\n"
                    + QObject::tr("Product Identifier: ") + (info.hasProductIdentifier() ? QString::number(info.productIdentifier(), 16) : QString()) + "\n"
                    + QObject::tr("Busy: ") + (info.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) + "\n";


        QLabel *label = new QLabel(s);
        layout->addWidget(label);
    }


    QWidget *workPage = new QWidget;
    workPage->setLayout(layout);


    QScrollArea area;
    area.setWindowTitle(QObject::tr("Info about all available serial ports."));
    area.setWidget(workPage);
    area.show();


    return a.exec();
}

0 0