Finished modbus write values implementation
This commit is contained in:
1 parent
3a3c385a55
commit
2505c2b695
6 files changed
+127
-22
No files matched your search
+64
-2
@@ -1,11 +1,15 @@
|
||||
#include "changevaluesdialog.h"
|
||||
#include "ui_changevaluesdialog.h"
|
||||
|
||||
// system includes
|
||||
#include <utility>
|
||||
|
||||
// Qt includes
|
||||
#include <QDebug>
|
||||
#include <QModbusTcpClient>
|
||||
#include <QMessageBox>
|
||||
|
||||
ChangeValuesDialog::ChangeValuesDialog(QModbusTcpClient &modbus, QWidget *parent) :
|
||||
ChangeValuesDialog::ChangeValuesDialog(QModbusTcpClient &modbus, int serverAddress, QModbusDataUnit::RegisterType registerType, QWidget *parent) :
|
||||
QDialog{parent},
|
||||
m_ui{std::make_unique<Ui::ChangeValuesDialog>()},
|
||||
m_modbus{modbus}
|
||||
@@ -24,6 +28,7 @@ ChangeValuesDialog::ChangeValuesDialog(QModbusTcpClient &modbus, QWidget *parent
|
||||
|
||||
connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &ChangeValuesDialog::sendRequest);
|
||||
|
||||
m_ui->spinBoxSlave->setValue(serverAddress);
|
||||
|
||||
{
|
||||
const auto addItem = [&](const auto &text, const auto &value){
|
||||
@@ -34,6 +39,11 @@ ChangeValuesDialog::ChangeValuesDialog(QModbusTcpClient &modbus, QWidget *parent
|
||||
addItem(tr("Input Registers"), QModbusDataUnit::InputRegisters);
|
||||
addItem(tr("Holding Registers"), QModbusDataUnit::HoldingRegisters);
|
||||
}
|
||||
m_ui->comboBoxType->setCurrentIndex(
|
||||
m_ui->comboBoxType->findData(
|
||||
QVariant::fromValue<QModbusDataUnit::RegisterType>(registerType)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
ChangeValuesDialog::~ChangeValuesDialog()
|
||||
@@ -43,5 +53,57 @@ ChangeValuesDialog::~ChangeValuesDialog()
|
||||
|
||||
void ChangeValuesDialog::sendRequest()
|
||||
{
|
||||
accept();
|
||||
const auto registerType = m_ui->comboBoxType->currentData();
|
||||
if (!registerType.isValid() || !registerType.canConvert<QModbusDataUnit::RegisterType>())
|
||||
{
|
||||
qDebug() << registerType << registerType.typeName() << registerType.canConvert<int>() << registerType.canConvert<QModbusDataUnit::RegisterType>();
|
||||
QMessageBox::warning(this,
|
||||
tr("Invalid register type selected!"),
|
||||
tr("Invalid register type selected!"));
|
||||
return;
|
||||
}
|
||||
|
||||
QModbusDataUnit dataUnit{registerType.value<QModbusDataUnit::RegisterType>(), m_ui->spinBoxFirstRegister->value(), m_model.values()};
|
||||
if (m_reply = std::unique_ptr<QModbusReply>(m_modbus.sendWriteRequest(std::move(dataUnit), m_ui->spinBoxSlave->value())))
|
||||
{
|
||||
if (m_reply->isFinished())
|
||||
replyFinished();
|
||||
else
|
||||
{
|
||||
m_ui->buttonBox->setEnabled(false);
|
||||
connect(m_reply.get(), &QModbusReply::finished, this, &ChangeValuesDialog::replyFinished);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::warning(this,
|
||||
tr("Request sending failed!"),
|
||||
tr("Request sending failed:\n\n%0").arg(m_modbus.errorString()));
|
||||
}
|
||||
}
|
||||
|
||||
void ChangeValuesDialog::replyFinished()
|
||||
{
|
||||
Q_ASSERT(m_reply);
|
||||
|
||||
if (!m_reply->isFinished())
|
||||
{
|
||||
qWarning() << "not yet finished?!";
|
||||
return;
|
||||
}
|
||||
|
||||
m_ui->buttonBox->setEnabled(true);
|
||||
|
||||
if (const QModbusDevice::Error error = m_reply->error(); error == QModbusDevice::NoError)
|
||||
{
|
||||
//accept();
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::warning(this,
|
||||
tr("Request failed!"),
|
||||
tr("Request failed with %0: %1")
|
||||
.arg(error)
|
||||
.arg(m_reply->errorString()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user