Browse Source

Add SetMultipleCoil

test
nasicurious 3 years ago
parent
commit
0d9591930a
  1. 1
      Servo/include/ModbusMaster.h
  2. 2
      Servo/include/ModbusWrapper.h
  3. 13
      Servo/src/ModbusMaster.cpp
  4. 11
      Servo/src/ModbusWrapper.cpp
  5. 19
      Test/MainWindow.cpp
  6. 1
      Test/MainWindow.h
  7. 298
      Test/MainWindow.ui

1
Servo/include/ModbusMaster.h

@ -47,6 +47,7 @@ public slots:
QVector<quint16> getHoldingRegister(int startAddress, quint16 readSize);
QVector<quint16> getInputRegister(int startAddress, quint16 readSize);
void setSingleCoil(int startAddress, bool coilFlag);
void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags);
};
#endif //MODBUSMASTER_H

2
Servo/include/ModbusWrapper.h

@ -28,6 +28,7 @@ public:
QVector<quint16> getInputRegister(int startAddress, quint16 readSize);
void setSingleCoil(int startAddress, bool coilFlag);
void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags);
signals:
void connectOrder(ModbusConfig modbusConfig);
@ -39,6 +40,7 @@ signals:
QVector<quint16> getInputRegisterOrder(int startAddress, quint16 readSize);
void setSingleCoilOrder(int startAddress, bool coilFlag);
void setMultipleCoilOrder(int startAddress, quint16 writeSize, QBitArray coilFlags);
//uncrustify off
public slots:

13
Servo/src/ModbusMaster.cpp

@ -190,6 +190,19 @@ void ModbusMaster::setSingleCoil(int startAddress, bool coilFlag)
}
}
void ModbusMaster::setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags)
{
try
{
_coilsToWrite = coilFlags;
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, writeSize);
}
catch(const ServoException& ex)
{
qDebug() << ex.what();
}
}
/*************************************************************************************************/
void ModbusMaster::readRequest(QModbusDataUnit::RegisterType registerType,
int startAddress,

11
Servo/src/ModbusWrapper.cpp

@ -59,6 +59,11 @@ void ModbusWrapper::init()
&_modbusMaster,
&ModbusMaster::setSingleCoil,
Qt::BlockingQueuedConnection);
connect(this,
&ModbusWrapper::setMultipleCoilOrder,
&_modbusMaster,
&ModbusMaster::setMultipleCoil,
Qt::BlockingQueuedConnection);
emit initOrder();
@ -95,6 +100,12 @@ void ModbusWrapper::setSingleCoil(int startAddress, bool coilFlag)
emit setSingleCoilOrder(startAddress, coilFlag);
}
/*************************************************************************************************/
void ModbusWrapper::setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags)
{
emit setMultipleCoilOrder(startAddress, writeSize, coilFlags);
}
/*************************************************************************************************/
void ModbusWrapper::connectToDevice(ModbusConfig modbusConfig)
{

19
Test/MainWindow.cpp

@ -102,3 +102,22 @@ void MainWindow::on_writeSingleCoil_clicked()
uiCoils.resize(1);
modbusWrapper.setSingleCoil(startAddress, ui->coilData->isChecked());
}
void MainWindow::on_writeMultiCoil_clicked()
{
int startAddress = ui->writeMultiCoilStartAddress->text().toInt();
int writeQty = ui->writeMultiCoilQty->text().toInt();
QString valueToWrite = ui->writeMultiCoilValue->text();
QBitArray uiCoils;
uiCoils.resize(writeQty);
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);
}

1
Test/MainWindow.h

@ -32,6 +32,7 @@ private slots:
//uncrustify on
void on_readButton_clicked();
void on_writeSingleCoil_clicked();
void on_writeMultiCoil_clicked();
};
#endif //MAINWINDOW_H

