You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

88 lines
2.8 KiB

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qdebug.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);
connect(&modBusObj, &modBusMaster::dataFromClient, this, &MainWindow::printDataFromClient);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_connectButton_clicked()
{
modBusObj.init();
modBusObj.open(ui->portEdit->text(),ui->serverEdit->value());
}
void MainWindow::on_readButton_clicked()
{
int startAddress = ui->readAddress->value();
// do not go beyond 10 entries
int numberOfEntries = qMin(ui->readSize->currentText().toInt(), 10 - startAddress);
switch (ui->writeTable->currentData().toInt()) {
case 0: modBusObj.getCoil(startAddress,static_cast<quint16>(numberOfEntries));
break;
case 1: modBusObj.getInputCoil(startAddress,static_cast<quint16>(numberOfEntries));
break;
case 2: modBusObj.getInputRegister(startAddress,static_cast<quint16>(numberOfEntries));
break;
case 3: modBusObj.getHoldingRegister(startAddress,static_cast<quint16>(numberOfEntries));
break;
}
}
void MainWindow::printDataFromClient(QString readedData)
{
ui->textEditRead->setText(readedData);
}
void MainWindow::on_writeButton_clicked()
{
uiCoils.resize(2);
uiholdingRegisters.resize(2);
int startAddress = ui->writeAddress->value();
// do not go beyond 10 entries
int numberOfEntries = qMin(ui->writeSize->currentText().toInt(), 10 - startAddress);
switch (ui->writeTable->currentData().toInt()) {
case 0:
qDebug() << "coil " << numberOfEntries ;
if(numberOfEntries>1)
{
uiCoils.setBit(0,ui->coilData1->isChecked());
uiCoils.setBit(1,ui->coilData2->isChecked());
modBusObj.setMultipleCoil(startAddress, numberOfEntries, uiCoils);
}
else if (numberOfEntries==1)
{
qDebug() << "coil umberOfEntries==1 " << numberOfEntries ;
modBusObj.setSingleCoil(startAddress, ui->coilData1->isChecked());
}
break;
case 3:
if(numberOfEntries>1)
{
uiholdingRegisters[0]=ui->textEdit1->toPlainText().toUInt();
uiholdingRegisters[1]=ui->textEdit2->toPlainText().toUInt();
modBusObj.setMultipleRegister(startAddress, numberOfEntries, uiholdingRegisters);
}
else if (numberOfEntries==1)
{
modBusObj.setSingleRgister(startAddress, ui->textEdit1->toPlainText().toUInt());
}
break;
}
}