From 1b7388821e761482225a0da40cd143b392bcf588 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:05:06 +0300 Subject: [PATCH] Checkpoint to git test --- M3KTE_TERM/M3KTE_TERM.pro | 3 + M3KTE_TERM/m3kte.cpp | 742 ++++++++++++++---------------- M3KTE_TERM/m3kte.h | 27 +- M3KTE_TERM/m3kte.ui | 10 +- M3KTE_TERM/scanboard.cpp | 29 ++ M3KTE_TERM/scanboard.h | 29 ++ M3KTE_TERM/scanboard.ui | 81 ++++ M3KTE_TERM/settingsdialog.h | 2 +- M3KTE_TERM/settingsdialog.ui | 6 +- M3KTE_TERM/writeregistermodel.cpp | 10 +- 10 files changed, 522 insertions(+), 417 deletions(-) create mode 100644 M3KTE_TERM/scanboard.cpp create mode 100644 M3KTE_TERM/scanboard.h create mode 100644 M3KTE_TERM/scanboard.ui diff --git a/M3KTE_TERM/M3KTE_TERM.pro b/M3KTE_TERM/M3KTE_TERM.pro index 7d5aad9..0f2e3a3 100644 --- a/M3KTE_TERM/M3KTE_TERM.pro +++ b/M3KTE_TERM/M3KTE_TERM.pro @@ -26,6 +26,7 @@ SOURCES += \ main.cpp \ m3kte.cpp \ multiplesettings.cpp \ + scanboard.cpp \ settingsdialog.cpp \ writeregistermodel.cpp @@ -33,6 +34,7 @@ HEADERS += \ devicesettingsdialog.h \ m3kte.h \ multiplesettings.h \ + scanboard.h \ settingsdialog.h \ writeregistermodel.h @@ -40,6 +42,7 @@ FORMS += \ devicesettingsdialog.ui \ m3kte.ui \ multiplesettings.ui \ + scanboard.ui \ settingsdialog.ui # Default rules for deployment. diff --git a/M3KTE_TERM/m3kte.cpp b/M3KTE_TERM/m3kte.cpp index 8feec46..f7d672a 100644 --- a/M3KTE_TERM/m3kte.cpp +++ b/M3KTE_TERM/m3kte.cpp @@ -17,7 +17,6 @@ M3KTE::M3KTE(QWidget *parent) , ui(new Ui::M3KTE) { ui->setupUi(this); - //Массив указателей на индикаторы напряжения топливных элементов { int i = 0; @@ -342,14 +341,10 @@ M3KTE::M3KTE(QWidget *parent) m_ProgressBar[i++] = ui->FuelCellVoltageBar_319; m_ProgressBar[i++] = ui->FuelCellVoltageBar_320; } - - m_settingsDialog = new SettingsDialog(this); - ui->writeTable->addItem(tr("Exceptions"), QModbusDataUnit::Coils); ui->writeTable->addItem(tr("Warnings"), QModbusDataUnit::HoldingRegisters); ui->writeTable->addItem(tr("Accidents"), QModbusDataUnit::HoldingRegisters); - for(int i = 0; i < 4; i++) { Boards[i].ModbusModelCoil = new WriteRegisterModel(this, 85 - (i/3*20), false); @@ -359,57 +354,72 @@ M3KTE::M3KTE(QWidget *parent) Boards[i].ModbusModelHoldingReg->setStartAddress(0); Boards[i].ModbusModelHoldingReg->setNumberOfValues(QString::number(85-(i/3*20))); } - m_deviceSettingsDialog = new DeviceSettingsDialog(this); m_regMultipleSettings = new MultipleSettings(this); - modbusDevice = new QModbusRtuSerialMaster(this); - //ui->M3kteRegSettings->setEnabled(false); - Boards[0].boardScanners = new QTimer(); Boards[1].boardScanners = new QTimer(); Boards[2].boardScanners = new QTimer(); Boards[3].boardScanners = new QTimer(); - Boards[0].boardScanners->setSingleShot(true); Boards[1].boardScanners->setSingleShot(true); Boards[2].boardScanners->setSingleShot(true); Boards[3].boardScanners->setSingleShot(true); - - connect(Boards[0].boardScanners, &QTimer::timeout, this, &M3KTE::firstBoardScan); - connect(Boards[1].boardScanners, &QTimer::timeout, this, &M3KTE::secondBoardScan); - connect(Boards[2].boardScanners, &QTimer::timeout, this, &M3KTE::thirdBoardScan); - connect(Boards[3].boardScanners, &QTimer::timeout, this, &M3KTE::fourthBoardScan); - + connect(Boards[0].boardScanners, &QTimer::timeout, this, [this]() + { + boardScan(0); + }); + connect(Boards[1].boardScanners, &QTimer::timeout, this, [this]() + { + boardScan(1); + }); + connect(Boards[2].boardScanners, &QTimer::timeout, this, [this]() + { + boardScan(2); + }); + connect(Boards[3].boardScanners, &QTimer::timeout, this, [this]() + { + boardScan(3); + }); Boards[0].adr = 1; Boards[1].adr = 2; Boards[2].adr = 3; Boards[3].adr = 4; - + ui->M3kteMenuSettings->setEnabled(false); + ui->M3kteRegSettings->setEnabled(false); ui->BSM_Warning->setEnabled(false); ui->BSM_Accident->setEnabled(false); ui->BSM_WorkInProgress->setEnabled(false); - initActions(); - ui->BST_Off->setChecked(true); - ui->boardSelectBox->setCurrentIndex(0); ui->writeTable->setCurrentIndex(0); changeTable(0, 0); - + Boards_Fields[0] = ui->FCBoardBox; + Boards_Fields[1] = ui->FCBoardBox_2; + Boards_Fields[2] = ui->FCBoardBox_3; + Boards_Fields[3] = ui->FCBoardBox_4; for (int i = 0; i < 4; i++) { statusM3KTE.Warnings[i] = false; statusM3KTE.Accidents[i] = false; + Boards_Fields[i]->setEnabled(false); + } + for (int i = 0; i < 5; i++) + { + ui->writeValueTable->resizeColumnToContents(i); } - QBrush tb(Qt::transparent); // Transparent brush, solid pattern for(int i = 0; i<320; i++) { + m_ProgressBar[i]->setTextVisible(true); + m_ProgressBar[i]->setMinimumSize(25, 25); + m_ProgressBar[i]->setMaximumSize(25, 25); + m_ProgressBar[i]->resize(25, 25); + m_ProgressBar[i]->setAlignment(Qt::AlignCenter); + m_ProgressBar[i]->setFormat(QString("%1").arg((i%85+1))); m_ProgressBar[i]->setValue(3); QString style_fc_off = "QProgressBar {border: 2px solid black; font: bold 10px} QProgressBar::chunk {background: hsva(" + QString::number(30) + ", 30, 30, 30%);} "; m_ProgressBar[i]->setStyleSheet(style_fc_off); - ThePhantomMenace[i] = new QPushButton(m_ProgressBar[i]); ThePhantomMenace[i]->setFlat(true); ThePhantomMenace[i]->setPalette(QPalette(tb, tb, tb, tb, tb, tb, tb, tb, tb)); @@ -417,16 +427,8 @@ M3KTE::M3KTE(QWidget *parent) selectPositionOnTree(i); }); } - debug(); - connect(m_deviceSettingsDialog, &DeviceSettingsDialog::parityChanged, this, &M3KTE::onParityUpdate); connect(m_deviceSettingsDialog, &DeviceSettingsDialog::speedChanged, this, &M3KTE::onSpeedUpdate); - - - //Вызов окна настройки подключения - - //Вызов окна настройки устройства - } M3KTE::~M3KTE() @@ -438,56 +440,11 @@ M3KTE::~M3KTE() delete ui; } -void M3KTE::debug() -{ - srand(time(0)); - for(int i = 0; i < 320; i++) - { - m_ProgressBar[i]->setTextVisible(true); - m_ProgressBar[i]->setMinimumSize(25, 25); - m_ProgressBar[i]->setMaximumSize(25, 25); - m_ProgressBar[i]->resize(25, 25); - m_ProgressBar[i]->setAlignment(Qt::AlignCenter); - m_ProgressBar[i]->setFormat(QString("%1").arg((i%85+1))); -// int j = rand()%4; -// if(j!=3) j=rand()%4; -// m_ProgressBar[i]->setValue(j); -// QString style_fc = "QProgressBar {border: 2px solid black; font: bold 10px} QProgressBar::chunk {background: hsva(" + QString::number(j*50-50) + ", 255, 255, 100%);} "; -// QString style_fc_off = "QProgressBar {border: 2px solid black; font: bold 10px} QProgressBar::chunk {background: hsva(" + QString::number(30) + ", 30, 30, 30%);} "; -// m_ProgressBar[i]->setStyleSheet(style_fc); - -// switch (j) { -// case 1: -// { -// m_ProgressBar[i]->setStatusTip(QString("П%1 ТЭ%2: Аварийный уровень напряжения.").arg(QString::number(i/85+1), QString::number(i%85))); -// break; -// } -// case 2: -// { -// m_ProgressBar[i]->setStatusTip(QString("П%1 ТЭ%2: Предупредительный уровень напряжения.").arg(QString::number(i/85+1), QString::number(i%85))); -// break; -// } -// case 3: -// { -// m_ProgressBar[i]->setStatusTip(QString("П%1 ТЭ%2: Уровень напряжения в норме.").arg(QString::number(i/85+1), QString::number(i%85))); -// break; -// } -// case 0: -// m_ProgressBar[i]->setStatusTip(QString("П%1 ТЭ%2: Топливный Элемент не учитывается.").arg(QString::number(i/85+1), QString::number(i%85))); -// m_ProgressBar[i]->setStyleSheet(style_fc_off); -// m_ProgressBar[i]->setValue(3); -// break; -// } - } -} - - void M3KTE::initActions() { ui->ConnectionMenuConnect->setEnabled(true); ui->ConnectionMenuDisconnect->setEnabled(false); ui->ConnectionMenuSettings->setEnabled(true); - connect(ui->ConnectionMenuConnect, &QAction::triggered, this, &M3KTE::onConnectClicked); connect(ui->ConnectionMenuDisconnect, &QAction::triggered, @@ -500,11 +457,9 @@ void M3KTE::initActions() this, &M3KTE::onSelectedBoardChanged); connect(ui->writeTable, QOverload::of(&QComboBox::currentIndexChanged), this, &M3KTE::onWriteTableChanged); - connect(ui->M3kteRegSettings, &QAction::triggered, m_regMultipleSettings, &QDialog::show); connect(m_regMultipleSettings, &MultipleSettings::write, this, &M3KTE::slotmultipleRegWrite); connect(m_regMultipleSettings, &MultipleSettings::writeAndSend, this, &M3KTE::slotmultipleRegWriteAndSend); - connect(ui->ConnectionMenuSettings, &QAction::triggered, m_settingsDialog, &QDialog::show); connect(ui->M3kteMenuSettings, &QAction::triggered, m_deviceSettingsDialog, &QDialog::show); } @@ -513,7 +468,6 @@ void M3KTE::onConnectClicked() { if (!modbusDevice) return; - statusBar()->clearMessage(); if (modbusDevice->state() != QModbusDevice::ConnectedState) { modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter, @@ -535,37 +489,42 @@ void M3KTE::onConnectClicked() } else { ui->ConnectionMenuConnect->setEnabled(false); ui->ConnectionMenuDisconnect->setEnabled(true); - - if(pingNetworkDevices()) + if(pingNetworkDevices()) //Опрос устройств { unsigned tmp_adr[4]; for (int i = 0; i < 4; i++) { tmp_adr[i] = Boards[i].adr; } - ui->M3kteRegSettings->setEnabled(true); - m_deviceSettingsDialog->updateSettingsAfterConnection(m_settingsDialog->settings().baud, m_settingsDialog->settings().parity, tmp_adr, CurrentConnectedDevice); - ui->boardSelectBox->setCurrentIndex(0); ui->writeTable->setCurrentIndex(0); changeTable(0, 0); - + ui->M3kteMenuSettings->setEnabled(true); + ui->M3kteRegSettings->setEnabled(true); 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); } - //Опрос устройств } } else { QString style_fc_off = "QProgressBar {border: 2px solid black; font: bold 10px} QProgressBar::chunk {background: hsva(" + QString::number(30) + ", 30, 30, 30%);} "; for(int i = 0; i < 4; i++) { Boards[i].boardScanners->stop(); + Boards_Fields[i]->setEnabled(false); + Boards[i].isActive=false; + for(int j = 0; j < (85 - (i/3*20)); j++) + { + Boards[i].coil[j] = false; + Boards[i].ModbusModelCoil->m_coils[j]=0; + Boards[i].ModbusModelHoldingReg->m_currentU[j]=0; + Boards[i].ModbusModelHoldingReg->m_holdingRegisters[j] = 0; + Boards[i].ModbusModelHoldingReg->m_holdingRegisters[j + (85 - (i/3*20))] = 0; + } } for(int i = 0; i < 320; i++) { @@ -574,21 +533,19 @@ void M3KTE::onConnectClicked() m_ProgressBar[i]->setValue(3); m_ProgressBar[i]->setStyleSheet(style_fc_off); } - modbusDevice->disconnectDevice(); + ui->M3kteMenuSettings->setEnabled(false); + ui->M3kteRegSettings->setEnabled(false); ui->ConnectionMenuConnect->setEnabled(true); ui->ConnectionMenuDisconnect->setEnabled(false); ui->BST_Off->setChecked(true); ui->BST_On->setChecked(false); - 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); - ui->M3kteRegSettings->setEnabled(false); } } @@ -597,10 +554,7 @@ void M3KTE::onReadButtonClicked() { if (!modbusDevice) return; - //ui->readValue->clear(); statusBar()->clearMessage(); - - if (auto *reply = modbusDevice->sendReadRequest(readRequest(), Boards[ui->boardSelectBox->currentIndex()].adr)) { if (!reply->isFinished()) connect(reply, &QModbusReply::finished, this, &M3KTE::onReadReady); @@ -609,22 +563,6 @@ void M3KTE::onReadButtonClicked() } else { statusBar()->showMessage(tr("Read error: ") + modbusDevice->errorString(), 5000); } -// if (auto *reply = modbusDevice->sendReadRequest(readRequest(), ui->serverEdit->value())) { -// if (!reply->isFinished()) -// connect(reply, &QModbusReply::finished, this, &M3KTE::onReadReady); -// else -// delete reply; // broadcast replies return immediately -// } else { -// statusBar()->showMessage(tr("Read error: ") + modbusDevice->errorString(), 5000); -// } -// if (auto *reply = modbusDevice->sendReadRequest(readRequest(), ui->serverEdit->value())) { -// if (!reply->isFinished()) -// connect(reply, &QModbusReply::finished, this, &M3KTE::onReadReady); -// else -// delete reply; // broadcast replies return immediately -// } else { -// statusBar()->showMessage(tr("Read error: ") + modbusDevice->errorString(), 5000); -// } } void M3KTE::onReadReady() @@ -632,7 +570,6 @@ void M3KTE::onReadReady() auto reply = qobject_cast(sender()); if (!reply) return; - if (reply->error() == QModbusDevice::NoError) { const QModbusDataUnit unit = reply->result(); int Adr = 255; @@ -645,10 +582,8 @@ void M3KTE::onReadReady() } } for (int i = 0, total = int(unit.valueCount()); i < total; ++i) { - //ui->readValue->addItem(entry); if(unit.registerType() == QModbusDataUnit::Coils) { - //QStandardItem *item = ui->writeValueTable->model()->item Boards[Adr].coil[i + unit.startAddress()] = unit.value(i); if(unit.value(i)==1) Boards[Adr].ModbusModelCoil->setData(ui->writeValueTable->model()->index(i + unit.startAddress(), 2), Qt::Checked, Qt::CheckStateRole); @@ -670,7 +605,6 @@ void M3KTE::onReadReady() arg(reply->errorString()). arg(reply->error(), -1, 16), 5000); } - reply->deleteLater(); } @@ -679,7 +613,6 @@ void M3KTE::onWriteButtonClicked() if (!modbusDevice) return; statusBar()->clearMessage(); - QModbusDataUnit writeUnit = writeRequest(); QModbusDataUnit::RegisterType table = writeUnit.registerType(); for (int i = 0, total = int(writeUnit.valueCount()); i < total; ++i) { @@ -694,7 +627,6 @@ void M3KTE::onWriteButtonClicked() writeUnit.setValue(i, Boards[ui->boardSelectBox->currentIndex()].ModbusModelHoldingReg->m_holdingRegisters[i + writeUnit.startAddress()]); } } - if (auto *reply = modbusDevice->sendWriteRequest(writeUnit, Boards[ui->boardSelectBox->currentIndex()].adr)) { if (!reply->isFinished()) { connect(reply, &QModbusReply::finished, this, [this, reply]() { @@ -729,7 +661,6 @@ void M3KTE::onWriteTableChanged(int index) void M3KTE::changeTable(int board, int tabletype) { - if(tabletype==0) { ui->writeValueTable->setModel(Boards[board].ModbusModelCoil); @@ -771,17 +702,18 @@ void M3KTE::changeTable(int board, int tabletype) ui->writeValueTable->hideColumn(2); ui->writeValueTable->showColumn(3); } + for (int i = 0; i < 5; i++) + { + ui->writeValueTable->resizeColumnToContents(i); + } } QModbusDataUnit M3KTE::readRequest() const { const auto table = static_cast(ui->writeTable->currentData().toInt()); - int startAddress = 85 * (ui->writeTable->currentIndex()/2); - Q_ASSERT(startAddress >= 0 && startAddress < 340); - - // do not go beyond 10 entries + Q_ASSERT(startAddress >= 0 && startAddress < 340); quint16 numberOfEntries = qMin((ushort)(85 - (ui->boardSelectBox->currentIndex()/3*20)), quint16(340 - startAddress)); return QModbusDataUnit(table, startAddress, numberOfEntries); } @@ -790,11 +722,8 @@ QModbusDataUnit M3KTE::writeRequest() const { const auto table = static_cast(ui->writeTable->currentData().toInt()); - int startAddress = 85 * (ui->writeTable->currentIndex()/2); Q_ASSERT(startAddress >= 0 && startAddress < 340); - - // do not go beyond 10 entries quint16 numberOfEntries = qMin((ushort)(85 - (ui->boardSelectBox->currentIndex()/3*20)), quint16(340 - startAddress)); return QModbusDataUnit(table, startAddress, numberOfEntries); } @@ -806,6 +735,7 @@ bool M3KTE::event(QEvent *event) BoardIdHasBeenChanged* _event = static_cast(event); QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::HoldingRegisters, 172, 1); _unit->setValue(0, _event->BoardNewID()); + Boards[_event->BoardNum()]._tmp_adr = _event->BoardNewID(); if (auto *reply = modbusDevice->sendWriteRequest(*_unit, Boards[_event->BoardNum()].adr)) { if(!reply->isFinished()) @@ -816,7 +746,10 @@ bool M3KTE::event(QEvent *event) if (auto *subreply = modbusDevice->sendReadRequest(*_unit, Boards[_event->BoardNum()]._tmp_adr)) { if (!subreply->isFinished()) - connect(subreply, &QModbusReply::finished, this, &M3KTE::checkAdrChange); + connect(subreply, &QModbusReply::finished, this, [subreply, this, _event]() + { + checkAdrChange(subreply, _event->BoardNum()); + }); else { errorAdrChange(); @@ -833,6 +766,7 @@ bool M3KTE::event(QEvent *event) }); else { + errorAdrChange(); delete reply; } } @@ -840,43 +774,34 @@ bool M3KTE::event(QEvent *event) { errorAdrChange(); } -// if (auto *reply = modbusDevice->sendReadRequest(*_unit, Boards[_event->BoardNum()]._tmp_adr)) -// { -// if (!reply->isFinished()) -// connect(reply, &QModbusReply::finished, this, &M3KTE::checkAdrChange); -// else -// delete reply; // broadcast replies return immediately -// } else { -// errorAdrChange(); -// } m_deviceSettingsDialog->show(); return true; } return QWidget::event(event); } -void M3KTE::checkAdrChange() +void M3KTE::checkAdrChange(QModbusReply *reply, unsigned boardNum) { - auto reply = qobject_cast(sender()); if (!reply) { errorAdrChange(); return; } if (reply->error() == QModbusDevice::NoError) { - for (int i = 0; i < CurrentConnectedDevice; i++) { - if(Boards[i]._tmp_adr == reply->serverAddress()) - { - //OK - Boards[i].adr = Boards[i]._tmp_adr; - reply->deleteLater(); - return; - } + if(Boards[boardNum]._tmp_adr == reply->serverAddress()) + { + //OK + Boards[boardNum].adr = Boards[boardNum]._tmp_adr; + reply->deleteLater(); + return; } //ERROR - errorAdrChange(); + else{ + Boards[boardNum]._tmp_adr = Boards[boardNum].adr; + errorAdrChange(); reply->deleteLater(); return; + } } errorAdrChange(); reply->deleteLater(); @@ -894,7 +819,6 @@ void M3KTE::errorAdrChange() void M3KTE::onSpeedUpdate() { //Отсутствие контроля записи регистра на плате. - QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::HoldingRegisters, 173, 1); _unit->setValue(0, m_deviceSettingsDialog->currentSpeed()); for (int i = 0; i < CurrentConnectedDevice; i++) { @@ -938,21 +862,17 @@ void M3KTE::onSpeedUpdate() msgBox.setDefaultButton(QMessageBox::Ok); int ret = msgBox.exec(); } - } }); - else - delete reply; // broadcast replies return immediately + else + delete reply; // broadcast replies return immediately } } - - } void M3KTE::onParityUpdate() { //Отсутствие контроля записи регистра на плате. - QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::HoldingRegisters, 174, 1); switch (m_deviceSettingsDialog->currentParity()) { @@ -1023,13 +943,9 @@ void M3KTE::onParityUpdate() bool M3KTE::pingNetworkDevices() { - int i=0; - QTimer *timer = new QTimer(this); - connect(timer, &QTimer::timeout, this, &M3KTE::timeForPingIsGone); - timer->setSingleShot(true); + CurrentConnectedDevice = 0; QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::InputRegisters, 85, 1); int tmp_adr = 1; - bool isRun = false; bool *tmp_isRun = &isRun; auto bar = new QProgressDialog(this); @@ -1037,34 +953,36 @@ bool M3KTE::pingNetworkDevices() { *tmp_isRun = true; }); - bar->setLabelText(tr("Поиск плат...")); + bar->setLabelText(tr("Поиск плат... Текущий адрес: %1").arg(tmp_adr)); bar->setCancelButton(nullptr); bar->setRange(0, 4); bar->setMinimumDuration(100); - bar->setValue(i); + bar->setValue(CurrentConnectedDevice); + modbusDevice->setNumberOfRetries(0); - CurrentConnectedDevice = 0; + QModbusRequest requestOfDeviceType(QModbusRequest::EncapsulatedInterfaceTransport, QByteArray::fromHex("0E0404")); + QModbusRequest requestOfBoardID(QModbusRequest::EncapsulatedInterfaceTransport, QByteArray::fromHex("0E0401")); - for(i=0; i<4;) + modbusDevice->setTimeout(50); + for(CurrentConnectedDevice=0; CurrentConnectedDevice<4;) { - - if(isRun && CurrentConnectedDevice < 1) + auto *reply = modbusDevice->sendRawRequest(requestOfDeviceType, tmp_adr); + //auto *reply = modbusDevice->sendReadRequest(*_unit, tmp_adr); + //Запрос типа устройства. + if(reply == nullptr) { onConnectClicked(); + bar->close(); + bar->deleteLater(); return false; } - else if(isRun) - { - break; - } - timerForPingSignal = false; - timer->start(m_settingsDialog->settings().responseTime); - auto *reply = modbusDevice->sendReadRequest(*_unit, tmp_adr); - while(!reply->isFinished() && !timerForPingSignal) + while(!reply->isFinished()) { if(isRun && CurrentConnectedDevice < 1) { onConnectClicked(); + bar->close(); + bar->deleteLater(); return false; } else if(isRun) @@ -1073,20 +991,73 @@ bool M3KTE::pingNetworkDevices() } QCoreApplication::processEvents(); } - if(timerForPingSignal) + if(isRun && CurrentConnectedDevice < 1) { - + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; } - else + else if(isRun) { + break; + } + else if (!isRun) + { + //Нужна проверка типа устройства if(reply->error()==QModbusDevice::NoError) { - timer->stop(); - CurrentConnectedDevice++; - Boards[i].adr = Boards[i]._tmp_adr = tmp_adr; - statusBar()->showMessage(tr("Плата %1 найдена по адресу %2.").arg(i).arg(tmp_adr), m_settingsDialog->settings().responseTime); - i++; - bar->setValue(i); + QModbusResponse resp = reply->rawResult(); + QString result = QString(resp.data().remove(0, MODBUS_REQUEST_PROTOCOL_INFO_LENGTH)); + //result.remove(0, MODBUS_REQUEST_PROTOCOL_INFO_LENGTH); + if(result == QString("KTE")) + { + auto *subreply = modbusDevice->sendRawRequest(requestOfBoardID, tmp_adr); + while(!subreply->isFinished()) + { + if(isRun && CurrentConnectedDevice < 1) + { + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + else if(isRun) + { + break; + } + QCoreApplication::processEvents(); + } + if(isRun && CurrentConnectedDevice < 1) + { + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + else if(isRun) + { + break; + } + else + { + //QString boardID(subreply->rawResult().data()); + //boardID.remove(0, MODBUS_REQUEST_PROTOCOL_INFO_LENGTH); + if(Boards[(int)(subreply->rawResult().data().at(MODBUS_REQUEST_PROTOCOL_INFO_LENGTH))-1].isActive) + { + QMessageBox::warning(this, "Ошибка при сканировании сети.", QString("Платы по адресам %1 и %2 имеют одинаковый ID %3").arg(Boards[(int)subreply->rawResult().data().at(MODBUS_REQUEST_PROTOCOL_INFO_LENGTH)].adr).arg(tmp_adr).arg((int)subreply->rawResult().data().at(MODBUS_REQUEST_PROTOCOL_INFO_LENGTH))); + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + CurrentConnectedDevice++; + Boards[(int)(subreply->rawResult().data().at(MODBUS_REQUEST_PROTOCOL_INFO_LENGTH))-1].adr = Boards[(int)(subreply->rawResult().data().at(MODBUS_REQUEST_PROTOCOL_INFO_LENGTH))-1]._tmp_adr = tmp_adr; + statusBar()->showMessage(tr("Плата %1 найдена по адресу %2.").arg((int)(subreply->rawResult().data().at(MODBUS_REQUEST_PROTOCOL_INFO_LENGTH))-1).arg(tmp_adr), m_settingsDialog->settings().responseTime); + Boards[(int)(subreply->rawResult().data().at(MODBUS_REQUEST_PROTOCOL_INFO_LENGTH))-1].isActive = true; + bar->setValue(CurrentConnectedDevice); + } + } } } tmp_adr++; @@ -1095,8 +1066,9 @@ bool M3KTE::pingNetworkDevices() { //ERROR //OUT OF RANGE - QMessageBox::warning(this, "Ошибка при сканировании сети.", QString("Выход за пределы допустимых адресов. Найдено %1 плат.").arg(i)); + QMessageBox::warning(this, "Ошибка при сканировании сети.", QString("Выход за пределы допустимых адресов. Найдено %1 плат.").arg(CurrentConnectedDevice)); bar->setValue(4); + bar->close(); bar->deleteLater(); onConnectClicked(); return false; @@ -1107,207 +1079,116 @@ bool M3KTE::pingNetworkDevices() } } isRun=false; - QMessageBox::warning(this, "Сканирование сети завершено.", tr("Найдено плат: %1 из 4.").arg(i)); + QMessageBox::warning(this, "Сканирование сети завершено.", tr("Найдено плат: %1 из 4.").arg(CurrentConnectedDevice)); if(isRun) { + QMessageBox::warning(this, "Ошибка при получении текущих настроек.", QString("Прерывание по запросу пользователя.")); onConnectClicked(); + bar->close(); + bar->deleteLater(); return false; } - + modbusDevice->setTimeout(m_settingsDialog->settings().responseTime); bar->setLabelText(tr("Считывание текущих настроек...")); - bar->setRange(0, CurrentConnectedDevice*3); QModbusDataUnit* _unit_settings[3]; _unit_settings[0] = new QModbusDataUnit(QModbusDataUnit::Coils, 0, 85); _unit_settings[1] = new QModbusDataUnit(QModbusDataUnit::HoldingRegisters, 0, 85); _unit_settings[2] = new QModbusDataUnit(QModbusDataUnit::HoldingRegisters, 85, 85); - - for(i=0; isetValue(i*3+j); - if(isRun) - { - onConnectClicked(); - return false; - } - - timerForPingSignal = false; - timer->start(m_settingsDialog->settings().responseTime); - auto *reply = modbusDevice->sendReadRequest(*_unit_settings[j], Boards[i].adr); - while(!reply->isFinished() && !timerForPingSignal) + Boards_Fields[i]->setEnabled(true); + for (int j = 0; j<3; j++) { + bar->setValue(i*3+j); if(isRun) { onConnectClicked(); + bar->close(); + bar->deleteLater(); return false; } - QCoreApplication::processEvents(); - } - if(timerForPingSignal) - { - QMessageBox::warning(this, "Ошибка при получении текущих настроек.", QString("Таймаут при опросе устройства %1 по адресу %2").arg(i).arg(Boards[i].adr)); - onConnectClicked(); - return false; - } - else - { + auto *reply = modbusDevice->sendReadRequest(*_unit_settings[j], Boards[i].adr); + if(!reply) + { + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + while(!reply->isFinished()) + { + if(isRun) + { + QMessageBox::warning(this, "Ошибка при получении текущих настроек.", QString("Прерывание по запросу пользователя.")); + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + QCoreApplication::processEvents(); + } if(reply->error()==QModbusDevice::NoError) { - timer->stop(); - stepForScanCurrentSettings(reply); + applySettingsFromScan(reply); } else { - QMessageBox::warning(this, "Ошибка при получении текущих настроек.", QString("Таймаут при опросе устройства %1 по адресу %2").arg(i).arg(Boards[i].adr)); + QMessageBox::warning(this, "Ошибка при получении текущих настроек.", QString("Таймаут при опросе устройства %1 по адресу %2").arg(i+1).arg(Boards[i].adr)); bar->setValue(CurrentConnectedDevice*3); + bar->close(); + bar->deleteLater(); onConnectClicked(); return false; } } } } + modbusDevice->setNumberOfRetries(m_settingsDialog->settings().numberOfRetries); beginScanBoards(); bar->deleteLater(); return true; - -} - -void M3KTE::timeForPingIsGone() -{ - timerForPingSignal=true; } void M3KTE::beginScanBoards() -{ - if(CurrentConnectedDevice>=1) - firstBoardScan(); - if(CurrentConnectedDevice>=2) - secondBoardScan(); - if(CurrentConnectedDevice>=3) - thirdBoardScan(); - if(CurrentConnectedDevice>=4) - fourthBoardScan(); +{ + for (int i = 0; i < 4; i++) { + if(Boards[i].isActive) + boardScan(i); + } return; } -void M3KTE::firstBoardScan() +void M3KTE::boardScan(unsigned boardID) { if (!modbusDevice) return; - - QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::InputRegisters, 0, 85); - statusBar()->clearMessage(); - - if (auto *reply = modbusDevice->sendReadRequest(*_unit, Boards[0].adr)) { - if (!reply->isFinished()) - connect(reply, &QModbusReply::finished, this, &M3KTE::firstBoardReady); - else - delete reply; // broadcast replies return immediately - } else { - QMessageBox::warning(this, QString("Ошибка при опросе платы #%1").arg(1), QString(tr("Read error: ") + modbusDevice->errorString())); - statusBar()->showMessage(tr("Read error: ") + modbusDevice->errorString(), 5000); - } -} - -void M3KTE::secondBoardScan() -{ - if (!modbusDevice) - return; - - QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::InputRegisters, 0, 85); - statusBar()->clearMessage(); - - if (auto *reply = modbusDevice->sendReadRequest(*_unit, Boards[1].adr)) { - if (!reply->isFinished()) - connect(reply, &QModbusReply::finished, this, &M3KTE::secondBoardReady); - else - delete reply; // broadcast replies return immediately - } else { - QMessageBox::warning(this, QString("Ошибка при опросе платы #%1").arg(2), QString(tr("Read error: ") + modbusDevice->errorString())); - statusBar()->showMessage(tr("Read error: ") + modbusDevice->errorString(), 5000); - } -} - -void M3KTE::thirdBoardScan() -{ - if (!modbusDevice) - return; - - QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::InputRegisters, 0, 85); - statusBar()->clearMessage(); - - if (auto *reply = modbusDevice->sendReadRequest(*_unit, Boards[2].adr)) { - if (!reply->isFinished()) - connect(reply, &QModbusReply::finished, this, &M3KTE::thirdBoardReady); - else - delete reply; // broadcast replies return immediately - } else { - QMessageBox::warning(this, QString("Ошибка при опросе платы #%1").arg(3), QString(tr("Read error: ") + modbusDevice->errorString())); - statusBar()->showMessage(tr("Read error: ") + modbusDevice->errorString(), 5000); - } -} - -void M3KTE::fourthBoardScan() -{ - if (!modbusDevice) - return; - - QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::InputRegisters, 0, 85); - statusBar()->clearMessage(); - - if (auto *reply = modbusDevice->sendReadRequest(*_unit, Boards[3].adr)) { - if (!reply->isFinished()) - connect(reply, &QModbusReply::finished, this, &M3KTE::fourthBoardReady); - else - delete reply; // broadcast replies return immediately - } else { - QMessageBox::warning(this, QString("Ошибка при опросе платы #%1").arg(4), QString(tr("Read error: ") + modbusDevice->errorString())); - statusBar()->showMessage(tr("Read error: ") + modbusDevice->errorString(), 5000); - } - -} - -void M3KTE::firstBoardReady() -{ - auto reply = qobject_cast(sender()); - displayResultOfScan(reply, 0); - reply->deleteLater(); - Boards[0].boardScanners->start(m_deviceSettingsDialog->currentBoardTimer(0)); -} - -void M3KTE::secondBoardReady() -{ - auto reply = qobject_cast(sender()); - displayResultOfScan(reply, 1); - reply->deleteLater(); - Boards[1].boardScanners->start(m_deviceSettingsDialog->currentBoardTimer(1)); -} - -void M3KTE::thirdBoardReady() -{ - auto reply = qobject_cast(sender()); - displayResultOfScan(reply, 2); - reply->deleteLater(); - Boards[2].boardScanners->start(m_deviceSettingsDialog->currentBoardTimer(2)); -} - -void M3KTE::fourthBoardReady() -{ - auto reply = qobject_cast(sender()); - displayResultOfScan(reply, 3); - reply->deleteLater(); - Boards[3].boardScanners->start(m_deviceSettingsDialog->currentBoardTimer(3)); + QModbusDataUnit* _unit = new QModbusDataUnit(QModbusDataUnit::InputRegisters, 0, 85); + statusBar()->clearMessage(); + if (auto *reply = modbusDevice->sendReadRequest(*_unit, Boards[boardID].adr)) { + if (!reply->isFinished()) + connect(reply, &QModbusReply::finished, this, [this, boardID]() + { + auto subreply = qobject_cast(sender()); + displayResultOfScan(subreply, boardID); + subreply->deleteLater(); + Boards[boardID].boardScanners->start(m_deviceSettingsDialog->currentBoardTimer(boardID)); + }); + else + delete reply; // broadcast replies return immediately + } else { + QMessageBox::warning(this, QString("Ошибка при опросе платы #%1").arg(boardID+1), QString(tr("Read error: ") + modbusDevice->errorString())); + statusBar()->showMessage(tr("Read error: ") + modbusDevice->errorString(), 5000); + } } void M3KTE::displayResultOfScan(QModbusReply *reply, int boardID) { if (!reply) return; - if (reply->error() == QModbusDevice::NoError) { - const QModbusDataUnit unit = reply->result(); bool W_Flag = false; bool A_Flag = false; @@ -1318,16 +1199,11 @@ void M3KTE::displayResultOfScan(QModbusReply *reply, int boardID) QMessageBox::warning(this, QString("Ошибка при опросе платы #%1").arg(boardID), QString("Принятый ответ: Стартовый адрес %1, Количество элементов %2").arg(unit.startAddress()).arg(unit.valueCount())); return; } - for(int i = unit.startAddress(), total = int(unit.valueCount()); i < total; ++i) { - //QErrorMessage::showMessage() - //if(Boards[boardID].ModbusModelCoil->data(Boards[boardID].ModbusModelCoil->index(i, 2), Qt::CheckStateRole).Bool != 0) - //if(Boards[boardID].ModbusModelCoil->get_coil(Boards[boardID].ModbusModelCoil->index(i, 2))==true) if(Boards[boardID].coil[i]==true) { int j = 0; - //if(Boards[boardID].ModbusModelHoldingReg->get_holreg(Boards[boardID].ModbusModelHoldingReg->index(85+i, 3)) > unit.value(i)) if(Boards[boardID].HR[i + 85] > unit.value(i)) { j = 1; @@ -1335,7 +1211,6 @@ void M3KTE::displayResultOfScan(QModbusReply *reply, int boardID) m_ProgressBar[i+boardID*85]->setWhatsThis(QString("П%1 ТЭ%2: Аварийный уровень напряжения.").arg(QString::number(boardID+1), QString::number(i%85))); A_Flag = true; } - //else if(Boards[boardID].ModbusModelHoldingReg->get_holreg(Boards[boardID].ModbusModelHoldingReg->index(i, 3)) > unit.value(i)) else if(Boards[boardID].HR[i] > unit.value(i)) { j = 2; @@ -1372,7 +1247,6 @@ void M3KTE::displayResultOfScan(QModbusReply *reply, int boardID) statusM3KTE.Warnings[boardID] = true; else statusM3KTE.Warnings[boardID] = false; - ui->BSM_Warning->setChecked(false); ui->BSM_Accident->setChecked(false); ui->BSM_WorkInProgress->setChecked(true); @@ -1404,52 +1278,48 @@ void M3KTE::displayResultOfScan(QModbusReply *reply, int boardID) reply->deleteLater(); } -void M3KTE::stepForScanCurrentSettings(QModbusReply *reply) +void M3KTE::applySettingsFromScan(QModbusReply *reply) { - if (reply->error() == QModbusDevice::NoError) { - const QModbusDataUnit unit = reply->result(); - int Adr = 255; - for(int i = 0; i < 4; i++) + if (reply->error() == QModbusDevice::NoError) { + const QModbusDataUnit unit = reply->result(); + int Adr = 255; + for(int i = 0; i < 4; i++) + { + if(Boards[i].adr==reply->serverAddress() &&Boards[i].isActive) { - if(Boards[i].adr==reply->serverAddress()) - { - Adr = i; - break; - } + Adr = i; + break; } - for (int i = 0, total = int(unit.valueCount()); i < (total); ++i) { - //ui->readValue->addItem(entry); - if(unit.registerType() == QModbusDataUnit::Coils) - { - Boards[Adr].coil[i + unit.startAddress()] = unit.value(i); - //QStandardItem *item = ui->writeValueTable->model()->item - if(unit.value(i)==1) - { - Boards[Adr].ModbusModelCoil->setData(ui->writeValueTable->model()->index(i + unit.startAddress(), 2), Qt::Checked, Qt::CheckStateRole); - } - else - { - Boards[Adr].ModbusModelCoil->setData(ui->writeValueTable->model()->index(i + unit.startAddress(), 2), Qt::Unchecked, Qt::CheckStateRole); - } - - } - else if(unit.registerType() == QModbusDataUnit::HoldingRegisters) - { - Boards[Adr].HR[i + unit.startAddress()] = unit.value(i); - Boards[Adr].ModbusModelHoldingReg->setData(Boards[Adr].ModbusModelHoldingReg->index(i + unit.startAddress(), 3), QString::number(unit.value(i), 16), Qt::EditRole); - } - } - } 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(); + for (int i = 0, total = int(unit.valueCount()); i < (total); ++i) { + if(unit.registerType() == QModbusDataUnit::Coils) + { + Boards[Adr].coil[i + unit.startAddress()] = unit.value(i); + if(unit.value(i)==1) + { + Boards[Adr].ModbusModelCoil->setData(ui->writeValueTable->model()->index(i + unit.startAddress(), 2), Qt::Checked, Qt::CheckStateRole); + } + else + { + Boards[Adr].ModbusModelCoil->setData(ui->writeValueTable->model()->index(i + unit.startAddress(), 2), Qt::Unchecked, Qt::CheckStateRole); + } + } + else if(unit.registerType() == QModbusDataUnit::HoldingRegisters) + { + Boards[Adr].HR[i + unit.startAddress()] = unit.value(i); + Boards[Adr].ModbusModelHoldingReg->setData(Boards[Adr].ModbusModelHoldingReg->index(i + unit.startAddress(), 3), QString::number(unit.value(i), 16), Qt::EditRole); + } + } + } 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(); } void M3KTE::slotmultipleRegWrite() @@ -1489,7 +1359,6 @@ void M3KTE::multipleRegSend() Boards[m_regMultipleSettings->getBoardId()].coil[i+m_regMultipleSettings->getStartAdr()] = m_regMultipleSettings->getNewValue(); } } - if (auto *reply = modbusDevice->sendWriteRequest(*unit_tx, Boards[m_regMultipleSettings->getBoardId()].adr)) { if (!reply->isFinished()) { connect(reply, &QModbusReply::finished, this, [this, reply]() { @@ -1517,12 +1386,10 @@ void M3KTE::multipleRegWrite() for (unsigned i = 0; i < m_regMultipleSettings->getCountReg(); i++) { if(m_regMultipleSettings->getTypeReg()) { - //Boards[m_regMultipleSettings->getBoardId()].HR[i] = m_regMultipleSettings->getNewValue(); Boards[m_regMultipleSettings->getBoardId()].ModbusModelHoldingReg->m_holdingRegisters[i+m_regMultipleSettings->getStartAdr()] = m_regMultipleSettings->getNewValue(); } else { - //Boards[m_regMultipleSettings->getBoardId()].coil[i] = m_regMultipleSettings->getNewValue(); Boards[m_regMultipleSettings->getBoardId()].ModbusModelCoil->m_coils[i+m_regMultipleSettings->getStartAdr()] = (bool)m_regMultipleSettings->getNewValue(); } } @@ -1534,5 +1401,110 @@ void M3KTE::selectPositionOnTree(unsigned int index) QModelIndex selected = ui->writeValueTable->model()->index(index%85, 0); ui->writeValueTable->selectionModel()->select(selected, QItemSelectionModel::ClearAndSelect |QItemSelectionModel::Rows); ui->writeValueTable->scrollTo(selected); - //selection +} + +bool M3KTE::autoBaudRateScan() +{ + unsigned countOfDeviceOnLine = 0; + QString resultOfScan; + + modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter, + m_settingsDialog->settings().portName); +#if QT_CONFIG(modbus_serialport) + modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter, + m_settingsDialog->settings().parity); + modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, + m_settingsDialog->settings().dataBits); + modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, + m_settingsDialog->settings().stopBits); +#endif + + modbusDevice->setTimeout(50); + modbusDevice->setNumberOfRetries(0); + uint m_baud[] = {9600, 14400, 19200, 31250, 38400, 56000, 57600, 115200}; + + bool isRun = false; + bool *tmp_isRun = &isRun; + auto bar = new QProgressDialog(this); + connect(bar, &QProgressDialog::canceled, this, [this, tmp_isRun]() + { + *tmp_isRun = true; + }); + + bar->setCancelButton(nullptr); + bar->setRange(0, 8); + bar->setMinimumDuration(100); + bar->setValue(0); + + QModbusRequest requestOfDeviceType(QModbusRequest::EncapsulatedInterfaceTransport, QByteArray::fromHex("0E0404")); + + for(int i = 0; i < 8; i++) + { + bar->setValue(i); + modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, + m_baud[i]); + if (!modbusDevice->connectDevice()) { + statusBar()->showMessage(tr("Connect failed: ") + modbusDevice->errorString(), 5000); + } + 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); + //Запрос типа устройства. + if(reply == nullptr) + { + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + while(!reply->isFinished()) + { + if(isRun) + { + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + QCoreApplication::processEvents(); + } + if(isRun) + { + onConnectClicked(); + bar->close(); + bar->deleteLater(); + return false; + } + else if (!isRun) + { + //Нужна проверка типа устройства + if(reply->error()==QModbusDevice::NoError) + { + QModbusResponse resp = reply->rawResult(); + QString result = QString(resp.data().remove(0, MODBUS_REQUEST_PROTOCOL_INFO_LENGTH)); + //result.remove(0, MODBUS_REQUEST_PROTOCOL_INFO_LENGTH); + if(result == QString("KTE")) + { + countOfDeviceOnLine++; + } + } + } + } + if(countOfDeviceOnLine>0) + resultOfScan += QString("%1 плат M3KTE работают на скорости %2.\n").arg(countOfDeviceOnLine).arg(m_baud[i]); + countOfDeviceOnLine = 0; + } + if(m_scanBoard->exec()==QDialog::Accepted) + { + if(m_scanBoard->getCheckState()==Qt::Checked) + { + + } + return true; + } + else + return false; } diff --git a/M3KTE_TERM/m3kte.h b/M3KTE_TERM/m3kte.h index 349ebdb..a579f28 100644 --- a/M3KTE_TERM/m3kte.h +++ b/M3KTE_TERM/m3kte.h @@ -9,6 +9,7 @@ #include "writeregistermodel.h" #include "devicesettingsdialog.h" #include "multiplesettings.h" +#include "scanboard.h" #include #include @@ -18,6 +19,7 @@ #include #include #include +#include #include @@ -25,6 +27,8 @@ #include #endif +#define MODBUS_REQUEST_PROTOCOL_INFO_LENGTH 8 + QT_BEGIN_NAMESPACE namespace Ui { class M3KTE; class SettingsDialog;} QT_END_NAMESPACE @@ -40,17 +44,18 @@ private: QModbusDataUnit readRequest() const; QModbusDataUnit writeRequest() const; void changeTable(int board, int tabletype); - void debug(); void errorAdrChange(); bool event(QEvent* event); bool pingNetworkDevices(); void beginScanBoards(); void displayResultOfScan(QModbusReply *reply, int boardID); - void stepForScanCurrentSettings(QModbusReply *reply); + void applySettingsFromScan(QModbusReply *reply); void multipleRegWrite(); void multipleRegSend(); + bool autoBaudRateScan(); + void selectPositionOnTree(unsigned index); private slots: @@ -62,9 +67,7 @@ private slots: void onReadButtonClicked(); void onReadReady(); - void timeForPingIsGone(); - - void checkAdrChange(); + void checkAdrChange(QModbusReply *reply, unsigned boardNum); void onWriteButtonClicked(); void onSelectedBoardChanged(int index); @@ -73,22 +76,14 @@ private slots: void onSpeedUpdate(); void onParityUpdate(); - void firstBoardScan(); - void secondBoardScan(); - void thirdBoardScan(); - void fourthBoardScan(); + void boardScan(unsigned boardID); - void firstBoardReady(); - void secondBoardReady(); - void thirdBoardReady(); - void fourthBoardReady(); public: M3KTE(QWidget *parent = nullptr); ~M3KTE(); private: Ui::M3KTE *ui; - bool timerForPingSignal = false; int CurrentConnectedDevice = 0; //int DeviceOnNetwork[4]; QProgressBar *m_ProgressBar[320]; @@ -98,6 +93,8 @@ private: DeviceSettingsDialog *m_deviceSettingsDialog = nullptr; SettingsDialog *m_settingsDialog = nullptr; MultipleSettings *m_regMultipleSettings = nullptr; + ScanBoard *m_scanBoard = nullptr; + QGroupBox *Boards_Fields[4]; //WriteRegisterModel *writeModel = nullptr; struct StatusM3KTE{ @@ -107,6 +104,8 @@ private: struct BoardModbusRegisters { + bool isActive = false; + int adr; int _tmp_adr; diff --git a/M3KTE_TERM/m3kte.ui b/M3KTE_TERM/m3kte.ui index 0099fb2..c334108 100644 --- a/M3KTE_TERM/m3kte.ui +++ b/M3KTE_TERM/m3kte.ui @@ -3914,7 +3914,7 @@ - + @@ -3927,6 +3927,9 @@ + + + @@ -3954,7 +3957,7 @@ - + @@ -3967,9 +3970,6 @@ - - - diff --git a/M3KTE_TERM/scanboard.cpp b/M3KTE_TERM/scanboard.cpp new file mode 100644 index 0000000..236432c --- /dev/null +++ b/M3KTE_TERM/scanboard.cpp @@ -0,0 +1,29 @@ +#include "scanboard.h" +#include "ui_scanboard.h" + +ScanBoard::ScanBoard(QWidget *parent) : + QDialog(parent), + ui(new Ui::ScanBoard) +{ + ui->setupUi(this); +} + +ScanBoard::~ScanBoard() +{ + delete ui; +} + +Qt::CheckState ScanBoard::getCheckState() +{ + return checkState; +} + +void ScanBoard::on_applyToAllBox_stateChanged(int arg1) +{ + checkState = (Qt::CheckState)arg1; +} + +void ScanBoard::showMeTheTruth(QString resultOfScan) +{ + ui->logger->append(resultOfScan); +} diff --git a/M3KTE_TERM/scanboard.h b/M3KTE_TERM/scanboard.h new file mode 100644 index 0000000..f58b6a9 --- /dev/null +++ b/M3KTE_TERM/scanboard.h @@ -0,0 +1,29 @@ +#ifndef SCANBOARD_H +#define SCANBOARD_H + +#include + +namespace Ui { +class ScanBoard; +} + +class ScanBoard : public QDialog +{ + Q_OBJECT + +public: + explicit ScanBoard(QWidget *parent = nullptr); + ~ScanBoard(); + + Qt::CheckState getCheckState(); + void showMeTheTruth(QString resultOfScan); +private slots: + void on_applyToAllBox_stateChanged(int arg1); + +private: + Qt::CheckState checkState; + + Ui::ScanBoard *ui; +}; + +#endif // SCANBOARD_H diff --git a/M3KTE_TERM/scanboard.ui b/M3KTE_TERM/scanboard.ui new file mode 100644 index 0000000..83d8525 --- /dev/null +++ b/M3KTE_TERM/scanboard.ui @@ -0,0 +1,81 @@ + + + ScanBoard + + + + 0 + 0 + 337 + 145 + + + + Dialog + + + + + + + + + + + + + + Применить ко всем обнаруженным платам + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + ScanBoard + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + ScanBoard + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/M3KTE_TERM/settingsdialog.h b/M3KTE_TERM/settingsdialog.h index e658a6e..1e390bf 100644 --- a/M3KTE_TERM/settingsdialog.h +++ b/M3KTE_TERM/settingsdialog.h @@ -23,7 +23,7 @@ public: int baud = 115200; int dataBits = QSerialPort::Data8; int stopBits = QSerialPort::OneStop; - int responseTime = 500; + int responseTime = 50; int numberOfRetries = 0; }; diff --git a/M3KTE_TERM/settingsdialog.ui b/M3KTE_TERM/settingsdialog.ui index a01262a..a80b9b5 100644 --- a/M3KTE_TERM/settingsdialog.ui +++ b/M3KTE_TERM/settingsdialog.ui @@ -20,13 +20,13 @@ мс - -1 + 10 5000 - 20 + 1 200 @@ -36,7 +36,7 @@ - false + true 0 diff --git a/M3KTE_TERM/writeregistermodel.cpp b/M3KTE_TERM/writeregistermodel.cpp index a5bd320..fbe9abc 100644 --- a/M3KTE_TERM/writeregistermodel.cpp +++ b/M3KTE_TERM/writeregistermodel.cpp @@ -25,10 +25,8 @@ QVariant WriteRegisterModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() >= RowCount || index.column() >= ColumnCount) return QVariant(); - Q_ASSERT(m_coils.count() == RowCount); Q_ASSERT(m_holdingRegisters.count() == RowCount); - if (index.column() == NumColumn && role == Qt::DisplayRole) return QString::number(index.row()); @@ -42,17 +40,14 @@ QVariant WriteRegisterModel::data(const QModelIndex &index, int role) const return QString("0x%1").arg(QString::number(m_holdingRegisters.at(index.row()), 16)); if(index.column() == CurrentUColumn && role == Qt::DisplayRole) - return QString("%1 В").arg(QString::number((double)((double)m_currentU.at(index.row())/(double)1000))); - + return QString("%1 В").arg(QString::number((double)((double)m_currentU.at(index.row())/(double)1000), 'f', 3)); return QVariant(); - } QVariant WriteRegisterModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) return QVariant(); - if (orientation == Qt::Horizontal) { switch (section) { case NumColumn: @@ -76,7 +71,6 @@ bool WriteRegisterModel::setData(const QModelIndex &index, const QVariant &value { if (!index.isValid() || index.row() >= RowCount || index.column() >= ColumnCount) return false; - Q_ASSERT(m_coils.count() == RowCount); Q_ASSERT(m_holdingRegisters.count() == RowCount); @@ -86,7 +80,6 @@ bool WriteRegisterModel::setData(const QModelIndex &index, const QVariant &value emit dataChanged(index, index); return true; } - if (index.column() == HoldingColumn && role == Qt::EditRole) { // holding registers bool result = false; quint16 newValue = value.toString().toUShort(&result, 16); @@ -96,7 +89,6 @@ bool WriteRegisterModel::setData(const QModelIndex &index, const QVariant &value emit dataChanged(index, index); return result; } - return false; }