繁体   English   中英

Qt串口通讯

[英]Qt serial communication

我刚刚尝试了以下代码。 但是插槽 function 不起作用。 连接正常,我是通过 qDubug 发现的。 控制台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

如您所见, aaa没有打印出来。 如果插槽 function 工作正常,则应打印。

如果有人发现出了什么问题,请告诉我。 提前致谢。

[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

串行通信代码

#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 在 Qt 5.13.1 中存在错误。 使用 Qt 5.13.0,或等待最新版本 (5.13.2 / 5.12.6)。

暂无
暂无

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

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