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"
<< "端口:" <<portInfo.portName()<< "\n"
<< "位置:" <<portInfo.systemLocation()<< "\n"
<< "描述:" <<portInfo.description()<< "\n"
<< "制造商:" <<portInfo.manufacturer()<< "\n"
<< "序列号:" <<portInfo.serialNumber()<< "\n"
<< "供应商标识符:"
<<(portInfo.hasVendorIdentifier()
?QByteArray::number(portInfo.vendorIdentifier(), 16)
: QByteArray())<< "\n"
<< "产品标识符:"
<<(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 位产品编号;否则返回零。
另请参阅 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 位厂商编号;否则返回零。
另请参阅 hasVendorIdentifier()、productIdentifier() 和hasProductIdentifier()。
QSerialPortInfo &QSerialPortInfo::operator=(const QSerialPortInfo &other)
将QSerialPortInfo 对象设置为与other 相等。
© 2026 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.