Browse Source

Add GetInputCoil

test
nasicurious 3 years ago
parent
commit
1ab62ee349
  1. 1
      Servo/include/ModbusMaster.h
  2. 2
      Servo/include/ModbusWrapper.h
  3. 31
      Servo/src/ModbusMaster.cpp
  4. 14
      Servo/src/ModbusWrapper.cpp
  5. 11
      Test/MainWindow.cpp

1
Servo/include/ModbusMaster.h

@ -39,6 +39,7 @@ public slots:
void connectToDevice(ModbusConfig modbusConfig);
void connectionStateChanged(QModbusDevice::State state);
QBitArray getCoil(int startAddress, quint16 readSize);
QBitArray getInputCoil(int startAddress, quint16 readSize);
};
#endif //MODBUSMASTER_H

2
Servo/include/ModbusWrapper.h

@ -22,11 +22,13 @@ public:
void connectToDevice(ModbusConfig modbusConfig);
void init();
QBitArray getCoil(int startAddress, quint16 readSize);
QBitArray getInputCoil(int startAddress, quint16 readSize);
signals:
void connectOrder(ModbusConfig modbusConfig);
void initOrder();
QBitArray getCoilOrder(int startAddress, quint16 readSize);
QBitArray getInputCoilOrder(int startAddress, quint16 readSize);
//uncrustify off
public slots:

31
Servo/src/ModbusMaster.cpp

@ -84,9 +84,36 @@ QBitArray ModbusMaster::getCoil(int startAddress, quint16 readSize)
for(uint i = 0; i < num; i++)
{
if(unit.value(static_cast<int>(i)) == 1)
coils.setBit(i);
coils.setBit(static_cast<int>(i));
else
coils.clearBit(i);
coils.clearBit(static_cast<int>(i));
}
return coils;
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
}
}
/*************************************************************************************************/
QBitArray ModbusMaster::getInputCoil(int startAddress, quint16 readSize)
{
try
{
readRequest(QModbusDataUnit::RegisterType::DiscreteInputs, startAddress, readSize);
auto unit = _modbusReplyUnit;
QBitArray coils;
auto num = unit.valueCount();
coils.resize(static_cast<int>(num));
for(uint i = 0; i < num; i++)
{
if(unit.value(static_cast<int>(i)) == 1)
coils.setBit(static_cast<int>(i));
else
coils.clearBit(static_cast<int>(i));
}
return coils;

14
Servo/src/ModbusWrapper.cpp

@ -38,6 +38,11 @@ void ModbusWrapper::init()
&modbusMaster,
&ModbusMaster::getCoil,
Qt::BlockingQueuedConnection);
connect(this,
&ModbusWrapper::getInputCoilOrder,
&modbusMaster,
&ModbusMaster::getInputCoil,
Qt::BlockingQueuedConnection);
emit initOrder();
@ -53,6 +58,15 @@ QBitArray ModbusWrapper::getCoil(int startAddress, quint16 readSize)
return result;
}
/*************************************************************************************************/
QBitArray ModbusWrapper::getInputCoil(int startAddress, quint16 readSize)
{
auto result = emit getInputCoilOrder(startAddress, readSize);
qDebug() << "getInputCoilOrder done";
return result;
}
/*************************************************************************************************/
void ModbusWrapper::connectToDevice(ModbusConfig modbusConfig)
{

11
Test/MainWindow.cpp

@ -51,11 +51,14 @@ void MainWindow::on_readButton_clicked()
printCoilsDataFromClient(modbusWrapper.getCoil(startAddress,
static_cast<quint16>(numberOfEntries)));
break;
//case 1: printCoilsDataFromSlave(modbusWrapper->getInputCoil(startAddress,static_cast<quint16>(numberOfEntries)));
//break;
//case 2: printRegisterDataFromSlave(modbusWrapper->getInputRegister(startAddress,static_cast<quint16>(numberOfEntries)));
case 1:
printCoilsDataFromClient(modbusWrapper.getInputCoil(startAddress,
static_cast<quint16>(numberOfEntries)));
break;
//case 2: printCoilsDataFromClient(modbusWrapper->getInputRegister(startAddress,static_cast<quint16>(numberOfEntries)));
//break;
//case 3: printRegisterDataFromSlave(modbusWrapper->getHoldingRegister(startAddress,static_cast<quint16>(numberOfEntries)));
//case 3: printCoilsDataFromClient(modbusWrapper->getHoldingRegister(startAddress,static_cast<quint16>(numberOfEntries)));
//break;
}
}

Loading…
Cancel
Save