QSerialPortInfo Class

提供有关现有串行端口的信息。更多

头文件: #include <QSerialPortInfo>
CMake: find_package(Qt6 REQUIRED COMPONENTS SerialPort)
target_link_libraries(mytarget PRIVATE Qt6::SerialPort)
qmake: QT += serialport

公共函数

QSerialPortInfo()
QSerialPortInfo(const QSerialPort &port)
QSerialPortInfo(const QString &name)
QSerialPortInfo(const QSerialPortInfo &other)
~QSerialPortInfo()
QString description() const
bool hasProductIdentifier() const
bool hasVendorIdentifier() const
bool isNull() const
QString manufacturer() const
QString portName() const
quint16 productIdentifier() const
QString serialNumber() const
void swap(QSerialPortInfo &other)
QString systemLocation() const
quint16 vendorIdentifier() const
QSerialPortInfo &operator=(const QSerialPortInfo &other)

静态公共成员

QList<QSerialPortInfo> availablePorts()
QList<qint32> standardBaudRates()

详细说明

使用静态availablePorts() 函数生成 QSerialPortInfo 对象列表。列表中的每个 QSerialPortInfo 对象代表一个串行端口,可查询port name,system location,description,manufacturer 和其他一些硬件参数。QSerialPortInfo 类还可用作QSerialPort 类的setPort() 方法的输入参数。

使用示例

示例代码枚举了所有可用的串行端口,并将其参数打印到控制台:

   const autoserialPortInfos=QSerialPortInfo::availablePorts();for(constQSerialPortInfo&portInfo: serialPortInfos) {        qDebug() << "\n"
                << "Port:"<<portInfo.portName()<<"\n"<< "Location:"<<portInfo.systemLocation()<<"\n"<< "Description:"<<  portInfo. description()<<"\n"<< "Manufacturer:"<<portInfo.manufacturer()<< "\n"<< "Serial number:"<<portInfo.serialNumber()<< "\n"<< "Vendor Identifier:"<<(portInfo.hasVendorIdentifier()?QByteArray::number(portInfo.vendorIdentifier(), 16) : QByteArray())<< "\n"<< "Product Identifier:"<<(portInfo.hasProductIdentifier()?QByteArray::number(portInfo.productIdentifier(), 16) : QByteArray()); }

Linux 上的端口枚举

默认情况下,Linux 使用libudev 枚举可用的串行端口。如果该库不可用,则会退回到/sys/class/tty 目录中读取文件。

众所周知,某些版本的libudev 存在错误,会错误报告 USB 集线器的 VID 和 PID,而不是实际设备的 VID 和 PID。在这种情况下,请定义QT_SERIALPORT_SKIP_UDEV_LOOKUP 环境变量,跳过libudev 查找,只使用/sys/class/tty 目录中的信息。

另请参阅 QSerialPort

成员函数文档

QSerialPortInfo::QSerialPortInfo()

构造一个空的 QSerialPortInfo 对象。

另请参阅 isNull().

[explicit] QSerialPortInfo::QSerialPortInfo(const QSerialPort &port)

从串行port 中构造一个 QSerialPortInfo 对象。

[explicit] QSerialPortInfo::QSerialPortInfo(const QString &name)

从串行端口name 构造一个 QSerialPortInfo 对象。

该构造函数根据端口名称name 从可用的串行端口中找到相关串行端口,并为该端口构造串行端口信息实例。

QSerialPortInfo::QSerialPortInfo(const QSerialPortInfo &other)

构造other 的副本。

[noexcept] QSerialPortInfo::~QSerialPortInfo()

销毁QSerialPortInfo 对象。对象中的值引用将失效。

[static] QList<QSerialPortInfo> QSerialPortInfo::availablePorts()

返回系统中可用串行端口的列表。

另请参阅 Port enumeration on Linux

QString QSerialPortInfo::description() const

返回串行端口的描述字符串(如果有),否则返回空字符串。

另请参阅 manufacturer() 和serialNumber()。

bool QSerialPortInfo::hasProductIdentifier() const

如果存在有效的16-bit 产品编号,则返回true ;否则返回false

另请参阅 productIdentifier()、vendorIdentifier() 和hasVendorIdentifier()。

bool QSerialPortInfo::hasVendorIdentifier() const

如果存在有效的16-bit 供应商编号,则返回true ;否则返回false

另请参阅 vendorIdentifier()、productIdentifier() 和hasProductIdentifier()。

bool QSerialPortInfo::isNull() const

返回QSerialPortInfo 对象是否包含串行端口定义。

QString QSerialPortInfo::manufacturer() const

返回串行端口的制造商字符串(如果有),否则返回空字符串。

另请参阅 description() 和serialNumber()。

QString QSerialPortInfo::portName() const

返回串行端口的名称。

另请参阅 systemLocation()。

quint16 QSerialPortInfo::productIdentifier() const

返回串行端口的 16 位产品编号(如果有),否则返回 0。

另请参阅 hasProductIdentifier()、vendorIdentifier() 和hasVendorIdentifier()。

QString QSerialPortInfo::serialNumber() const

返回串行端口的序列号字符串(如果有),否则返回空字符串。

注意: 序列号可能包括字母。

另请参阅 description() 和manufacturer()。

[static] QList<qint32> QSerialPortInfo::standardBaudRates()

返回目标平台支持的可用标准波特率列表。

void QSerialPortInfo::swap(QSerialPortInfo &other)

QSerialPortInfo other 与此QSerialPortInfo 互换。这种操作速度非常快,而且从未出现过故障。

QString QSerialPortInfo::systemLocation() const

返回串行端口的系统位置。

另请参见 portName()。

quint16 QSerialPortInfo::vendorIdentifier() const

返回串行端口的 16 位供应商编号(如果有),否则返回 0。

另请参阅 hasVendorIdentifier()、productIdentifier() 和hasProductIdentifier()。

QSerialPortInfo &QSerialPortInfo::operator=(const QSerialPortInfo &other)

设置QSerialPortInfo 对象等于other

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.