MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / finalizeAndConnect

Method finalizeAndConnect

app/src/IO/Drivers/Modbus.cpp:412–455  ·  view source on GitHub ↗

* @brief Wires the Modbus client signals and attempts the connection. */

Source from the content-addressed store, hash-verified

410 * @brief Wires the Modbus client signals and attempts the connection.
411 */
412bool IO::Drivers::Modbus::finalizeAndConnect(const QString& target)
413{
414 if (!m_device) {
415 Misc::Utilities::showMessageBox(
416 tr("Modbus Initialization Failed"),
417 tr("Unable to create Modbus device. Check your system configuration and try again."),
418 QMessageBox::Critical);
419 return false;
420 }
421
422 m_device->setTimeout(1000);
423 m_device->setNumberOfRetries(3);
424
425 connect(m_device,
426 &QModbusClient::stateChanged,
427 this,
428 &IO::Drivers::Modbus::onStateChanged,
429 Qt::UniqueConnection);
430 connect(m_device,
431 &QModbusClient::errorOccurred,
432 this,
433 &IO::Drivers::Modbus::onErrorOccurred,
434 Qt::UniqueConnection);
435
436 if (!connectWithRetry()) {
437 QString error = m_device->errorString();
438 m_device->deleteLater();
439 m_device = nullptr;
440
441 Misc::Utilities::showMessageBox(
442 tr("Modbus Connection Failed"),
443 error.isEmpty()
444 ? tr("Unable to connect to \"%1\". Check your connection settings.").arg(target)
445 : tr("\"%1\": %2").arg(target, error),
446 QMessageBox::Critical);
447 return false;
448 }
449
450 if (m_device->state() == QModbusDevice::ConnectedState)
451 m_pollTimer->start(m_pollInterval);
452
453 Q_EMIT configurationChanged();
454 return true;
455}
456
457/**
458 * @brief Connects the Modbus client. For TCP it retries a few times with a short backoff so a

Callers

nothing calls this directly

Calls 3

errorStringMethod · 0.80
isEmptyMethod · 0.80
startMethod · 0.45

Tested by

no test coverage detected