Browse Source

Add GetHoldingRegister

test
nasicurious 3 years ago
parent
commit
1059eb2c8b
  1. 1
      Servo/include/ModbusMaster.h
  2. 7
      Servo/include/ModbusWrapper.h
  3. 25
      Servo/src/ModbusMaster.cpp
  4. 34
      Servo/src/ModbusWrapper.cpp
  5. 24
      Test/MainWindow.cpp
  6. 1
      Test/MainWindow.h

1
Servo/include/ModbusMaster.h

@ -40,6 +40,7 @@ public slots:
void connectionStateChanged(QModbusDevice::State state);
QBitArray getCoil(int startAddress, quint16 readSize);
QBitArray getInputCoil(int startAddress, quint16 readSize);
QVector<quint16> getHoldingRegister(int startAddress, quint16 readSize);
};
#endif //MODBUSMASTER_H

7
Servo/include/ModbusWrapper.h

@ -13,8 +13,8 @@ class ModbusWrapper : public QObject
Q_OBJECT
private:
ModbusMaster modbusMaster;
QThread workerThread;
ModbusMaster _modbusMaster;
QThread _workerThread;
public:
explicit ModbusWrapper(QObject* parent = nullptr);
@ -23,12 +23,15 @@ public:
void init();
QBitArray getCoil(int startAddress, quint16 readSize);
QBitArray getInputCoil(int startAddress, quint16 readSize);
QVector<quint16> getHoldingRegister(int startAddress, quint16 readSize);
signals:
void connectOrder(ModbusConfig modbusConfig);
void initOrder();
QBitArray getCoilOrder(int startAddress, quint16 readSize);
QBitArray getInputCoilOrder(int startAddress, quint16 readSize);
QVector<quint16> getHoldingRegisterOrder(int startAddress,
quint16 readSize);
//uncrustify off
public slots:

25
Servo/src/ModbusMaster.cpp

@ -124,6 +124,31 @@ QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize)
}
}
/*************************************************************************************************/
QVector<quint16> ModbusMaster::getHoldingRegister(int startAddress, quint16 readSize)
{
try
{
readRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, readSize);
auto unit = _modbusReplyUnit;
QVector<quint16> registerValues;
auto num = unit.valueCount();
registerValues.resize(static_cast<int>(num));
for(uint i = 0; i < unit.valueCount(); i++)
{
registerValues[static_cast<int>(i)] = unit.value(static_cast<int>(i));
}
return registerValues;
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
}
}
/*************************************************************************************************/
void ModbusMaster::readRequest(QModbusDataUnit::RegisterType registerType,
int startAddress,

34
Servo/src/ModbusWrapper.cpp

@ -8,41 +8,46 @@ ModbusWrapper::ModbusWrapper(QObject* parent) : QObject(parent)
/*************************************************************************************************/
ModbusWrapper::~ModbusWrapper()
{
workerThread.quit();
workerThread.wait();
_workerThread.quit();
_workerThread.wait();
}
/*************************************************************************************************/
void ModbusWrapper::init()
{
modbusMaster.moveToThread(&workerThread);
workerThread.setObjectName("workerThread");
workerThread.start();
_modbusMaster.moveToThread(&_workerThread);
_workerThread.setObjectName("workerThread");
_workerThread.start();
QEventLoop loop;
connect(&workerThread, &QThread::started, &loop, &QEventLoop::quit);
connect(&_workerThread, &QThread::started, &loop, &QEventLoop::quit);
loop.exec();
connect(this,
&ModbusWrapper::initOrder,
&modbusMaster,
&_modbusMaster,
&ModbusMaster::init,
Qt::BlockingQueuedConnection);
connect(this,
&ModbusWrapper::connectOrder,
&modbusMaster,
&_modbusMaster,
&ModbusMaster::connectToDevice,
Qt::BlockingQueuedConnection);
connect(this,
&ModbusWrapper::getCoilOrder,
&modbusMaster,
&_modbusMaster,
&ModbusMaster::getCoil,
Qt::BlockingQueuedConnection);
connect(this,
&ModbusWrapper::getInputCoilOrder,
&modbusMaster,
&_modbusMaster,
&ModbusMaster::getInputCoil,
Qt::BlockingQueuedConnection);
connect(this,
&ModbusWrapper::getHoldingRegisterOrder,
&_modbusMaster,
&ModbusMaster::getHoldingRegister,
Qt::BlockingQueuedConnection);
emit initOrder();
@ -67,6 +72,15 @@ QBitArray ModbusWrapper::getInputCoil(int startAddress, quint16 readSize)
return result;
}
/*************************************************************************************************/
QVector<quint16> ModbusWrapper::getHoldingRegister(int startAddress, quint16 readSize)
{
auto result = emit getHoldingRegisterOrder(startAddress, readSize);
qDebug() << "getHoldingRegister done";
return result;
}
/*************************************************************************************************/
void ModbusWrapper::connectToDevice(ModbusConfig modbusConfig)
{

24
Test/MainWindow.cpp

@ -56,10 +56,14 @@ void MainWindow::on_readButton_clicked()
printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress,
static_cast<quint16>(numberOfEntries)));
break;
//case 2: printCoilsDataFromClient(modbusWrapper->getInputRegister(startAddress,static_cast<quint16>(numberOfEntries)));
//break;
//case 3: printCoilsDataFromClient(modbusWrapper->getHoldingRegister(startAddress,static_cast<quint16>(numberOfEntries)));
//break;
//case 2: printCoilsDataFromClient(modbusWrapper.(startAddress,static_cast<quint16>(numberOfEntries)));
//break;
case 3:
printRegisterDataFromClient(modbusWrapper.getHoldingRegister(startAddress,
static_cast<quint16>(
numberOfEntries)));
break;
}
}
@ -74,3 +78,15 @@ void MainWindow::printCoilsDataFromClient(QBitArray uiCoils)
}
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);
}

1
Test/MainWindow.h

@ -24,6 +24,7 @@ public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
void printCoilsDataFromClient(QBitArray uiCoils);
void printRegisterDataFromClient(QVector<quint16> uiHoldingRegisters);
//uncrustify off
private slots:

Loading…
Cancel
Save