diff --git a/Inc/mainwindow.h b/Inc/mainwindow.h index afe074d..94153c3 100644 --- a/Inc/mainwindow.h +++ b/Inc/mainwindow.h @@ -70,6 +70,7 @@ private: QLabel* m_statusLabel{}; int m_requestCounter; int m_responseCounter; + bool m_disconnectRequested{false}; }; #endif // MAINWINDOW_H diff --git a/Src/mainwindow.cpp b/Src/mainwindow.cpp index 438bb5d..a344f2e 100644 --- a/Src/mainwindow.cpp +++ b/Src/mainwindow.cpp @@ -154,19 +154,21 @@ void MainWindows::onStateChanged(QModbusDevice::State state) { void MainWindows::onConnectButtonClicked() { if (!m_connected) { + m_disconnectRequested = false; if (!m_modbusClient->connectDevice()) { QMessageBox::critical(this, tr("Connection Error"), tr("Could not connect to the Modbus device!")); } // Start connection timeout timer QTimer::singleShot(m_connectTimeout, this, [this]() { - if (!m_connected) { + if (!m_connected && !m_disconnectRequested) { m_modbusClient->disconnectDevice(); QMessageBox::warning(this, tr("Connection Timeout"), tr("Connection attempt timed out!")); } }); } else { + m_disconnectRequested = true; m_modbusClient->disconnectDevice(); } }