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> getHoldingRegister(int startAddress, quint16 readSize);
QVector<quint16> getInputRegister(int startAddress, quint16 readSize); QVector<quint16> getInputRegister(int startAddress, quint16 readSize);
void setSingleCoil(int startAddress, bool coilFlag); void setSingleCoil(int startAddress, bool coilFlag);
void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags);
}; };
#endif //MODBUSMASTER_H #endif //MODBUSMASTER_H

2
Servo/include/ModbusWrapper.h

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

11
Servo/src/ModbusWrapper.cpp

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

19
Test/MainWindow.cpp

@ -102,3 +102,22 @@ void MainWindow::on_writeSingleCoil_clicked()
uiCoils.resize(1); uiCoils.resize(1);
modbusWrapper.setSingleCoil(startAddress, ui->coilData->isChecked()); 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 //uncrustify on
void on_readButton_clicked(); void on_readButton_clicked();
void on_writeSingleCoil_clicked(); void on_writeSingleCoil_clicked();
void on_writeMultiCoil_clicked();
}; };
#endif //MAINWINDOW_H #endif //MAINWINDOW_H

298
Test/MainWindow.ui

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>800</width> <width>800</width>
<height>600</height> <height>507</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -157,7 +157,7 @@
<x>340</x> <x>340</x>
<y>70</y> <y>70</y>
<width>191</width> <width>191</width>
<height>111</height> <height>161</height>
</rect> </rect>
</property> </property>
<property name="title"> <property name="title">
@ -167,7 +167,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>10</x> <x>10</x>
<y>20</y> <y>30</y>
<width>61</width> <width>61</width>
<height>23</height> <height>23</height>
</rect> </rect>
@ -180,7 +180,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>70</x> <x>70</x>
<y>40</y> <y>60</y>
<width>113</width> <width>113</width>
<height>25</height> <height>25</height>
</rect> </rect>
@ -190,7 +190,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>10</x> <x>10</x>
<y>40</y> <y>60</y>
<width>71</width> <width>71</width>
<height>26</height> <height>26</height>
</rect> </rect>
@ -203,7 +203,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>20</x> <x>20</x>
<y>70</y> <y>120</y>
<width>141</width> <width>141</width>
<height>25</height> <height>25</height>
</rect> </rect>
@ -213,6 +213,289 @@
</property> </property>
</widget> </widget>
</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>groupBox</zorder>
<zorder>connect</zorder> <zorder>connect</zorder>
<zorder>label_5</zorder> <zorder>label_5</zorder>
@ -223,6 +506,9 @@
<zorder>writeTable</zorder> <zorder>writeTable</zorder>
<zorder>label_9</zorder> <zorder>label_9</zorder>
<zorder>groupBox_2</zorder> <zorder>groupBox_2</zorder>
<zorder>groupBox_3</zorder>
<zorder>groupBox_4</zorder>
<zorder>groupBox_5</zorder>
</widget> </widget>
<widget class="QMenuBar" name="menubar"> <widget class="QMenuBar" name="menubar">
<property name="geometry"> <property name="geometry">

Loading…
Cancel
Save