#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; } /*************************************************************************************************/ 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"; } catch(ServoException ex) { qDebug() << ex.what(); } } void MainWindow::on_readButton_clicked() { int startAddress = ui->startAddress->text().toInt(); int numberOfEntries = ui->readSize->text().toInt(); 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; } } 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() { int startAddress = ui->writeSingleCoilAddress->text().toInt(); QBitArray uiCoils; uiCoils.resize(1); modbusWrapper.setSingleCoil(startAddress, ui->coilData->isChecked()); }