#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: printCoilsDataFromSlave(modbusWrapper->getInputCoil(startAddress,static_cast(numberOfEntries))); //break; //case 2: printRegisterDataFromSlave(modbusWrapper->getInputRegister(startAddress,static_cast(numberOfEntries))); //break; //case 3: printRegisterDataFromSlave(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); }