Pro文件中加入:
QT += serialbus serialport widgets
H文件:
#ifndef MODBUSCLIENT_H
#define MODBUSCLIENT_H
#include <QMainWindow>
#include <QTimer>
#include <QModbusDataUnit>
#include <QModbusReply>
extern double_t wendu1;
extern double_t wendu2;
extern double_t wendu3;
extern double_t wendu4;
extern double_t wendu5;
extern double_t wendu6;
extern double_t wendu7;
extern double_t wendu8;
class QModbusClient;
class QModbusReply;
namespace Ui {
class modbusclient;
}
class modbusclient : public QMainWindow
{
Q_OBJECT
public:
explicit modbusclient(QWidget *parent = 0);
~modbusclient();
private:
Ui::modbusclient *ui;
QModbusClient *modbusDevice = nullptr;
private slots:
void on_lianjie_caijiqi_clicked();
void on_kaiqi_caijiqi_clicked();
void readReady();
void kaishicaiji();
};
#endif
1、在H文件中创建QModbusClient对象 *modbusDevice。
C文件:
#include "modbusclient.h"
#include "ui_modbusclient.h"
#include <QModbusTcpClient>
#include <QDebug>
double_t wendu1;
double_t wendu2;
double_t wendu3;
double_t wendu4;
double_t wendu5;
double_t wendu6;
double_t wendu7;
double_t wendu8;
modbusclient::modbusclient(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::modbusclient)
{
ui->setupUi(this);
modbusDevice = new QModbusTcpClient(this);
}
modbusclient::~modbusclient()
{
delete ui;
}
void modbusclient::on_kaiqi_caijiqi_clicked()
{
kaishicaiji();
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(kaishicaiji()));
timer->start(1000);
}
void modbusclient::on_lianjie_caijiqi_clicked()
{
if (!modbusDevice)
return;
if (modbusDevice->state() != QModbusDevice::ConnectedState) {
modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter, 502);
modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter, "192.168.1.126");
modbusDevice->setTimeout(2000);
modbusDevice->setNumberOfRetries(3);
if (modbusDevice->connectDevice()) {
qDebug()<< "Connected!";
}
}
else {
modbusDevice->disconnectDevice();
}
}
void modbusclient::kaishicaiji()
{
if (!modbusDevice)
return;
QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters,32,8);
if (auto *reply = modbusDevice->sendReadRequest(readUnit,1)) {
if (!reply->isFinished())
connect(reply, &QModbusReply::finished, this, &modbusclient::readReady);
else
delete reply;
}
}
void modbusclient::readReady()
{
auto reply = qobject_cast<QModbusReply *>(sender());
if (!reply)
return;
if (reply->error() == QModbusDevice::NoError) {
const QModbusDataUnit unit = reply->result();
wendu1 = unit.value(0)/10.0;
wendu2 = unit.value(1)/10.0;
wendu3 = unit.value(2)/10.0;
wendu4 = unit.value(3)/10.0;
wendu5 = unit.value(4)/10.0;
wendu6 = unit.value(5)/10.0;
wendu7 = unit.value(6)/10.0;
wendu8 = unit.value(7)/10.0;
ui->wendu_1->setNum(wendu1);
ui->wendu_2->setNum(wendu2);
ui->wendu_3->setNum(wendu3);
ui->wendu_4->setNum(wendu4);
ui->wendu_5->setNum(wendu5);
ui->wendu_6->setNum(wendu6);
ui->wendu_7->setNum(wendu7);
ui->wendu_8->setNum(wendu8);
}
reply->deleteLater();
}
2、在C文件中将modbusDevice赋值为QModbusTcpClient类型的对象 3、在on_lianjie_caijiqi_clicked()函数中设置TCP连接的地址、端口、超时设置和重试次数 4、利用QModbusDevice的connectDevice方法接受MODBUS网络 5、创建QModbusDataUnit数据单元 6、调用QModbusDataUnit的sendReadRequest方法读取数据
UI文件: 客户端工作时截图:
|