[Qt]串口发送接收

———————mainwindow.h————————————————-
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include
#include
#include “SerialPortWorker.h”

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void onSendButtonClicked();
void onDataReceived(const QByteArray &data);
void onErrorOccurred(const QString &error);

private:
Ui::MainWindow *ui;
QThread *workerThread;
SerialPortWorker *worker;
};

#endif // MAINWINDOW_H

————————————-SerialPortWorker.h——————————
#ifndef SERIALPORTWORKER_H
#define SERIALPORTWORKER_H

#include
#include
#include

class SerialPortWorker : public QObject {
Q_OBJECT

public:
explicit SerialPortWorker(const QString &portName, QObject *parent = nullptr);
~SerialPortWorker();

public slots:
void startWork();
void sendData(const QByteArray &data);

signals:
void dataReceived(const QByteArray &data);
void errorOccurred(const QString &error);

private slots:
void handleReadyRead();

private:
QSerialPort *serialPort;
QString portName;
};

#endif // SERIALPORTWORKER_H

—————————————main.cpp————————–
#include “mainwindow.h”

#include

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

——————————-mainwindow.cpp—————————-

#include “mainwindow.h”
#include “ui_mainwindow.h”
#include
#include

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow), workerThread(new QThread), worker(nullptr)
{
ui->setupUi(this);

worker = new SerialPortWorker(“/dev/ttyUSB0”); // 使用实际串口名称
worker->moveToThread(workerThread);

connect(workerThread, &QThread::started, worker, &SerialPortWorker::startWork);
connect(worker, &SerialPortWorker::dataReceived, this, &MainWindow::onDataReceived);
connect(worker, &SerialPortWorker::errorOccurred, this, &MainWindow::onErrorOccurred);
connect(ui->sendButton, &QPushButton::clicked, this, &MainWindow::onSendButtonClicked);

workerThread->start();
}

MainWindow::~MainWindow() {
workerThread->quit();
workerThread->wait();
delete ui;
}

void MainWindow::onSendButtonClicked() {
QByteArray data = ui->dataInput->text().toUtf8();
worker->sendData(data);
}

void MainWindow::onDataReceived(const QByteArray &data) {
ui->dataOutput->append(data);
}

void MainWindow::onErrorOccurred(const QString &error) {
qDebug() << "Error:" << error; } ---------------------------------SerialPortWorker.cpp--------------------------------- #include "SerialPortWorker.h" #include

SerialPortWorker::SerialPortWorker(const QString &portName, QObject *parent)
: QObject(parent), portName(portName), serialPort(nullptr) {}

SerialPortWorker::~SerialPortWorker() {
if (serialPort && serialPort->isOpen()) {
serialPort->close();
delete serialPort;
}
}

void SerialPortWorker::startWork() {
serialPort = new QSerialPort(); // 在当前线程中创建 QSerialPort
serialPort->setPortName(portName);
serialPort->setBaudRate(QSerialPort::Baud9600);
serialPort->setDataBits(QSerialPort::Data8);
serialPort->setParity(QSerialPort::NoParity);
serialPort->setStopBits(QSerialPort::OneStop);
serialPort->setFlowControl(QSerialPort::NoFlowControl);

if (!serialPort->open(QIODevice::ReadWrite)) {
emit errorOccurred(serialPort->errorString());
return;
}

connect(serialPort, &QSerialPort::readyRead, this, &SerialPortWorker::handleReadyRead);
}

void SerialPortWorker::sendData(const QByteArray &data) {
if (serialPort && serialPort->isOpen()) {
serialPort->write(data);
}
}

void SerialPortWorker::handleReadyRead() {
QByteArray data = serialPort->readAll();
emit dataReceived(data);
}

发表评论

邮箱地址不会被公开。 必填项已用*标注