简体   繁体   English

Qt串口通讯

[英]Qt serial communication

I just tried the following code.我刚刚尝试了以下代码。 But the slot function is not working.但是插槽 function 不起作用。 The connection is OK and I found out it by qDubug.连接正常,我是通过 qDubug 发现的。 The console output is as follows.控制台output如下。

[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.如您所见, aaa没有打印出来。 If the slot function works fine, it should be printed.如果插槽 function 工作正常,则应打印。

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. QSP 在 Qt 5.13.1 中存在错误。 Use or Qt 5.13.0, or wait for a newest versions (5.13.2 / 5.12.6).使用 Qt 5.13.0,或等待最新版本 (5.13.2 / 5.12.6)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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