Skip to content

Commit 9cfa451

Browse files
committed
Add 'Copy' action type
1 parent 9bc4200 commit 9cfa451

File tree

11 files changed

+327
-28
lines changed

11 files changed

+327
-28
lines changed

bugfix.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ Fix 'ByteArray'-format DataView issue
2727

2828
# 0.1.4
2929

30-
Fixed a crash bug for the 'readCoils' and 'readDiscreteInputs' functions when the bit offset was not a multiple of 8
30+
Fixed a crash bug for the 'readCoils' and 'readDiscreteInputs' functions when the bit offset was not a multiple of 8
31+
32+
# 0.2.2
33+
34+
Fix Action value updates - Not Following Defined Register and Byte Order

changelog.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@
3333

3434
* Fix bugs
3535

36+
# 0.2.2
37+
38+
* Fixed bug
39+
3640
# 0.3.0
3741

3842
* Add Modbus Scanner
3943
* Add new Modbus functions: MASK_WRITE_REGISTER, READ_WRITE_MULTIPLE_REGISTERS
4044
* Improve 'Send Message' dialog window
4145
* Add all encodings suppoted by Qt framework to the 'String' data format
42-
* Add saving DataViewItem values for Client
46+
* Add possibility to save DataViewItem values in values when saving project for Client
47+
* Upgrade status bar with Tx/Rx statistic
48+
* Add `Copy` action type

src/server/gui/actions/server_actionsmodel.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ bool mbServerActionsModel::setData(const QModelIndex &index, const QVariant &val
152152
Qt::ItemFlags mbServerActionsModel::flags(const QModelIndex &index) const
153153
{
154154
Qt::ItemFlags f = QAbstractTableModel::flags(index);
155-
f |= Qt::ItemIsEditable;
155+
if (!mbServer::global()->isRunning())
156+
f |= Qt::ItemIsEditable;
156157
return f;
157158
}
158159

src/server/gui/dialogs/server_dialogaction.cpp

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ mbServerDialogAction::mbServerDialogAction(QWidget *parent) :
6969

7070
// Offset
7171
sp = ui->spOffset;
72-
sp->setMinimum(0);
73-
sp->setMaximum(USHRT_MAX);
72+
sp->setMinimum(1);
73+
sp->setMaximum(USHRT_MAX+1);
7474

7575
// Data type
7676
cmb = ui->cmbDataType;
@@ -90,7 +90,7 @@ mbServerDialogAction::mbServerDialogAction(QWidget *parent) :
9090
sp->setMinimum(0);
9191
sp->setMaximum(INT_MAX);
9292

93-
// Simulation type
93+
// Action type
9494
cmb = ui->cmbActionType;
9595
e = mb::metaEnum<mbServerAction::ActionType>();
9696
for (int i = 0; i < e.keyCount(); i++)
@@ -99,19 +99,35 @@ mbServerDialogAction::mbServerDialogAction(QWidget *parent) :
9999
cmb->setCurrentIndex(mbServerAction::Defaults::instance().actionType);
100100
setActionType(cmb->currentIndex());
101101

102-
// Simulation Increment
102+
// Action Increment
103103
ui->lnActionIncrement->setText(QString::number(d.incrementValue));
104104

105-
// Simulation Sine
105+
// Action Sine
106106
ui->lnActionSinePeriod->setText(QString::number(d.sinePeriod));
107107
ui->lnActionSinePhaseShift->setText(QString::number(d.sinePhaseShift));
108108
ui->lnActionSineAmplitude->setText(QString::number(d.sineAmplitude));
109109
ui->lnActionSineVerticalShift->setText(QString::number(d.sineVerticalShift));
110110

111-
// Simulation Random
111+
// Action Random
112112
ui->lnActionRandomMin->setText(QString::number(d.randomMin));
113113
ui->lnActionRandomMax->setText(QString::number(d.randomMax));
114114

115+
// Action Copy
116+
cmb = ui->cmbCopySourceAdrType;
117+
cmb->addItem(mb::toModbusMemoryTypeString(Modbus::Memory_0x));
118+
cmb->addItem(mb::toModbusMemoryTypeString(Modbus::Memory_1x));
119+
cmb->addItem(mb::toModbusMemoryTypeString(Modbus::Memory_3x));
120+
cmb->addItem(mb::toModbusMemoryTypeString(Modbus::Memory_4x));
121+
cmb->setCurrentIndex(2);
122+
123+
sp = ui->spCopySourceOffset;
124+
sp->setMinimum(1);
125+
sp->setMaximum(USHRT_MAX+1);
126+
127+
sp = ui->spCopySize;
128+
sp->setMinimum(0);
129+
sp->setMaximum(USHRT_MAX);
130+
115131
//--------------------- ADVANCED ---------------------
116132
// Byte Order
117133
cmb = ui->cmbByteOrder;
@@ -149,6 +165,10 @@ MBSETTINGS mbServerDialogAction::cachedSettings() const
149165
adr.type = mb::toModbusMemoryType(ui->cmbAdrType->currentText());
150166
adr.offset = static_cast<quint16>(ui->spOffset->value()-1);
151167

168+
mb::Address adrCopy;
169+
adrCopy.type = mb::toModbusMemoryType(ui->cmbCopySourceAdrType->currentText());
170+
adrCopy.offset = static_cast<quint16>(ui->spCopySourceOffset->value()-1);
171+
152172
m[prefix+vs.device ] = ui->cmbDevice->currentText();
153173
m[prefix+vs.address ] = mb::toInt(adr);
154174
m[prefix+vs.dataType ] = ui->cmbDataType->currentText();
@@ -160,6 +180,8 @@ MBSETTINGS mbServerDialogAction::cachedSettings() const
160180
m[prefix+vs.sineVerticalShift] = ui->lnActionSineVerticalShift->text();
161181
m[prefix+vs.randomMin ] = ui->lnActionRandomMin->text();
162182
m[prefix+vs.randomMax ] = ui->lnActionRandomMax->text();
183+
m[prefix+vs.copySourceAddress] = mb::toInt(adrCopy);
184+
m[prefix+vs.copySize ] = ui->spCopySize->value();
163185
m[prefix+vs.actionType ] = ui->cmbActionType->currentText();
164186
m[prefix+vs.byteOrder ] = ui->cmbByteOrder->currentText();
165187
m[prefix+vs.registerOrder ] = ui->cmbRegisterOrder->currentText();
@@ -186,6 +208,14 @@ void mbServerDialogAction::setCachedSettings(const MBSETTINGS &m)
186208
ui->spOffset->setValue(adr.offset+1);
187209
}
188210

