目前我获取的是window系统上的串口信息,可以使用Qt Serial Port模块来获取系统的串口。
Qt的帮助文档:
在项目文件(.pro)里包含serialport模块
QT += serialport
以下方法来获取系统的串口列表
#include <QSerialPortInfo>
#include <QDebug>
shangwei::shangwei(QWidget *parent)
: QWidget(parent)
, ui(new Ui::shangwei)
{
ui->setupUi(this);
//获取win系统串口——————————
QList<QSerialPortInfo> winSerialList = QSerialPortInfo::availablePorts();
QStringList wSerialPNameList ;
QStringList wDescription;
QStringList wManufacturer;
QStringList wSerialNumber;
QStringList wSystemLocation;
/* foreach (声明变量, 容器) {}:声明变量是遍历过程中每次使用的变量
**QSerialPortInfo &QSerialPortInfo::operator=(const QSerialPortInfo &other)
* Sets the QSerialPortInfo object to be equal to other.将QSerialPortnfo对象设置为等于其他对象。
* 我的理解是将QSerialPortnfo对象的地址记录到info里,使用foreach时就用info指向QSerialPortnfo对象并调用函数方法
*/
foreach(const QSerialPortInfo &info,winSerialList){
wSerialPNameList<<info.portName();
wDescription<<info.description();
wManufacturer<<info.manufacturer();
wSerialNumber<<info.serialNumber();
wSystemLocation<<info.systemLocation();
}
qDebug()<<wSerialPNameList;
qDebug()<<"————";
qDebug()<<wDescription;
qDebug()<<"————";
qDebug()<<wManufacturer;
qDebug()<<"————";
qDebug()<<wSerialNumber;
qDebug()<<"————";
qDebug()<<wSystemLocation;
/*为什么直接打印会出错:debug<<winSerialList;?
因为传递的参数(const QSerialPortInfo)与该方法或者函数定义的参数类型不匹配
no match for 'operator<<' (operand types are 'QDebug' and 'const QSerialPortInfo')
*/
}
PC端串口信息
代码输出结果