簡體   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