diff --git a/M3KTE_TERM/m3kte.cpp b/M3KTE_TERM/m3kte.cpp index 63325f9..7a86540 100644 --- a/M3KTE_TERM/m3kte.cpp +++ b/M3KTE_TERM/m3kte.cpp @@ -472,6 +472,10 @@ M3KTE::M3KTE(QWidget *parent) Boards[2].adr = 3; Boards[3].adr = 4; + ui->BSM_Warning->setEnabled(false); + ui->BSM_Accident->setEnabled(false); + ui->BSM_WorkInProgress->setEnabled(false); + initActions(); ui->BST_Off->setChecked(true); @@ -594,6 +598,9 @@ void M3KTE::onConnectClicked() ui->BST_Off->setChecked(false); ui->BST_On->setChecked(true); + ui->BSM_Warning->setEnabled(true); + ui->BSM_Accident->setEnabled(true); + ui->BSM_WorkInProgress->setEnabled(true); ui->BSM_WorkInProgress->setChecked(true); if(pingNetworkDevices()) @@ -617,6 +624,9 @@ void M3KTE::onConnectClicked() ui->BSM_Warning->setChecked(false); ui->BSM_Accident->setChecked(false); ui->BSM_WorkInProgress->setChecked(false); + ui->BSM_Warning->setEnabled(false); + ui->BSM_Accident->setEnabled(false); + ui->BSM_WorkInProgress->setEnabled(false); } } @@ -713,7 +723,7 @@ void M3KTE::onWriteButtonClicked() writeUnit.setValue(i, Boards[ui->boardSelectBox->currentIndex()].ModbusModelHoldingReg->m_holdingRegisters[i + writeUnit.startAddress()]); } - if (auto *reply = modbusDevice->sendWriteRequest(writeUnit, ui->boardSelectBox->currentIndex()+1)) { + if (auto *reply = modbusDevice->sendWriteRequest(writeUnit, Boards[ui->boardSelectBox->currentIndex()].adr)) { if (!reply->isFinished()) { connect(reply, &QModbusReply::finished, this, [this, reply]() { if (reply->error() == QModbusDevice::ProtocolError) { @@ -931,7 +941,7 @@ bool M3KTE::pingNetworkDevices() connect(timer, &QTimer::timeout, this, &M3KTE::timeForPingIsGone); timer->setSingleShot(true); QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::InputRegisters, 85, 1); - int tmp_adr = 0; + int tmp_adr = 1; auto bar = new QProgressDialog(this); bar->setLabelText(tr("Поиск плат...")); @@ -943,28 +953,34 @@ bool M3KTE::pingNetworkDevices() for(i=0; i<4;) { - bar->setValue(i+1); + bar->setValue(i); timerForPingSignal = false; timer->start(m_settingsDialog->settings().responseTime); - auto *reply = modbusDevice->sendReadRequest(*_unit, Boards[i].adr); + auto *reply = modbusDevice->sendReadRequest(*_unit, tmp_adr); while(!reply->isFinished() && !timerForPingSignal) { QCoreApplication::processEvents(); } if(timerForPingSignal) - {} + { + + + } else { - timer->stop(); - Boards[i].adr = Boards[i]._tmp_adr = tmp_adr; - i++; + if(reply->error()==QModbusDevice::NoError) + { + timer->stop(); + Boards[i].adr = Boards[i]._tmp_adr = tmp_adr; + i++; + } } tmp_adr++; if(tmp_adr>=247) { //ERROR //OUT OF RANGE - QMessageBox::warning(this, "Ошибка при сканировании сети.", QString("Выход за пределы допустимых адресов. Найдено %1 плат").arg(i)); + QMessageBox::warning(this, "Ошибка при сканировании сети.", QString("Выход за пределы допустимых адресов. Найдено %1 плат.").arg(i)); bar->deleteLater(); onConnectClicked(); return false; @@ -982,7 +998,7 @@ bool M3KTE::pingNetworkDevices() { for (int j = 0; j<3; j++) { - bar->setValue(i*3+j+2); + bar->setValue(i*3+j); timerForPingSignal = false; timer->start(m_settingsDialog->settings().responseTime); auto *reply = modbusDevice->sendReadRequest(*_unit_settings[j], Boards[i].adr); @@ -998,8 +1014,17 @@ bool M3KTE::pingNetworkDevices() } else { - timer->stop(); - stepForScanCurrentSettings(reply); + if(reply->error()==QModbusDevice::NoError) + { + timer->stop(); + stepForScanCurrentSettings(reply); + } + else + { + QMessageBox::warning(this, "Ошибка при получении текущих настроек.", QString("Таймаут при опросе устройства %1 по адресу %2").arg(i).arg(Boards[i].adr)); + onConnectClicked(); + return false; + } } } } @@ -1208,6 +1233,14 @@ void M3KTE::displayResultOfScan(QModbusReply *reply, int boardID) ui->BSM_Warning->setEnabled(true); } } + } else if (reply->error() == QModbusDevice::ProtocolError) { + statusBar()->showMessage(tr("Read response error: %1 (Mobus exception: 0x%2)"). + arg(reply->errorString()). + arg(reply->rawResult().exceptionCode(), -1, 16), 5000); + } else { + statusBar()->showMessage(tr("Read response error: %1 (code: 0x%2)"). + arg(reply->errorString()). + arg(reply->error(), -1, 16), 5000); } reply->deleteLater(); }