Qt 使用线程 访问modbus485 读写仪器仪表参数 c++ 代码实例 不采用继承Qthread 重写run 的方法实现
时间: 2024-02-20 20:57:10 浏览: 100
以下是使用 Qt 线程和 Modbus 库实现访问 Modbus 485 读写仪器仪表参数的 C++ 代码示例,不采用继承 QThread 重写 run 的方法实现:
```c++
// modbusworker.h
#ifndef MODBUSWORKER_H
#define MODBUSWORKER_H
#include <QObject>
#include <QModbusDevice>
#include <QModbusTcpClient>
class ModbusWorker : public QObject
{
Q_OBJECT
public:
explicit ModbusWorker(QObject *parent = nullptr);
void setConnectionParameters(QString host, quint16 port);
void setDeviceAddress(int address);
bool connectDevice();
void readData(int startAddress, int numberOfValues);
void writeData(int startAddress, int value);
signals:
void dataRead(QVector<quint16> data);
void dataWritten();
private:
QModbusDevice *modbusDevice;
QString connectionHost;
quint16 connectionPort;
int deviceAddress;
};
#endif // MODBUSWORKER_H
```
```c++
// modbusworker.cpp
#include "modbusworker.h"
ModbusWorker::ModbusWorker(QObject *parent)
: QObject(parent)
{
modbusDevice = new QModbusTcpClient(this);
}
void ModbusWorker::setConnectionParameters(QString host, quint16 port)
{
connectionHost = host;
connectionPort = port;
}
void ModbusWorker::setDeviceAddress(int address)
{
deviceAddress = address;
}
bool ModbusWorker::connectDevice()
{
if (!modbusDevice)
return false;
modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter, connectionHost);
modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter, connectionPort);
modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter, "COM1");
modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter, "EvenParity");
modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, 9600);
modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, 8);
modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, 1);
modbusDevice->setTimeout(1000);
modbusDevice->setNumberOfRetries(3);
if (!modbusDevice->connectDevice()) {
return false;
}
return true;
}
void ModbusWorker::readData(int startAddress, int numberOfValues)
{
if (!modbusDevice)
return;
if (auto *reply = modbusDevice->sendReadRequest(QModbusDataUnit(QModbusDataUnit::HoldingRegisters,
startAddress, numberOfValues),
deviceAddress)) {
if (!reply->isFinished()) {
connect(reply, &QModbusReply::finished, this, [=]() {
if (reply->error() == QModbusDevice::NoError) {
emit dataRead(reply->result().values());
}
reply->deleteLater();
});
} else {
delete reply;
}
} else {
emit dataRead(QVector<quint16>());
}
}
void ModbusWorker::writeData(int startAddress, int value)
{
if (!modbusDevice)
return;
if (auto *reply = modbusDevice->sendWriteRequest(QModbusDataUnit(QModbusDataUnit::HoldingRegisters,
startAddress, 1),
QVector<quint16>{static_cast<quint16>(value)},
deviceAddress)) {
if (!reply->isFinished()) {
connect(reply, &QModbusReply::finished, this, [=]() {
if (reply->error() == QModbusDevice::NoError) {
emit dataWritten();
}
reply->deleteLater();
});
} else {
delete reply;
}
} else {
emit dataWritten();
}
}
```
```c++
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "modbusworker.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_connectPushButton_clicked();
void on_readPushButton_clicked();
void on_writePushButton_clicked();
void on_dataRead(QVector<quint16> data);
void on_dataWritten();
private:
Ui::MainWindow *ui;
ModbusWorker *modbusWorker;
};
#endif // MAINWINDOW_H
```
```c++
// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QThread>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, modbusWorker(new ModbusWorker(this))
{
ui->setupUi(this);
connect(modbusWorker, &ModbusWorker::dataRead, this, &MainWindow::on_dataRead);
connect(modbusWorker, &ModbusWorker::dataWritten, this, &MainWindow::on_dataWritten);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_connectPushButton_clicked()
{
if (ui->tcpConnectionRadioButton->isChecked()) {
modbusWorker->setConnectionParameters(ui->hostLineEdit->text(),
ui->portSpinBox->value());
} else if (ui->serialConnectionRadioButton->isChecked()) {
// Set serial connection parameters
}
modbusWorker->setDeviceAddress(ui->deviceAddressSpinBox->value());
if (!modbusWorker->connectDevice()) {
QMessageBox::warning(this, "Error", "Unable to connect to Modbus device.");
}
}
void MainWindow::on_readPushButton_clicked()
{
modbusWorker->readData(ui->startAddressSpinBox->value(),
ui->numberOfValuesSpinBox->value());
}
void MainWindow::on_writePushButton_clicked()
{
modbusWorker->writeData(ui->startAddressSpinBox->value(),
ui->writeValueSpinBox->value());
}
void MainWindow::on_dataRead(QVector<quint16> data)
{
// Process read data
}
void MainWindow::on_dataWritten()
{
// Process write data
}
```
在主界面中,通过连接不同的按钮和输入框,调用 ModbusWorker 类中的函数,实现连接 Modbus 设备、读取数据、写入数据等操作。而 ModbusWorker 类中的函数则使用 Qt Modbus 库提供的类和函数,实现连接 Modbus 设备、读写数据等操作,同时使用信号与槽机制将数据传递到主线程进行处理。
阅读全文