开发平台:Qt 6.3
编译环境:MinGW 11.2.0 64bit
源码免费下载方式在文末!部分功能参照源码学习使用!
一、继承QSerialPort类创建子类Serial(serial.h)
#ifndef SERIAL_H#define SERIAL_H#include <QObject>#include <QtSerialPort/QSerialPort>#include <QtSerialPort/QSerialPortInfo>#include <QStringList>#include <QString>#include <QByteArray>class Serial : public QSerialPort{Q_OBJECTpublic:explicit Serial();// 数据缓存区QByteArray dataBuf;// 串口描述内容QStringList serialDescription;// 串口扫描函数QStringList serialScan(void);bool serialOpen(QString serialName, int baudRate);void ReadData();void sendData(QByteArray sendData);void serialClose();signals:void readSignal(void);public slots:};#endif // SERIAL_H
二、实现串口打开,扫描,关闭和读写函数(serial.cpp)
#include "serial.h"#include <QDebug>Serial::Serial(){}QStringList Serial::serialScan(void){QStringList serialPortList;serialDescription.clear(); // 每次扫描先清除串口描述信息foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {this->setPort(info);serialPortList.append(this->portName());serialDescription.append(info.description());//qDebug() << this->portName();//qDebug() << info.description();}return serialPortList;}bool Serial::serialOpen(QString serialName, int baudRate){this->setPortName(serialName);if(this->open(QIODeviceBase::ReadWrite)) {this->setBaudRate(baudRate);this->setDataBits(QSerialPort::Data8);this->setParity(QSerialPort::NoParity);this->setStopBits(QSerialPort::OneStop);this->setFlowControl(QSerialPort::NoFlowControl);// 下位机发送数据会响应这个槽函数connect(this, &QSerialPort::readyRead, this, &Serial::ReadData);// 下位机发送数据会触发这个信号connect(this, &QSerialPort::readyRead, this, &Serial::readSignal);return true;}return false;}// 读数据void Serial::ReadData(){dataBuf = this->readAll();}// 写数据void Serial::sendData(QByteArray sendData){this->write(sendData);}// 关闭串口void Serial::serialClose(){this->clear();this->close();}
三、创建windows事件过滤类,重写QAbstractNativeEventFilter类下的虚函数(SerialEvent.h)
#ifndef SERIALEVENT_H#define SERIALEVENT_H#include <QObject>#include <QAbstractNativeEventFilter>#include <QDebug>#include <windows.h>#include <dbt.h>class SerialEvent : public QObject, public QAbstractNativeEventFilter{Q_OBJECTpublic:bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result){(void)result;if(eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG"){MSG* msg = reinterpret_cast<MSG*>(message);int msgType = msg->message;if(msgType == WM_DEVICECHANGE){PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;switch (msg->wParam){case DBT_DEVICEARRIVAL:// qDebug() << "DBT_DEVICEARRIVAL:" << lpdb->dbch_devicetype;if(lpdb->dbch_devicetype == DBT_DEVTYP_PORT){PDEV_BROADCAST_PORT lpdbv = (PDEV_BROADCAST_PORT)lpdb;QString portName = QString::fromWCharArray(lpdbv->dbcp_name);// qDebug() << "device arrival:" << portName;emit comDevArriaval(portName);}break;case DBT_DEVICEREMOVECOMPLETE:// qDebug() << "DBT_DEVICEREMOVECOMPLETE:" << lpdb->dbch_devicetype;if(lpdb->dbch_devicetype == DBT_DEVTYP_PORT){PDEV_BROADCAST_PORT lpdbv = (PDEV_BROADCAST_PORT)lpdb;QString portName = QString::fromWCharArray(lpdbv->dbcp_name);// qDebug() << "device remove complete:" << portName;emit comDevRemoveComplete(portName);}break;case DBT_DEVNODES_CHANGED:break;default:break;}}}return false;}signals:// 串口插入了void comDevArriaval(QString devName);//串口拔掉了void comDevRemoveComplete(QString devName);};#endif // SERIALEVENT_H
四、 在应用程序中注册 a.installNativeEventFilter(w.mySerialEvent);并连接信号与处理函数(Widget.cpp)
// 串口插入处理connect(mySerialEvent, &SerialEvent::comDevArriaval, this , [=](){ui->serial_port_C->clear();serialNameList = serial_main->serialScan();ui->serial_port_C->addItems(serialNameList);});// 串口拔出处理connect(mySerialEvent, &SerialEvent::comDevRemoveComplete, this , [=](QString devName){// 打开串口时串口变化 如果处于打开状态的端口被拔出则重置串口状态if(!serial_ONOFF_P_flag){if(ui->serial_port_C->currentText() == devName){ui->serial_ONOFF_P->setText("打开串口");ui->serial_ONOFF_L->setPixmap(QPixmap(":/IMG/OFF.png"));serial_ONOFF_P_flag = true;serial_main->close();this->setComboboxStatus(false);QMessageBox::warning(this, "串口连接错误", "<font size=\"4\">当前串口已关闭</font>");}}ui->serial_port_C->clear(); // 清空串口复选框serialNameList = serial_main->serialScan(); // 扫描串口ui->serial_port_C->addItems(serialNameList); // 添加串口名});
界面仍采用的32位版本界面!
64位版本源码下载地址:/download/m0_50669075/85234856
32位版本源码下载地址:(102条消息) 使用Qt5UI设计开发的串口助手多功能工具源码文件和安装包_qt串口程序-QT文档类资源-CSDN文库
END