From 009ac176f96ba2f2fc4dd5651c9fc63fe98e0a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=A8?= =?UTF-8?q?=D1=82=D0=B5=D0=B9=D0=B1=D0=B5=D0=B7=D0=B0=D0=BD=D0=B4=D1=82?= Date: Thu, 23 Jan 2025 17:10:34 +0300 Subject: [PATCH] Second checkpoint --- M3KTE_TERM/m3kte.cpp | 88 +++++++++++++++++++++++++++++++++++++++- M3KTE_TERM/scanboard.cpp | 10 +++++ M3KTE_TERM/scanboard.h | 4 ++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/M3KTE_TERM/m3kte.cpp b/M3KTE_TERM/m3kte.cpp index f7d672a..a207aec 100644 --- a/M3KTE_TERM/m3kte.cpp +++ b/M3KTE_TERM/m3kte.cpp @@ -1407,6 +1407,7 @@ bool M3KTE::autoBaudRateScan() { unsigned countOfDeviceOnLine = 0; QString resultOfScan; + QVector KTE[8]; modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter, m_settingsDialog->settings().portName); @@ -1441,6 +1442,7 @@ bool M3KTE::autoBaudRateScan() for(int i = 0; i < 8; i++) { bar->setValue(i); + bar->setLabelText(tr("Поиск плат... Текущая скорость: %1").arg(m_baud[i])); modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, m_baud[i]); if (!modbusDevice->connectDevice()) { @@ -1448,7 +1450,7 @@ bool M3KTE::autoBaudRateScan() } for(int tmp_adr = 1; tmp_adr < 248; tmp_adr++) { - bar->setLabelText(tr("Поиск плат... Текущий адрес: %1").arg(tmp_adr)); + auto *reply = modbusDevice->sendRawRequest(requestOfDeviceType, tmp_adr); //auto *reply = modbusDevice->sendReadRequest(*_unit, tmp_adr); @@ -1489,6 +1491,7 @@ bool M3KTE::autoBaudRateScan() if(result == QString("KTE")) { countOfDeviceOnLine++; + KTE[i].append(tmp_adr); } } } @@ -1496,12 +1499,95 @@ bool M3KTE::autoBaudRateScan() if(countOfDeviceOnLine>0) resultOfScan += QString("%1 плат M3KTE работают на скорости %2.\n").arg(countOfDeviceOnLine).arg(m_baud[i]); countOfDeviceOnLine = 0; + modbusDevice->disconnectDevice(); } if(m_scanBoard->exec()==QDialog::Accepted) { if(m_scanBoard->getCheckState()==Qt::Checked) { + for(int i = 0; i < 8; i++) + { + for (int j = 0; j < KTE[i].size(); j++) + { + for (int l = i; l < 8; l++) { + if(KTE[l].indexOf(KTE[i].at(j))==-1) + { + QMessageBox::warning(this, "Error", QString("Несколько устройств по адресу %1, работающих на скоростях %2 и %3.").arg(KTE[i].at(j)).arg(m_baud[i]).arg(m_baud[l])); + return false; + } + } + } + } + QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::HoldingRegisters, 173, 1); + _unit->setValue(0, m_scanBoard->getBaud()); + for (int i = 0; i < 8; i++) { + bar->setValue(i); + bar->setLabelText(tr("Синхронизация плат на скорости %1").arg(m_baud[i])); + for(int j = 0; j < KTE[i].size(); j++) + { + modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, + m_baud[i]); + if (!modbusDevice->connectDevice()) { + statusBar()->showMessage(tr("Connect failed: ") + modbusDevice->errorString(), 5000); + } + auto *reply = modbusDevice->sendWriteRequest(*_unit, KTE[i].at(j)); + if(!reply) + { + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + while(!reply->isFinished()) + { + if(isRun) + { + dfQMessageBox::warning(this, "Ошибка при получении текущих настроек.", QString("Прерывание по запросу пользователя.")); + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + QCoreApplication::processEvents(); + } + if(reply->error()==QModbusDevice::TimeoutError) + { + modbusDevice->disconnectDevice(); + modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, + m_settingsDialog->UpdateBaud(m_scanBoard->getBaud())); + modbusDevice->connectDevice(); + QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::InputRegisters, 85, 1); + if (auto *subreply = modbusDevice->sendReadRequest(*_unit, Boards[i].adr)) + { + while(!subreply->isFinished()) + { + if(isRun) + { + dfQMessageBox::warning(this, "Ошибка при получении текущих настроек.", QString("Прерывание по запросу пользователя.")); + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + QCoreApplication::processEvents(); + } + if(subreply->error()!=QModbusDevice::NoError) + { + QMessageBox::warning() + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + } + } + else if(reply->error()!=QModbusDevice::NoError) + { + return false; + } + modbusDevice->disconnectDevice(); + } } return true; } diff --git a/M3KTE_TERM/scanboard.cpp b/M3KTE_TERM/scanboard.cpp index 236432c..b67ec22 100644 --- a/M3KTE_TERM/scanboard.cpp +++ b/M3KTE_TERM/scanboard.cpp @@ -27,3 +27,13 @@ void ScanBoard::showMeTheTruth(QString resultOfScan) { ui->logger->append(resultOfScan); } + +quint16 ScanBoard::getBaud() +{ + return baud; +} + +void ScanBoard::on_baudRateBox_currentTextChanged(const QString &arg1) +{ + baud = arg1.toInt(nullptr, 10); +} diff --git a/M3KTE_TERM/scanboard.h b/M3KTE_TERM/scanboard.h index f58b6a9..2cf10c5 100644 --- a/M3KTE_TERM/scanboard.h +++ b/M3KTE_TERM/scanboard.h @@ -17,11 +17,15 @@ public: Qt::CheckState getCheckState(); void showMeTheTruth(QString resultOfScan); + quint16 getBaud(); private slots: void on_applyToAllBox_stateChanged(int arg1); + void on_baudRateBox_currentTextChanged(const QString &arg1); + private: Qt::CheckState checkState; + quint16 baud; Ui::ScanBoard *ui; };