一、描述
使用静态函数生成 QSerialPortInfo 对象列表。列表中的每个 QSerialPortInfo 对象代表一个串行端口,可以查询端口名称、系统位置、描述和制造商。QSerialPortInfo 类还可以用作 QSerialPort 类的 setPort() 方法的输入参数。
二、Qt Example
#include <QCoreApplication>#include <QSerialPortInfo>#include <QTextStream>int main(int argc, char* argv[]){QCoreApplication coreApplication(argc, argv);QTextStream out(stdout);const auto serialPortInfos = QSerialPortInfo::availablePorts();out << "Total number of ports available: " << serialPortInfos.count() << Qt::endl;for (const QSerialPortInfo& serialPortInfo : serialPortInfos) {// 返回串行端口的名称。out << "Port: " << serialPortInfo.portName() << Qt::endl;// 返回串口的系统位置out << "Location: " << serialPortInfo.systemLocation() << Qt::endl;// 返回串行端口的描述字符串(如果可用);否则返回空字符串。out << "Description: " << serialPortInfo.description() << Qt::endl;// 返回串行端口的制造商字符串(如果可用);否则返回空字符串。out << "Manufacturer: " << serialPortInfo.manufacturer() << Qt::endl;// 返回串口的序列号字符串(如果可用);否则返回空字符串。out << "Serial number: " << serialPortInfo.serialNumber() << Qt::endl;// 返回串行端口的16位供应商编号(如果可用);否则返回零。if (serialPortInfo.hasProductIdentifier()) { // 16->16进制out << "roduct Identifier: "<< QByteArray::number(serialPortInfo.vendorIdentifier(), 16) << Qt::endl;}// 如果存在有效的 16 位产品编号,则返回 true;否则返回falseif (serialPortInfo.hasProductIdentifier()) { // 16->16进制out << "roduct Identifier: "<< QByteArray::number(serialPortInfo.productIdentifier(), 16) << Qt::endl;}}return 0;}
三、参考链接
例子 https://doc.qt.io/qt-5/qtserialport-cenumerator-example.html QSerialPortInfo文档 https://doc.qt.io/qt-5/qserialportinfo.html
