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); void connectionStateChanged(QModbusDevice::State state);
QBitArray getCoil(int startAddress, quint16 readSize); QBitArray getCoil(int startAddress, quint16 readSize);
QBitArray getInputCoil(int startAddress, quint16 readSize); QBitArray getInputCoil(int startAddress, quint16 readSize);
QVector<quint16> getHoldingRegister(int startAddress, quint16 readSize);
}; };
#endif //MODBUSMASTER_H #endif //MODBUSMASTER_H

7
Servo/include/ModbusWrapper.h

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

34
Servo/src/ModbusWrapper.cpp

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

24
Test/MainWindow.cpp

@ -56,10 +56,14 @@ void MainWindow::on_readButton_clicked()
printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress, printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress,
static_cast<quint16>(numberOfEntries))); static_cast<quint16>(numberOfEntries)));
break; break;
//case 2: printCoilsDataFromClient(modbusWrapper->getInputRegister(startAddress,static_cast<quint16>(numberOfEntries)));
//break; //case 2: printCoilsDataFromClient(modbusWrapper.(startAddress,static_cast<quint16>(numberOfEntries)));
//case 3: printCoilsDataFromClient(modbusWrapper->getHoldingRegister(startAddress,static_cast<quint16>(numberOfEntries))); //break;
//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); 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(QWidget* parent = nullptr);
~MainWindow(); ~MainWindow();
void printCoilsDataFromClient(QBitArray uiCoils); void printCoilsDataFromClient(QBitArray uiCoils);
void printRegisterDataFromClient(QVector<quint16> uiHoldingRegisters);
//uncrustify off //uncrustify off
private slots: private slots:

Loading…
Cancel
Save