211+
it = m.find(prefix+vs.copySourceAddress);
212+
if (it != end)
213+
{
214+
mb::Address adr = mb::toAddress(it.value().toInt());
215+
ui->cmbCopySourceAdrType->setCurrentText(mb::toModbusMemoryTypeString(adr.type));
216+
ui->spCopySourceOffset->setValue(adr.offset+1);
217+
}
218+
189219
it = m.find(prefix+vs.device ); if (it != end) ui->cmbDevice ->setCurrentText(it.value().toString());
190220
it = m.find(prefix+vs.dataType ); if (it != end) ui->cmbDataType->setCurrentText(it.value().toString());
191221
it = m.find(prefix+vs.period ); if (it != end) ui->spPeriod ->setValue (it.value().toInt() );
@@ -196,6 +226,7 @@ void mbServerDialogAction::setCachedSettings(const MBSETTINGS &m)
196226
it = m.find(prefix+vs.sineVerticalShift); if (it != end) ui->lnActionSineVerticalShift->setText(it.value().toString());
197227
it = m.find(prefix+vs.randomMin ); if (it != end) ui->lnActionRandomMin->setText(it.value().toString());
198228
it = m.find(prefix+vs.randomMax ); if (it != end) ui->lnActionRandomMax->setText(it.value().toString());
229+
it = m.find(prefix+vs.copySize ); if (it != end) ui->spCopySize->setValue(it.value().toInt());
199230
it = m.find(prefix+vs.actionType ); if (it != end) ui->cmbActionType->setCurrentText(mb::enumKey(mb::enumValue<mbServerAction::ActionType>(it.value())));
200231
it = m.find(prefix+vs.byteOrder ); if (it != end) fillFormByteOrder(mb::enumDataOrderValue(it.value()));
201232
it = m.find(prefix+vs.registerOrder ); if (it != end) fillFormRegisterOrder(mb::enumDataOrderValue(it.value()));
@@ -287,6 +318,14 @@ void mbServerDialogAction::fillFormActionType(const MBSETTINGS &settings)
287318
ui->lnActionRandomMin->setText(settings.value(sAction.randomMin).toString());
288319
ui->lnActionRandomMax->setText(settings.value(sAction.randomMax).toString());
289320
break;
321+
case mbServerAction::Copy:
322+
{
323+
mb::Address adr = mb::toAddress(settings.value(sAction.copySourceAddress).toInt());
324+
ui->cmbCopySourceAdrType->setCurrentText(mb::toModbusMemoryTypeString(adr.type));
325+
ui->spCopySourceOffset->setValue(adr.offset+1);
326+
ui->spCopySize->setValue(settings.value(sAction.copySize).toInt());
327+
}
328+
break;
290329
}
291330
ui->cmbActionType->setCurrentText(mb::enumKey<mbServerAction::ActionType>(t));
292331
}
@@ -350,6 +389,15 @@ void mbServerDialogAction::fillDataActionType(MBSETTINGS &settings)
350389
settings[sAction.randomMin] = ui->lnActionRandomMin->text();
351390
settings[sAction.randomMax] = ui->lnActionRandomMax->text();
352391
break;
392+
case mbServerAction::Copy:
393+
{
394+
mb::Address adr;
395+
adr.type = mb::toModbusMemoryType(ui->cmbCopySourceAdrType->currentText());
396+
adr.offset = static_cast<quint16>(ui->spCopySourceOffset->value()-1);
397+
settings[sAction.copySourceAddress] = mb::toInt(adr);
398+
settings[sAction.copySize ] = ui->spCopySize->value();
399+
}
400+
break;
353401
}
354402
settings[sAction.actionType] = t;
355403
}

