correct read, change reg
This commit is contained in:
@@ -55,6 +55,7 @@ private:
|
|||||||
QVector<QPushButton*> m_buttons;
|
QVector<QPushButton*> m_buttons;
|
||||||
QVector<ColoredSquare*> m_squares;
|
QVector<ColoredSquare*> m_squares;
|
||||||
QVector<int> m_colorIndices;
|
QVector<int> m_colorIndices;
|
||||||
|
QVector<int> m_buttonValues; // Add this new member to store button states
|
||||||
|
|
||||||
QModbusTcpClient* m_modbusClient;
|
QModbusTcpClient* m_modbusClient;
|
||||||
QPushButton* m_connectButton;
|
QPushButton* m_connectButton;
|
||||||
|
|||||||
@@ -139,19 +139,6 @@ void Mainwindows::onPollTimer() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mainwindows::writeRegister(int index) {
|
|
||||||
if (!m_connected) return;
|
|
||||||
|
|
||||||
QModbusDataUnit writeUnit(QModbusDataUnit::HoldingRegisters, m_buttonRegisters[index], 1);
|
|
||||||
|
|
||||||
if (auto *reply = m_modbusClient->sendWriteRequest(writeUnit, 1)) {
|
|
||||||
if (!reply->isFinished())
|
|
||||||
connect(reply, &QModbusReply::finished, reply, &QModbusReply::deleteLater);
|
|
||||||
else
|
|
||||||
delete reply;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Mainwindows::readRegisters() {
|
void Mainwindows::readRegisters() {
|
||||||
if (!m_connected) return;
|
if (!m_connected) return;
|
||||||
|
|
||||||
@@ -199,7 +186,6 @@ void Mainwindows::onReadReady() {
|
|||||||
static const QColor colors[] = {Qt::gray, Qt::black, Qt::green};
|
static const QColor colors[] = {Qt::gray, Qt::black, Qt::green};
|
||||||
m_colorIndices[i] = value % 3;
|
m_colorIndices[i] = value % 3;
|
||||||
m_squares[i]->setColor(colors[m_colorIndices[i]]);
|
m_squares[i]->setColor(colors[m_colorIndices[i]]);
|
||||||
qDebug() << "Indicator" << i << "address:" << address << "value:" << value;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -207,9 +193,9 @@ void Mainwindows::onReadReady() {
|
|||||||
// Handle buttons
|
// Handle buttons
|
||||||
for (int i = 0; i < m_buttonRegisters.size(); ++i) {
|
for (int i = 0; i < m_buttonRegisters.size(); ++i) {
|
||||||
if (address == m_buttonRegisters[i]) {
|
if (address == m_buttonRegisters[i]) {
|
||||||
|
m_buttonValues[i] = value & 1; // Update stored button value
|
||||||
QStringList bName = {"Открыть", "Закрыть"};
|
QStringList bName = {"Открыть", "Закрыть"};
|
||||||
m_buttons[i]->setText(bName[value & 1]);
|
m_buttons[i]->setText(bName[m_buttonValues[i]]);
|
||||||
qDebug() << "Button" << i << "address:" << address << "value:" << value;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,9 +227,18 @@ void Mainwindows::createUIElements() {
|
|||||||
if (i < m_buttonCount) {
|
if (i < m_buttonCount) {
|
||||||
auto* button = new QPushButton(QString("Register %1").arg(m_buttonRegisters[i]), this);
|
auto* button = new QPushButton(QString("Register %1").arg(m_buttonRegisters[i]), this);
|
||||||
m_buttons.append(button);
|
m_buttons.append(button);
|
||||||
|
m_buttonValues.append(0); // Initialize button values
|
||||||
m_table->setCellWidget(i, 0, button);
|
m_table->setCellWidget(i, 0, button);
|
||||||
connect(button, &QPushButton::clicked, [this, i]() {
|
connect(button, &QPushButton::clicked, [this, i]() {
|
||||||
writeRegister(i);
|
m_buttonValues[i] = !m_buttonValues[i]; // Toggle value
|
||||||
|
QModbusDataUnit writeUnit(QModbusDataUnit::HoldingRegisters, m_buttonRegisters[i], 1);
|
||||||
|
writeUnit.setValue(0, m_buttonValues[i]);
|
||||||
|
if (auto *reply = m_modbusClient->sendWriteRequest(writeUnit, 1)) {
|
||||||
|
if (!reply->isFinished())
|
||||||
|
connect(reply, &QModbusReply::finished, reply, &QModbusReply::deleteLater);
|
||||||
|
else
|
||||||
|
delete reply;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user