zoukankan      html  css  js  c++  java
  • Qt 自动搜索串口号列表

    @功能:
    SerialPortList 类实现当前可用的串口进行实时扫描,当发现有新的串口
    或是现有串口消失时,SerialPortList类将发出一个QStringList类型的
    信号onNewSerialPort,QStringList类型对象中存放当前可用的串口设备。

    @使用方法:
    1、Qt版本需要高于5.1以上;

    2、在新建Qt工程文件中时需要加入QSerialPort模块;即:在xxx.pro文件
       中加入 Qt += serialport

    3、在应用程序中定义一个曹,用于响应SerialPortList信号,获取串口列表
       void onNewPortList(QStringList portName){
        ui->comboBoxComList->clear();
        ui->comboBoxComList->addItems(portName);
       }

    4、在应用程序中构建SerialProt对象,连接onNewSerialPort信号:
      portList = new SerialPortList(200);
      connect(portList, SIGNAL(onNewSerialPort(QStringList)),
              this,     SLOT(onNewPortList(QStringList)));

    5、开启扫描
      portList->ScanStart();

    serialPortList.h

    #ifndef SERIALPORTLIST_H
    #define SERIALPORTLIST_H
    
    #include <QTimer>
    #include <QDebug>
    #include <QSerialPort>
    #include <QStringList>
    #include <QSerialPortInfo>
    
    /* @交流:
     * worldsing.cnblogs.com
     *
     * @功能:
     * SerialPortList 类实现当前可用的串口进行实时扫描,当发现有新的串口
     * 或是现有串口消失时,SerialPortList类将发出一个QStringList类型的
     * 信号onNewSerialPort,QStringList类型对象中存放当前可用的串口设备。
     *
     * @使用方法:
     * 1、Qt版本需要高于5.1以上;
     *
     * 2、在新建Qt工程文件中时需要加入QSerialPort模块;即:在xxx.pro文件
     *    中加入 Qt += serialport
     *
     * 3、在应用程序中定义一个曹,用于响应SerialPortList信号,获取串口列表
     *    void onNewPortList(QStringList portName){
     *     ui->comboBoxComList->clear();
     *     ui->comboBoxComList->addItems(portName);
     *    }
     *
     * 4、在应用程序中构建SerialProt对象,连接onNewSerialPort信号:
     *   portList = new SerialPortList(200);
     *   connect(portList, SIGNAL(onNewSerialPort(QStringList)),
     *           this,     SLOT(onNewPortList(QStringList)));
     *
     * 5、开启扫描
     *   portList->ScanStart();
     */
    
    #define  DEBUG_INFOR_EN   0
    
    
    class SerialPortList : public QObject
    {
        Q_OBJECT
    
    public:
        SerialPortList();
        SerialPortList(quint16);
        ~SerialPortList();
    
        quint16 scanCycleMs;
        QTimer *timer;
        QStringList oldPortStringList;
    
        void ScanStart();
        void ScanStop();
    
    public slots:
        void onTimeOut();
    
    signals:
        void onNewSerialPort(QStringList);
    };
    
    #endif // SERIALPORTLIST_H

    serialPortList.cpp

    #include "serialportlist.h"
    
    //默认构造
    SerialPortList::SerialPortList()
    {
        timer = new QTimer;
        scanCycleMs = 200;
        oldPortStringList.clear();
        connect(timer, SIGNAL(timeout()), this, SLOT(onTimeOut()));
    }
    
    //自定义扫描周期构造
    SerialPortList::SerialPortList(quint16 CycleMs)
    {
        timer = new QTimer;
        scanCycleMs = CycleMs;
        oldPortStringList.clear();
        connect(timer, SIGNAL(timeout()), this, SLOT(onTimeOut()));
    }
    
    SerialPortList::~SerialPortList()
    {
        delete timer;
    }
    
    //开始扫描
    void SerialPortList::ScanStart()
    {
        timer->stop();
        timer->start(scanCycleMs);
    }
    
    //停止扫描
    void SerialPortList::ScanStop(){
        timer->stop();
    }
    
    //周期扫描服务
    void SerialPortList::onTimeOut(){
    
        QStringList newPortStringList;
        //搜索串口
        foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){
          #if DEBUG_INFOR_EN
            qDebug() << "Name        : " << info.portName();
            qDebug() << "Description : " << info.description();
            qDebug() << "Manufacturer: " << info.manufacturer();
          #endif
          newPortStringList += info.portName();
        }
    
        //更新旧的串口列表
        if(newPortStringList.size() != oldPortStringList.size())
        {
            oldPortStringList = newPortStringList;
            emit onNewSerialPort(oldPortStringList);
        }
    }
  • 相关阅读:
    winForm ComboBox 控件默认值绑定及只可选择不可输入设定处理
    [c#]CacheHelper缓存类
    access数据库用sql语句添加字段,修改字段,删除字段
    35岁前程序员要规划好的四件事
    C#将网页内容转换成图片保存到本地( webbrowser 可应用于B/S结构中)
    SQL中返回刚插入记录的ID
    JIRA破解
    C#数组查找与排序
    最好的缺陷管理软件下载及破解Jira3.10 Enterprise Edition
    sql2000数据库 sql语句C#分页类代码
  • 原文地址:https://www.cnblogs.com/worldsing/p/3944806.html
Copyright © 2011-2022 走看看