日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Qt5.8】Qt5.8中串口信息类QSerialPortInfo

發布時間:2024/4/21 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Qt5.8】Qt5.8中串口信息类QSerialPortInfo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

00. 目錄

    • 00. 目錄
    • 01. QSerialPortInfo簡介
    • 02. QSerialPortInfo類成員函數
    • 03. 程序示例一
    • 04. 程序示例二

01. QSerialPortInfo簡介

QSerialPortInfo類提供已存在串口設備的信息。使用QSerialPortInfo類的靜態成員函數生成QSerialPortInfo對象的鏈表。鏈表中的每個QSerialPortInfo對象代表一個串口,每個串口可以使用端口名、系統定位、描述、制造商查詢。QSerialPortInfo類對象也可以用做QSerialPort類的setPort()成員函數的參數。

02. QSerialPortInfo類成員函數

//構造函數 QSerialPortInfo() QSerialPortInfo(const QSerialPort &port) QSerialPortInfo(const QString &name) QSerialPortInfo(const QSerialPortInfo &other)//析構函數 ~QSerialPortInfo()//返回當前系統可用串口的鏈表 [static] QList<QSerialPortInfo> QSerialPortInfo::availablePorts()//如果串口可用,返回串口的描述信息 QString QSerialPortInfo::description() const//如果有一個合法的16位生產碼,返回true bool QSerialPortInfo::hasProductIdentifier() const//如果有一個合法的16位制造商編碼,返回true bool QSerialPortInfo::hasVendorIdentifier() const//如果串口當前正忙,返回true bool QSerialPortInfo::isBusy() const//如果串口可用,返回串口的制造商的名字 QString QSerialPortInfo::manufacturer() const//返回串口的名字 QString QSerialPortInfo::portName() const//如果串口可用,返回串口的16位的生產編碼 quint16 QSerialPortInfo::productIdentifier() const//如果串口可用,返回串口的序列號 QString QSerialPortInfo::serialNumber() const//返回目標平臺支持的可用的標準波特率的鏈表 [static] QList<qint32> QSerialPortInfo::standardBaudRates()//使用other交換QSerialPortInfo對象 void QSerialPortInfo::swap(QSerialPortInfo &other)//返回串口的系統位置 QString QSerialPortInfo::systemLocation() const//如果串口可用,返回16位的制造商編碼 quint16 QSerialPortInfo::vendorIdentifier() const

03. 程序示例一

#include "widget.h" #include <QDebug> #include <QList> #include <QtSerialPort/QSerialPortInfo>Widget::Widget(QWidget *parent): QWidget(parent) {//獲取當前系統下所有可以用的串口QList<QSerialPortInfo> serialPortInfo = QSerialPortInfo::availablePorts();qDebug() << "串口的個數: " << serialPortInfo.count();//顯示目標串口支持的波特率列表QList<qint32> baudRates = QSerialPortInfo::standardBaudRates();qDebug() << baudRates;qDebug() << "串口的描述:" << serialPortInfo.at(0).description();qDebug() << "hasProductIdentifier(): " << serialPortInfo.at(0).hasProductIdentifier();qDebug() << "hasVendorIdentifier(): " << serialPortInfo.at(0).hasVendorIdentifier();qDebug() << "isBusy: " << serialPortInfo.at(0).isBusy();qDebug() << "manufacturer: " << serialPortInfo.at(0).manufacturer();qDebug() << "portName: " << serialPortInfo.at(0).portName();qDebug() << "productIdentifier: " << serialPortInfo.at(0).productIdentifier();qDebug() << "serialNumber: " << serialPortInfo.at(0).serialNumber();qDebug() << "vendorIdentifier: " << serialPortInfo.at(0).vendorIdentifier();qDebug() << "systemLocation: " << serialPortInfo.at(0).systemLocation();}Widget::~Widget() {}

運行結果:

04. 程序示例二

int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QTextStream out(stdout);const auto serialPortInfos = QSerialPortInfo::availablePorts();out << QObject::tr("Total number of ports available: ") << serialPortInfos.count() << endl;const QString blankString = QObject::tr("N/A");QString description;QString manufacturer;QString serialNumber;for (const QSerialPortInfo &serialPortInfo : serialPortInfos) {description = serialPortInfo.description();manufacturer = serialPortInfo.manufacturer();serialNumber = serialPortInfo.serialNumber();out << endl<< QObject::tr("Port: ") << serialPortInfo.portName() << endl<< QObject::tr("Location: ") << serialPortInfo.systemLocation() << endl<< QObject::tr("Description: ") << (!description.isEmpty() ? description : blankString) << endl<< QObject::tr("Manufacturer: ") << (!manufacturer.isEmpty() ? manufacturer : blankString) << endl<< QObject::tr("Serial number: ") << (!serialNumber.isEmpty() ? serialNumber : blankString) << endl<< QObject::tr("Vendor Identifier: ") << (serialPortInfo.hasVendorIdentifier() ? QByteArray::number(serialPortInfo.vendorIdentifier(), 16) : blankString) << endl<< QObject::tr("Product Identifier: ") << (serialPortInfo.hasProductIdentifier() ? QByteArray::number(serialPortInfo.productIdentifier(), 16) : blankString) << endl<< QObject::tr("Busy: ") << (serialPortInfo.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) << endl;}return 0; }

運行結果:

總結

以上是生活随笔為你收集整理的【Qt5.8】Qt5.8中串口信息类QSerialPortInfo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。