#include "MainWindow.h" #include "ui_MainWindow.h" MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->writeTable->addItem(tr("Coils"), 0); ui->writeTable->addItem(tr("Discrete Inputs"), 1); ui->writeTable->addItem(tr("Input Registers"), 2); ui->writeTable->addItem(tr("Holding Registers"), 3); } /*************************************************************************************************/ MainWindow::~MainWindow() { delete ui; timer->stop(); } /*************************************************************************************************/ void MainWindow::on_connect_clicked() { try { configDevice.serialPort = "/dev/pts/2"; configDevice.clientAddress = 1; configDevice.responseTime = 1000; configDevice.numberOfRetries = 3; qDebug() << "before init"; modbusWrapper.init(); qDebug() << "after init"; qDebug() << "before connect"; modbusWrapper.connectToDevice(configDevice); qDebug() << "after connect"; ui->errorMonitoring->setText("No Error"); } catch(ServoException ex) { ui->errorMonitoring->setText(ex.getMessage()); } } /*************************************************************************************************/ void MainWindow::on_readButton_clicked() { int startAddress = ui->startAddress->text().toInt(); int numberOfEntries = ui->readSize->text().toInt(); try { switch(ui->writeTable->currentData().toInt()) { case 0: printCoilsDataFromClient(modbusWrapper.getCoil(startAddress, static_cast(numberOfEntries))); break; case 1: printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress, static_cast( numberOfEntries))); break; case 2: printRegisterDataFromClient(modbusWrapper.getInputRegister(startAddress, static_cast( numberOfEntries))); break; case 3: printRegisterDataFromClient(modbusWrapper.getHoldingRegister(startAddress, static_cast( numberOfEntries))); break; } ui->errorMonitoring->setText("No Error"); } catch(const ServoException& exp) { ui->errorMonitoring->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::printCoilsDataFromClient(QBitArray uiCoils) { QString readedData; for(int i = 0; i < uiCoils.count(); i++) { const QString entry = tr("Number: %1, Value: %2").arg(i) .arg(QString::number(uiCoils[i])); readedData += (entry + "\n"); } ui->textEditRead->setText(readedData); } /*************************************************************************************************/ void MainWindow::printRegisterDataFromClient(QVector uiHoldingRegisters) { QString readedData; for(int i = 0; i < uiHoldingRegisters.count(); i++) { const QString entry = tr("Number: %1, Value: %2").arg(i) .arg(QString::number(uiHoldingRegisters[i])); readedData += (entry + "\n"); } ui->textEditRead->setText(readedData); } /*************************************************************************************************/ void MainWindow::on_writeSingleCoil_clicked() { try { int startAddress = ui->writeSingleCoilAddress->text().toInt(); modbusWrapper.setSingleCoil(startAddress, ui->coilData->isChecked()); ui->errorMonitoring->setText("No Error"); } catch(const ServoException& exp) { ui->errorMonitoring->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::on_writeMultiCoil_clicked() { try { int startAddress = ui->writeMultiCoilStartAddress->text().toInt(); int writeQty = ui->writeMultiCoilQty->text().toInt(); QString valueToWrite = ui->writeMultiCoilValue->text(); QBitArray uiCoils; uiCoils.resize(writeQty); for(int i = 0; i < valueToWrite.size(); i++) { if(valueToWrite.at(i) == '0') uiCoils[i] = false; else uiCoils[i] = true; } modbusWrapper.setMultipleCoil(startAddress, static_cast(writeQty), uiCoils); ui->errorMonitoring->setText("No Error"); } catch(const ServoException& exp) { ui->errorMonitoring->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::on_writeSingleRegister_clicked() { try { int startAddress = ui->writeSingleRegisterAddress->text().toInt(); quint16 value = static_cast(ui->writeSingleRegisterValue->text().toInt()); modbusWrapper.setSingleRegister(startAddress, value); ui->errorMonitoring->setText("No Error"); } catch(const ServoException& exp) { ui->errorMonitoring->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::on_writeMultiRegister_clicked() { try { int startAddress = ui->writeMultiRegisterStartAddress->text().toInt(); int writeQty = ui->writeMultiRegisterQty->text().toInt(); QString valueToWrite = ui->writeMultiRegisterValue->text(); QVector uiHoldingRegisters; uiHoldingRegisters.resize(writeQty); QRegExp rx("(\\ |\\,|\\.|\\:|\\t)"); //RegEx for ' ' or ',' or '.' or ':' or '\t' QStringList stringListValue = valueToWrite.split(rx); for(int i = 0; i < stringListValue.size(); i++) { uiHoldingRegisters[i] = static_cast(stringListValue[i].toInt()); } modbusWrapper.setMultipleRegister(startAddress, static_cast(writeQty), uiHoldingRegisters); ui->errorMonitoring->setText("No Error"); } catch(const ServoException& exp) { ui->errorMonitoring->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::on_init_clicked() { try { _servoControler.init(ui->serialPortServo->text()); timer = new QTimer(); timer->setInterval(1000); timer->start(); connect(timer, &QTimer::timeout, this, &MainWindow::handleGetRequestFromServo); } catch(ServoException exp) { ui->showServoError->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::handleGetRequestFromServo() { try { ui->showSpeed->setText(QString::number(_servoControler.getSpeed())); ui->showStartAngle->setText(QString::number(_servoControler.getStartAngle())); ui->showStopAngle->setText(QString::number(_servoControler.getStopAngle())); ui->showAngleOffset->setText(QString::number(_servoControler.getAngleOffset())); ui->showAzimuth->setText(QString::number(_servoControler.getAzimuth())); } catch(ServoException exp) { ui->showServoError->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::on_startMoving_clicked() { try { _servoControler.startMoving(); } catch(ServoException exp) { ui->showServoError->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::on_stopMoving_clicked() { try { _servoControler.stopMoving(); } catch(ServoException exp) { ui->showServoError->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::on_ResetAzimuth_clicked() { try { _servoControler.resetAzimuth(); } catch(ServoException exp) { ui->showServoError->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::on_sectorSpeed_clicked() { try { _servoControler.sector(ui->startAngle->text().toDouble(), ui->stopAngle->text().toDouble(), ui->speed->text().toDouble()); } catch(ServoException exp) { ui->showServoError->setText(exp.getMessage()); } } /*************************************************************************************************/ void MainWindow::on_fix_clicked() { try { _servoControler.fix(ui->startAngle->text().toDouble()); } catch(ServoException exp) { ui->showServoError->setText(exp.getMessage()); } }