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.

289 lines
8.2 KiB

#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
3 years ago
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";
3 years ago
ui->errorMonitoring->setText("No Error");
}
catch(ServoException ex)
{
3 years ago
ui->errorMonitoring->setText(ex.getMessage());
}
}
3 years ago
/*************************************************************************************************/
3 years ago
void MainWindow::on_readButton_clicked()
{
int startAddress = ui->startAddress->text().toInt();
int numberOfEntries = ui->readSize->text().toInt();
try
3 years ago
{
switch(ui->writeTable->currentData().toInt())
{
case 0:
printCoilsDataFromClient(modbusWrapper.getCoil(startAddress,
static_cast<quint16>(numberOfEntries)));
break;
3 years ago
case 1:
printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress,
static_cast<quint16>(
numberOfEntries)));
break;
case 2:
printRegisterDataFromClient(modbusWrapper.getInputRegister(startAddress,
static_cast<quint16>(
numberOfEntries)));
break;
case 3:
printRegisterDataFromClient(modbusWrapper.getHoldingRegister(startAddress,
static_cast<quint16>(
numberOfEntries)));
break;
}
3 years ago
ui->errorMonitoring->setText("No Error");
}
catch(const ServoException& exp)
{
3 years ago
ui->errorMonitoring->setText(exp.getMessage());
3 years ago
}
}
/*************************************************************************************************/
3 years ago
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<quint16> 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()
{
3 years ago
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()
{
3 years ago
try {
int startAddress = ui->writeMultiCoilStartAddress->text().toInt();
int writeQty = ui->writeMultiCoilQty->text().toInt();
QString valueToWrite = ui->writeMultiCoilValue->text();
3 years ago
QBitArray uiCoils;
uiCoils.resize(writeQty);
3 years ago
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<quint16>(writeQty), uiCoils);
ui->errorMonitoring->setText("No Error");
}
catch(const ServoException& exp)
{
3 years ago
ui->errorMonitoring->setText(exp.getMessage());
}
}
/*************************************************************************************************/
void MainWindow::on_writeSingleRegister_clicked()
{
3 years ago
try {
int startAddress = ui->writeSingleRegisterAddress->text().toInt();
quint16 value = static_cast<quint16>(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()
{
3 years ago
try {
int startAddress = ui->writeMultiRegisterStartAddress->text().toInt();
int writeQty = ui->writeMultiRegisterQty->text().toInt();
QString valueToWrite = ui->writeMultiRegisterValue->text();
QVector<quint16> uiHoldingRegisters;
uiHoldingRegisters.resize(writeQty);
3 years ago
QRegExp rx("(\\ |\\,|\\.|\\:|\\t)"); //RegEx for ' ' or ',' or '.' or ':' or '\t'
QStringList stringListValue = valueToWrite.split(rx);
3 years ago
for(int i = 0; i < stringListValue.size(); i++)
{
uiHoldingRegisters[i] = static_cast<quint16>(stringListValue[i].toInt());
}
modbusWrapper.setMultipleRegister(startAddress,
static_cast<quint16>(writeQty),
uiHoldingRegisters);
ui->errorMonitoring->setText("No Error");
}
catch(const ServoException& exp)
{
3 years ago
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());
}
}