* @brief Connects the Modbus client. For TCP it retries a few times with a short backoff so a * server a control-script onConnect() just launched has time to start listening; the * per-attempt errors are swallowed via m_connecting. RTU connects once (no server race). */
| 460 | * per-attempt errors are swallowed via m_connecting. RTU connects once (no server race). |
| 461 | */ |
| 462 | bool IO::Drivers::Modbus::connectWithRetry() |
| 463 | { |
| 464 | const int attempts = (m_protocolIndex == 1) ? kTcpConnectAttempts : 1; |
| 465 | |
| 466 | m_connecting = true; |
| 467 | bool connected = false; |
| 468 | for (int attempt = 0; attempt < attempts; ++attempt) { |
| 469 | if (m_device->connectDevice()) { |
| 470 | QEventLoop loop; |
| 471 | QTimer timeout; |
| 472 | timeout.setSingleShot(true); |
| 473 | connect(&timeout, &QTimer::timeout, &loop, &QEventLoop::quit); |
| 474 | connect( |
| 475 | m_device, &QModbusClient::stateChanged, &loop, &QEventLoop::quit, Qt::UniqueConnection); |
| 476 | connect( |
| 477 | m_device, &QModbusClient::errorOccurred, &loop, &QEventLoop::quit, Qt::UniqueConnection); |
| 478 | |
| 479 | timeout.start(kTcpConnectTimeoutMs); |
| 480 | // code-verify off |
| 481 | while (m_device->state() == QModbusDevice::ConnectingState && timeout.isActive()) |
| 482 | loop.exec(); |
| 483 | // code-verify on |
| 484 | |
| 485 | if (m_device->state() == QModbusDevice::ConnectedState) { |
| 486 | connected = true; |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | m_device->disconnectDevice(); |
| 492 | if (attempt + 1 < attempts) |
| 493 | QThread::msleep(kTcpConnectBackoffMs); |
| 494 | } |
| 495 | |
| 496 | m_connecting = false; |
| 497 | return connected; |
| 498 | } |
| 499 | |
| 500 | //-------------------------------------------------------------------------------------------------- |
| 501 | // Property getters |
nothing calls this directly
no test coverage detected