#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(); 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; } } catch(const ServoException& exp) { qDebug() << "aaaa"; qDebug() << exp.what(); } } /*************************************************************************************************/ 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(); modbusWrapper.setSingleCoil(startAddress, ui->coilData->isChecked()); } /*************************************************************************************************/ void MainWindow::on_writeMultiCoil_clicked() { 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); } /*************************************************************************************************/ void MainWindow::on_writeSingleRegister_clicked() { int startAddress = ui->writeSingleRegisterAddress->text().toInt(); quint16 value = static_cast(ui->writeSingleRegisterValue->text().toInt()); modbusWrapper.setSingleRegister(startAddress, value); } /*************************************************************************************************/ void MainWindow::on_writeMultiRegister_clicked() { 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); }