src/server/gui/dialogs/server_dialogaction.ui

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
<item>
163163
<widget class="QStackedWidget" name="swActionType">
164164
<property name="currentIndex">
165-
<number>1</number>
165+
<number>0</number>
166166
</property>
167167
<widget class="QWidget" name="pgIncrement">
168168
<layout class="QFormLayout" name="formLayout_3">
@@ -254,6 +254,67 @@
254254
</item>
255255
</layout>
256256
</widget>
257+
<widget class="QWidget" name="pgCopy">
258+
<layout class="QFormLayout" name="formLayout_6">
259+
<item row="0" column="0">
260+
<widget class="QLabel" name="label_16">
261+
<property name="text">
262+
<string>Source</string>
263+
</property>
264+
</widget>
265+
</item>
266+
<item row="0" column="1">
267+
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="0,1">
268+
<item>
269+
<widget class="QComboBox" name="cmbCopySourceAdrType">
270+
<property name="sizePolicy">
271+
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
272+
<horstretch>0</horstretch>
273+
<verstretch>0</verstretch>
274+
</sizepolicy>
275+
</property>
276+
<property name="maximumSize">
277+
<size>
278+
<width>50</width>
279+
<height>16777215</height>
280+
</size>
281+
</property>
282+
</widget>
283+
</item>
284+
<item>
285+
<widget class="QSpinBox" name="spCopySourceOffset">
286+
<property name="minimum">
287+
<number>1</number>
288+
</property>
289+
<property name="maximum">
290+
<number>465535</number>
291+
</property>
292+
</widget>
293+
</item>
294+
</layout>
295+
</item>
296+
<item row="1" column="0">
297+
<widget class="QLabel" name="label_17">
298+
<property name="text">
299+
<string>Size</string>
300+
</property>
301+
</widget>
302+
</item>
303+
<item row="1" column="1">
304+
<widget class="QSpinBox" name="spCopySize">
305+
<property name="minimum">
306+
<number>1</number>
307+
</property>
308+
<property name="maximum">
309+
<number>65536</number>
310+
</property>
311+
<property name="value">
312+
<number>1</number>
313+
</property>
314+
</widget>
315+
</item>
316+
</layout>
317+
</widget>
257318
</widget>
258319
</item>
259320
</layout>

src/server/project/server_action.cpp

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ mbServerAction::Strings::Strings() :
3737
sineAmplitude (QStringLiteral("sineAmplitude")),
3838
sineVerticalShift(QStringLiteral("sineVerticalShift")),
3939
randomMin (QStringLiteral("randomMin")),
40-
randomMax (QStringLiteral("randomMax"))
40+
randomMax (QStringLiteral("randomMax")),
41+
copySourceAddress(QStringLiteral("sourceAddress")),
42+
copySize (QStringLiteral("size"))
4143
{
4244
}
4345

