Qt串行通信

Qt serial communication

本文关键字:通信 Qt      更新时间:2023-10-16

我刚刚尝试了以下代码。但是老虎机功能不起作用。连接正常,我通过 qDubug 发现了它。控制台输出如下所示。

[ZDSGuard] 32 DllMain hook strProductName2 : C:qt_examplebuild- 
serial_test-Desktop_Qt_5_13_1_MinGW_32_bit-Debugdebugserial_test.exe-1
ddd
ss
ccc

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

如果有人发现问题所在,请告诉我。 提前谢谢。

[ZDSGuard] 32 DllMain hook strProductName2 : C:qt_examplebuild- 
serial_test-Desktop_Qt_5_13_1_MinGW_32_bit-Debugdebugserial_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(。