298
Test/MainWindow.ui

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
<height>507</height>
</rect>
</property>
<property name="windowTitle">
@ -157,7 +157,7 @@
<x>340</x>
<y>70</y>
<width>191</width>
<height>111</height>
<height>161</height>
</rect>
</property>
<property name="title">
@ -167,7 +167,7 @@
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<y>30</y>
<width>61</width>
<height>23</height>
</rect>
@ -180,7 +180,7 @@
<property name="geometry">
<rect>
<x>70</x>
<y>40</y>
<y>60</y>
<width>113</width>
<height>25</height>
</rect>
@ -190,7 +190,7 @@
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<y>60</y>
<width>71</width>
<height>26</height>
</rect>
@ -203,7 +203,7 @@
<property name="geometry">
<rect>
<x>20</x>
<y>70</y>
<y>120</y>
<width>141</width>
<height>25</height>
</rect>
@ -213,6 +213,289 @@
</property>
</widget>
</widget>
<widget class="QGroupBox" name="groupBox_3">
<property name="geometry">
<rect>
<x>540</x>
<y>70</y>
<width>241</width>
<height>161</height>
</rect>
</property>
<property name="title">
<string>Write Multiple Coil</string>
</property>
<widget class="QLabel" name="label_8">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>126</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>Number of values:</string>
</property>
</widget>
<widget class="QLineEdit" name="writeMultiCoilStartAddress">
<property name="geometry">
<rect>
<x>110</x>
<y>30</y>
<width>121</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_10">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>126</width>
<height>26</height>
</rect>
</property>
<property name="text">
<string>Start address:</string>
</property>
</widget>
<widget class="QLineEdit" name="writeMultiCoilQty">
<property name="geometry">
<rect>
<x>140</x>
<y>60</y>
<width>91</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="writeMultiCoilValue">
<property name="geometry">
<rect>
<x>60</x>
<y>90</y>
<width>171</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="Values">
<property name="geometry">
<rect>
<x>10</x>
<y>90</y>
<width>51</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>Values</string>
</property>
</widget>
<widget class="QPushButton" name="writeMultiCoil">
<property name="geometry">
<rect>
<x>50</x>
<y>130</y>
<width>141</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>Write Multi Coil</string>
</property>
</widget>
</widget>
<widget class="QGroupBox" name="groupBox_4">
<property name="geometry">
<rect>
<x>340</x>
<y>250</y>
<width>181</width>
<height>201</height>
</rect>
</property>
<property name="title">
<string>Write Single Register</string>
</property>
<widget class="QLineEdit" name="writeSingleRegisterAddress">
<property name="geometry">
<rect>
<x>70</x>
<y>30</y>
<width>101</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_11">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>71</width>
<height>26</height>
</rect>
</property>
<property name="text">
<string>address:</string>
</property>
</widget>
<widget class="QLineEdit" name="writeSingleRegisterQty">
<property name="geometry">
<rect>
<x>60</x>
<y>70</y>
<width>111</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="Values_2">
<property name="geometry">
<rect>
<x>10</x>
<y>70</y>
<width>51</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>Value</string>
</property>
</widget>
<widget class="QPushButton" name="writeSingleRegister">
<property name="geometry">
<rect>
<x>10</x>
<y>110</y>
<width>161</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>Write Single Register</string>
</property>
</widget>
</widget>
<widget class="QGroupBox" name="groupBox_5">
<property name="geometry">
<rect>
<x>530</x>
<y>250</y>
<width>261</width>
<height>201</height>
</rect>
</property>
<property name="title">
<string>Write Multiple Register</string>
</property>
<widget class="QPushButton" name="writeMultiRegister">
<property name="geometry">
<rect>
<x>50</x>
<y>170</y>
<width>141</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>Write Multi Register</string>
</property>
</widget>
<widget class="QLabel" name="Values_3">
<property name="geometry">
<rect>
<x>10</x>
<y>110</y>
<width>51</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>Values</string>
</property>
</widget>
<widget class="QLabel" name="label_12">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>126</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>Number of values:</string>
</property>
</widget>
<widget class="QLineEdit" name="writeMultiRegisterStartAddress">
<property name="geometry">
<rect>
<x>110</x>
<y>30</y>
<width>121</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="writeMultiRegisterQty">
<property name="geometry">
<rect>
<x>140</x>
<y>60</y>
<width>91</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="writeMultiRegisterValue">
<property name="geometry">
<rect>
<x>60</x>
<y>110</y>
<width>171</width>
<height>51</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_13">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>126</width>
<height>26</height>
</rect>
</property>
<property name="text">
<string>Start address:</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>90</x>
<y>90</y>
<width>91</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Ubuntu Condensed</family>
<pointsize>8</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Comma Delimiter</string>
</property>
</widget>
</widget>
<zorder>groupBox</zorder>
<zorder>connect</zorder>
<zorder>label_5</zorder>
@ -223,6 +506,9 @@
<zorder>writeTable</zorder>
<zorder>label_9</zorder>
<zorder>groupBox_2</zorder>
<zorder>groupBox_3</zorder>
<zorder>groupBox_4</zorder>
<zorder>groupBox_5</zorder>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">

Loading…
Cancel
Save