@@ -61,7 +63,9 @@ mbServerAction::Defaults::Defaults() :
6163
sineAmplitude (100),
6264
sineVerticalShift(0),
6365
randomMin (0),
64-
randomMax (100)
66+
randomMax (100),
67+
copySourceAddress(300001),
68+
copySize (1)
6569
{
6670
}
6771

@@ -235,9 +239,7 @@ void mbServerAction::setExtendedSettingsStr(const QString &settings)
235239
MBSETTINGS mbServerAction::settings() const
236240
{
237241
MBSETTINGS p = commonSettings();
238-
MBSETTINGS e = extendedSettings();
239-
for (MBSETTINGS::const_iterator it = e.constBegin(); it != e.constEnd(); it++)
240-
p.insert(it.key(), it.value());
242+
mb::unite(p, extendedSettings());
241243
return p;
242244
}
243245

@@ -314,6 +316,9 @@ void mbServerAction::setNewActionExtended(ActionType actionType)
314316
case Random:
315317
m_actionExtended = new ActionRandom;
316318
break;
319+
case Copy:
320+
m_actionExtended = new ActionCopy;
321+
break;
317322
default:
318323
return;
319324
}
@@ -450,3 +455,39 @@ QString mbServerAction::ActionRandom::extendedSettingsStr() const
450455
s.randomMax, max.toString());
451456
}
452457

458+
// -----------------------------------------------------------------------------------------------------------------------
459+
// --------------------------------------------------------- COPY --------------------------------------------------------
460+
// -----------------------------------------------------------------------------------------------------------------------
461+
462+
MBSETTINGS mbServerAction::ActionCopy::extendedSettings() const
463+
{
464+
const Strings &s = Strings::instance();
465+
MBSETTINGS p;
466+
p[s.copySourceAddress] = mb::toInt(sourceAddress);
467+
p[s.copySize ] = size;
468+
return p;
469+
}
470+
471+
void mbServerAction::ActionCopy::setExtendedSettings(const MBSETTINGS &settings)
472+
{
473+
const Strings &s = Strings::instance();
474+
475+
MBSETTINGS::const_iterator it;
476+
auto end = settings.end();
477+
478+
it = settings.find(s.copySourceAddress);
479+
if (it != end)
480+
sourceAddress = mb::toAddress(it.value().toInt());
481+
482+
it = settings.find(s.copySize);
483+
if (it != end)
484+
size = static_cast<quint16>(it.value().toUInt());
485+
}
486+
487+
QString mbServerAction::ActionCopy::extendedSettingsStr() const
488+
{
489+
const Strings &s = Strings::instance();
490+
return QString("%1=%2;%3=%4").arg(s.copySourceAddress, mb::toString(sourceAddress),
491+
s.copySize , QString::number(size));
492+
}
493+

src/server/project/server_action.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class mbServerAction : public QObject
3636
{
3737
Increment,
3838
Sine,
39-
Random
39+
Random,
40+
Copy
4041
};
4142
Q_ENUM(ActionType)
4243

@@ -57,6 +58,8 @@ class mbServerAction : public QObject
5758
const QString sineVerticalShift;
5859
const QString randomMin ;
5960
const QString randomMax ;
61+
const QString copySourceAddress;
62+
const QString copySize ;
6063

6164
Strings();
6265
static const Strings &instance();
@@ -78,6 +81,8 @@ class mbServerAction : public QObject
7881
const int sineVerticalShift;
7982
const int randomMin ;
8083
const int randomMax ;
84+
const int copySourceAddress;
85+
const quint16 copySize ;
8186

8287
Defaults();
8388
static const Defaults &instance();
@@ -203,6 +208,23 @@ class mbServerAction : public QObject
203208
}
204209
};
205210

211+
struct ActionCopy : public ActionExtended
212+
{
213+
mb::Address sourceAddress;
214+
quint16 size;
215+
216+
MBSETTINGS extendedSettings() const override;
217+
void setExtendedSettings(const MBSETTINGS &settings) override;
218+
QString extendedSettingsStr() const override;
219+
220+
ActionCopy()
221+
{
222+
Defaults d = Defaults::instance();
223+
sourceAddress = mb::toAddress(d.copySourceAddress);
224+
size = d.copySize;
225+
}
226+
};
227+
206228
private:
207229
void setNewActionExtended(ActionType actionType);
208230

0 commit comments

Comments
 (0)