Browse Source

Add set Single Register and MultipleResister

test
nasicurious 3 years ago
parent
commit
a9fcd6ac17
  1. 2
      Servo/include/ModbusMaster.h
  2. 7
      Servo/include/ModbusWrapper.h
  3. 30
      Servo/src/ModbusMaster.cpp
  4. 24
      Servo/src/ModbusWrapper.cpp
  5. 36
      Test/MainWindow.cpp
  6. 2
      Test/MainWindow.h
  7. 2
      Test/MainWindow.ui

2
Servo/include/ModbusMaster.h

@ -48,6 +48,8 @@ public slots:
QVector<quint16> getInputRegister(int startAddress, quint16 readSize);
void setSingleCoil(int startAddress, bool coilFlag);
void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags);
void setSingleRegister(int startAddress, quint16 registerValue);
void setMultipleRegister(int startAddress, quint16 writeSize, QVector<quint16> registerValues);
};
#endif //MODBUSMASTER_H

7
Servo/include/ModbusWrapper.h

@ -29,6 +29,8 @@ public:
void setSingleCoil(int startAddress, bool coilFlag);
void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags);
void setSingleRegister(int startAddress, quint16 registerValue);
void setMultipleRegister(int startAddress, quint16 writeSize, QVector<quint16> registerValues);
signals:
void connectOrder(ModbusConfig modbusConfig);
@ -41,7 +43,10 @@ signals:
void setSingleCoilOrder(int startAddress, bool coilFlag);
void setMultipleCoilOrder(int startAddress, quint16 writeSize, QBitArray coilFlags);
void setSingleRegisterOrder(int startAddress, quint16 registerValue);
void setMultipleRegisterOrder(int startAddress,
quint16 writeSize,
QVector<quint16> registerValues);
//uncrustify off
public slots:
//uncrustify on

30
Servo/src/ModbusMaster.cpp

@ -203,6 +203,36 @@ void ModbusMaster::setMultipleCoil(int startAddress, quint16 writeSize, QBitArra
}
}
void ModbusMaster::setSingleRegister(int startAddress, quint16 registerValue)
{
try
{
_RegistersToWrite.resize(1);
_RegistersToWrite[0] = registerValue;
writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, 1);
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
}
}
/*************************************************************************************************/
void ModbusMaster::setMultipleRegister(int startAddress,
quint16 writeSize,
QVector<quint16> registerValues)
{
try
{
_RegistersToWrite = registerValues;
writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, writeSize);
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
}
}
/*************************************************************************************************/
void ModbusMaster::readRequest(QModbusDataUnit::RegisterType registerType,
int startAddress,

24
Servo/src/ModbusWrapper.cpp

@ -64,6 +64,16 @@ void ModbusWrapper::init()
&_modbusMaster,
&ModbusMaster::setMultipleCoil,
Qt::BlockingQueuedConnection);
connect(this,
&ModbusWrapper::setSingleRegisterOrder,
&_modbusMaster,
&ModbusMaster::setSingleRegister,
Qt::BlockingQueuedConnection);
connect(this,
&ModbusWrapper::setMultipleRegisterOrder,
&_modbusMaster,
&ModbusMaster::setMultipleRegister,
Qt::BlockingQueuedConnection);
emit initOrder();
@ -106,6 +116,20 @@ void ModbusWrapper::setMultipleCoil(int startAddress, quint16 writeSize, QBitArr
emit setMultipleCoilOrder(startAddress, writeSize, coilFlags);
}
/*************************************************************************************************/
void ModbusWrapper::setSingleRegister(int startAddress, quint16 registerValue)
{
emit setSingleRegisterOrder(startAddress, registerValue);
}
/*************************************************************************************************/
void ModbusWrapper::setMultipleRegister(int startAddress,
quint16 writeSize,
QVector<quint16> registerValues)
{
emit setMultipleRegisterOrder(startAddress, writeSize, registerValues);
}
/*************************************************************************************************/
void ModbusWrapper::connectToDevice(ModbusConfig modbusConfig)
{

36
Test/MainWindow.cpp

@ -41,6 +41,7 @@ void MainWindow::on_connect_clicked()
}
}
/*************************************************************************************************/
void MainWindow::on_readButton_clicked()
{
int startAddress = ui->startAddress->text().toInt();
@ -71,6 +72,7 @@ void MainWindow::on_readButton_clicked()
}
}
/*************************************************************************************************/
void MainWindow::printCoilsDataFromClient(QBitArray uiCoils)
{
QString readedData;
@ -83,6 +85,7 @@ void MainWindow::printCoilsDataFromClient(QBitArray uiCoils)
ui->textEditRead->setText(readedData);
}
/*************************************************************************************************/
void MainWindow::printRegisterDataFromClient(QVector<quint16> uiHoldingRegisters)
{
QString readedData;
@ -95,14 +98,14 @@ void MainWindow::printRegisterDataFromClient(QVector<quint16> uiHoldingRegisters
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());
}
/*************************************************************************************************/
void MainWindow::on_writeMultiCoil_clicked()
{
int startAddress = ui->writeMultiCoilStartAddress->text().toInt();
@ -121,3 +124,32 @@ void MainWindow::on_writeMultiCoil_clicked()
}
modbusWrapper.setMultipleCoil(startAddress, static_cast<quint16>(writeQty), uiCoils);
}
/*************************************************************************************************/
void MainWindow::on_writeSingleRegister_clicked()
{
int startAddress = ui->writeSingleRegisterAddress->text().toInt();
quint16 value = static_cast<quint16>(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<quint16> 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<quint16>(stringListValue[i].toInt());
}
modbusWrapper.setMultipleRegister(startAddress,
static_cast<quint16>(writeQty),
uiHoldingRegisters);
}

2
Test/MainWindow.h

@ -33,6 +33,8 @@ private slots:
void on_readButton_clicked();
void on_writeSingleCoil_clicked();
void on_writeMultiCoil_clicked();
void on_writeSingleRegister_clicked();
void on_writeMultiRegister_clicked();
};
#endif //MAINWINDOW_H

2
Test/MainWindow.ui

@ -343,7 +343,7 @@
<string>address:</string>
</property>
</widget>
<widget class="QLineEdit" name="writeSingleRegisterQty">
<widget class="QLineEdit" name="writeSingleRegisterValue">
<property name="geometry">
<rect>
<x>60</x>

Loading…
Cancel
Save