Qt串口短接的自动收发检测以多线程方式代码实现
时间: 2024-03-17 15:43:56 浏览: 73
以下是一个简单的Qt串口短接的自动收发检测的多线程方式的代码实现:
```
// SerialThread.h
#ifndef SERIALTHREAD_H
#define SERIALTHREAD_H
#include <QObject>
#include <QtSerialPort/QSerialPort>
class SerialThread : public QObject
{
Q_OBJECT
public:
explicit SerialThread(QObject *parent = nullptr);
void setPortName(const QString &portName);
void setBaudRate(int baudRate);
void setDataBits(QSerialPort::DataBits dataBits);
void setParity(QSerialPort::Parity parity);
void setStopBits(QSerialPort::StopBits stopBits);
signals:
void error(const QString &s);
void message(const QString &s);
void finished();
public slots:
void run();
private:
QString m_portName;
int m_baudRate;
QSerialPort::DataBits m_dataBits;
QSerialPort::Parity m_parity;
QSerialPort::StopBits m_stopBits;
};
#endif // SERIALTHREAD_H
// SerialThread.cpp
#include "SerialThread.h"
SerialThread::SerialThread(QObject *parent) : QObject(parent)
{
m_portName = "";
m_baudRate = QSerialPort::Baud115200;
m_dataBits = QSerialPort::Data8;
m_parity = QSerialPort::NoParity;
m_stopBits = QSerialPort::OneStop;
}
void SerialThread::setPortName(const QString &portName)
{
m_portName = portName;
}
void SerialThread::setBaudRate(int baudRate)
{
m_baudRate = baudRate;
}
void SerialThread::setDataBits(QSerialPort::DataBits dataBits)
{
m_dataBits = dataBits;
}
void SerialThread::setParity(QSerialPort::Parity parity)
{
m_parity = parity;
}
void SerialThread::setStopBits(QSerialPort::StopBits stopBits)
{
m_stopBits = stopBits;
}
void SerialThread::run()
{
QSerialPort serialPort;
serialPort.setPortName(m_portName);
serialPort.setBaudRate(m_baudRate);
serialPort.setDataBits(m_dataBits);
serialPort.setParity(m_parity);
serialPort.setStopBits(m_stopBits);
if (!serialPort.open(QIODevice::ReadWrite))
{
emit error(tr("Failed to open port %1, error: %2").arg(m_portName).arg(serialPort.errorString()));
return;
}
QString testCommand = "AT\r\n";
QByteArray testData = testCommand.toLocal8Bit();
int timeout = 1000;
while (true)
{
// 发送测试命令
serialPort.write(testData);
// 等待一定时间后读取返回数据
if (serialPort.waitForReadyRead(timeout))
{
QByteArray responseData = serialPort.readAll();
QString response = QString::fromLocal8Bit(responseData);
if (response.trimmed() == "OK")
{
emit message(tr("Port %1 is working properly.").arg(m_portName));
}
else
{
emit error(tr("Port %1 is short-circuited.").arg(m_portName));
break;
}
}
else
{
emit error(tr("Port %1 is not responding.").arg(m_portName));
break;
}
}
serialPort.close();
emit finished();
}
// MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "SerialThread.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 on_pushButton_clicked();
void handleError(const QString &s);
void handleMessage(const QString &s);
void handleThreadFinished();
private:
Ui::MainWindow *ui;
SerialThread *m_thread;
};
#endif // MAINWINDOW_H
// MainWindow.cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_thread = new SerialThread(this);
connect(m_thread, &SerialThread::error, this, &MainWindow::handleError);
connect(m_thread, &SerialThread::message, this, &MainWindow::handleMessage);
connect(m_thread, &SerialThread::finished, this, &MainWindow::handleThreadFinished);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString portName = ui->comboBox->currentText();
int baudRate = ui->comboBox_2->currentText().toInt();
QSerialPort::DataBits dataBits = static_cast<QSerialPort::DataBits>(ui->comboBox_3->currentIndex());
QSerialPort::Parity parity = static_cast<QSerialPort::Parity>(ui->comboBox_4->currentIndex());
QSerialPort::StopBits stopBits = static_cast<QSerialPort::StopBits>(ui->comboBox_5->currentIndex());
m_thread->setPortName(portName);
m_thread->setBaudRate(baudRate);
m_thread->setDataBits(dataBits);
m_thread->setParity(parity);
m_thread->setStopBits(stopBits);
m_thread->start();
}
void MainWindow::handleError(const QString &s)
{
ui->textEdit->append(QString("<font color=red>%1</font>").arg(s));
}
void MainWindow::handleMessage(const QString &s)
{
ui->textEdit->append(s);
}
void MainWindow::handleThreadFinished()
{
ui->pushButton->setEnabled(true);
}
// main.cpp
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
```
在上面的代码中,SerialThread类是串口收发线程类,其中的run()函数实现了自动收发检测的逻辑。在MainWindow类中,通过点击界面上的按钮来启动线程,并通过信号和槽来进行线程间的通信。
阅读全文