求助Qt中QserialPort对象无法发出readyRead信号问题

求助Qt中QserialPort对象无法发出readyRead信号问题

源码地址:https://github.com/76312/QSerialPort

mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include 
#include 
#include 
#include 

QT_BEGIN_NAMESPACE
namespace Ui { class MyWidget; }
QT_END_NAMESPACE

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    MyWidget(QWidget *parent = nullptr);
    ~MyWidget();


private slots:
    void on_btn_open_clicked();
    void received_data();
private:
    Ui::MyWidget *ui;
    QSerialPort *serial;
};
#endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h"
#include "ui_mywidget.h"

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("串口调试助手");
    serial = new QSerialPort();

    connect(serial,&QSerialPort::readyRead,this,&MyWidget::received_data);
    QString description;
    QString manufacturer;
    QString serialNumber;
    QList<QSerialPortInfo> serialPortInfos = QSerialPortInfo::availablePorts();

    //将所有可以使用的串口设备添加到ComboBox中
    for (const QSerialPortInfo &serialPortInfo : serialPortInfos)
    {
        ui->serialport_name->addItem(serialPortInfo.portName());
    }

    //设置波特率
    ui->comboBox_baudRate->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
    ui->comboBox_baudRate->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
    ui->comboBox_baudRate->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
    ui->comboBox_baudRate->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
    ui->comboBox_baudRate->setCurrentIndex(3);

    //设置数据位
    ui->comboBox_dataBits->addItem(QStringLiteral("5"), QSerialPort::Data5);
    ui->comboBox_dataBits->addItem(QStringLiteral("6"), QSerialPort::Data6);
    ui->comboBox_dataBits->addItem(QStringLiteral("7"), QSerialPort::Data7);
    ui->comboBox_dataBits->addItem(QStringLiteral("8"), QSerialPort::Data8);
    ui->comboBox_dataBits->setCurrentIndex(3);

    //设置奇偶校验位
    ui->comboBox_parity->addItem(tr("None"), QSerialPort::NoParity);
    ui->comboBox_parity->addItem(tr("Even"), QSerialPort::EvenParity);
    ui->comboBox_parity->addItem(tr("Odd"), QSerialPort::OddParity);
    ui->comboBox_parity->addItem(tr("Mark"), QSerialPort::MarkParity);
    ui->comboBox_parity->addItem(tr("Space"), QSerialPort::SpaceParity);

    //设置停止位
    ui->comboBox_stopBit->addItem(QStringLiteral("1"), QSerialPort::OneStop);
    ui->comboBox_stopBit->addItem(QStringLiteral("2"), QSerialPort::TwoStop);

    ui->btn_send->setEnabled(false);
}

MyWidget::~MyWidget()
{
    delete ui;
}


void MyWidget::on_btn_open_clicked()
{
    //设置串口名字
    serial->setPortName(ui->serialport_name->currentText());

    //设置波特率
    serial->setBaudRate(ui->comboBox_baudRate->currentText().toInt());
    qDebug()<<serial->baudRate();
    //设置数据位
    serial->setDataBits(QSerialPort::Data8);

    //设置奇偶校验位
    serial->setParity(QSerialPort::NoParity);

    //设置停止位
    serial->setStopBits(QSerialPort::OneStop);

    //设置流控
    serial->setFlowControl(QSerialPort::NoFlowControl);

    //打开串口
    if (!serial->isOpen())
    {
        serial->open(QIODevice::ReadWrite);
        serial->setDataTerminalReady(true);
        ui->comboBox_baudRate->setEnabled(false);
        ui->comboBox_dataBits->setEnabled(false);
        ui->comboBox_parity->setEnabled(false);
        ui->serialport_name->setEnabled(false);
        ui->comboBox_stopBit->setEnabled(false);

        ui->btn_send->setEnabled(true);

        ui->btn_open->setText("关闭串口");

        //信号与槽函数关联
        if (serial->isOpen()) qDebug()<<"打开串口";
    }

    else
    {
        //关闭串口
        //serial->clear();

        serial->close();

        //serial->deleteLater();

        //恢复设置功能
        ui->comboBox_baudRate->setEnabled(true);
        ui->comboBox_dataBits->setEnabled(true);
        ui->comboBox_parity->setEnabled(true);
        ui->serialport_name->setEnabled(true);
        ui->comboBox_stopBit->setEnabled(true);

        ui->btn_open->setText("打开串口");
        ui->btn_send->setEnabled(false);

       if (!serial->isOpen()) qDebug()<<"关闭串口";

    }

}


//串口接收到数据
void MyWidget::received_data()
{
    qDebug()<<"接收数据";
    static int reNum = 0;
    QByteArray buf;
    buf = serial->readAll();

    if (!buf.isEmpty())
    {

        reNum += buf.size();
        QString myStrTemp = QString::fromLocal8Bit(buf); //支持中文显示
        QString str = ui->textEdit_recv->toPlainText();
        str += myStrTemp;
        ui->textEdit_recv->clear();
        ui->textEdit_recv->append(str);

    }
    buf.clear();
}

main文件

#include "mywidget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication a(argc, argv);
    MyWidget w;
    w.show();
    return a.exec();
}

Qt工程文件

QT       += core gui serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mywidget.cpp

HEADERS += \
    mywidget.h

FORMS += \
    mywidget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

你可能感兴趣的:(求助Qt中QserialPort对象无法发出readyRead信号问题)