简体   繁体   中英

Qt serial communication

I just tried the following code. But the slot function is not working. The connection is OK and I found out it by qDubug. The console output is as follows.

[ZDSGuard] 32 DllMain hook strProductName2 : C:\qt_example\build- 
serial_test-Desktop_Qt_5_13_1_MinGW_32_bit-Debug\debug\serial_test.exe-1
ddd
ss
ccc

As you can find, aaa is not printed out. If the slot function works fine, it should be printed.

Please let me know if somebody finds out what is wrong. Thanks in advance.

[ZDSGuard] 32 DllMain hook strProductName2 : C:\qt_example\build- 
serial_test-Desktop_Qt_5_13_1_MinGW_32_bit-Debug\debug\serial_test.exe-1
ddd
ss
ccc

Serial Communication Code

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include <QLabel>


QSerialPort *serial;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    serial = new QSerialPort();
    serial->setPortName("COM4");
    serial->setBaudRate(QSerialPort::Baud115200);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->setParity(QSerialPort::NoParity);
    serial->setDataBits(QSerialPort::Data8);
    serial->setStopBits(QSerialPort::OneStop);
    if (serial->open(QIODevice::ReadWrite))
       ui->label->setText("bb");
    if (QObject::connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived())))
        qDebug()<< "ddd";
    //ui->label->setText("aa");
    qDebug() << "ss";
}

MainWindow::~MainWindow()
{

    delete ui;

    serial->close();
}

void MainWindow::serialReceived()
{
    QByteArray BA;
    BA=serial->readAll();
    ui->label->setText("aa");
    //printf(BA);
    qDebug()<<"aaa";//BA;
}

QSP has bug in Qt 5.13.1. Use or Qt 5.13.0, or wait for a newest versions (5.13.2 / 5.12.6).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM