Skip to content

Commit

Permalink
Added min/max params for simulation action of Increment
Browse files Browse the repository at this point in the history
  • Loading branch information
serhmarch committed Nov 5, 2024
1 parent d86f0ab commit 5bea792
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/client/runtime/client_devicerunnable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ bool mbClientDeviceRunnable::hasReadMessageOnDuty()
for (Messages_t::Iterator it = m_readMessages.begin(); it != m_readMessages.end(); ++it)
{
mbClientRunMessagePtr m = *it;
if ((tm - m->timestamp()) >= m->period())
if ((tm - m->beginTimestamp()) >= m->period())
{
m_readMessages.erase(it); // remove it from queue ...
m_readMessages.enqueue(m); // and push it to back of queue
Expand Down
10 changes: 9 additions & 1 deletion src/client/runtime/client_runmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mbClientRunMessage::mbClientRunMessage(mbClientRunItem *item, uint16_t maxCount,
m_writeCount = 0;
m_period = item->period();
m_status = Modbus::Status_Uncertain;
m_beginTimestamp = 0;
m_timestamp = 0;
addItemPrivate(item);
m_deleteItems = false;
Expand All @@ -58,6 +59,7 @@ mbClientRunMessage::mbClientRunMessage(uint16_t offset, uint16_t count, uint16_t
m_period = 0;
m_maxCount = maxCount;
m_status = Modbus::Status_Uncertain;
m_beginTimestamp = 0;
m_timestamp = 0;
m_deleteItems = false;
m_isCompleted = false;
Expand Down Expand Up @@ -117,6 +119,11 @@ Modbus::StatusCode mbClientRunMessage::status() const
return m_status;
}

mb::Timestamp_t mbClientRunMessage::beginTimestamp() const
{
return m_beginTimestamp;
}

mb::Timestamp_t mbClientRunMessage::timestamp() const
{
return m_timestamp;
Expand Down Expand Up @@ -174,7 +181,7 @@ Modbus::StatusCode mbClientRunMessage::setData(uint16_t /*innerOffset*/, uint16_

void mbClientRunMessage::prepareToSend()
{
// base implementation does nothing
m_beginTimestamp = mb::currentTimestamp();
}

void mbClientRunMessage::setComplete(Modbus::StatusCode status, mb::Timestamp_t timestamp)
Expand Down Expand Up @@ -280,6 +287,7 @@ void mbClientRunMessageRead::setComplete(Modbus::StatusCode status, mb::Timestam

void mbClientRunMessageWrite::prepareToSend()
{
mbClientRunMessage::prepareToSend();
for (Items_t::ConstIterator it = m_items.cbegin(); it != m_items.cend(); ++it)
{
mbClientRunItem *pItem = static_cast<mbClientRunItem*>(*it);
Expand Down
2 changes: 2 additions & 0 deletions src/client/runtime/client_runmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class mbClientRunMessage : public QObject
int innerBufferBitSize() const;
int innerBufferRegSize() const;
Modbus::StatusCode status() const;
mb::Timestamp_t beginTimestamp() const;
mb::Timestamp_t timestamp() const;

public:
Expand Down Expand Up @@ -111,6 +112,7 @@ class mbClientRunMessage : public QObject
uint32_t m_period;
uint16_t m_maxCount;
Modbus::StatusCode m_status;
mb::Timestamp_t m_beginTimestamp;
mb::Timestamp_t m_timestamp;
uint8_t m_buff[MB_MAX_BYTES];

Expand Down
3 changes: 2 additions & 1 deletion src/core/sdk/mbcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ QString toString(Modbus::StatusCode status)
case Modbus::Status_BadSerialOpen : return QStringLiteral("BadSerialOpen");
case Modbus::Status_BadSerialWrite : return QStringLiteral("BadSerialWrite");
case Modbus::Status_BadSerialRead : return QStringLiteral("BadSerialRead");
case Modbus::Status_BadSerialReadTimeout : return QStringLiteral("Status_BadSerialReadTimeout");
case Modbus::Status_BadSerialReadTimeout : return QStringLiteral("BadSerialReadTimeout");
case Modbus::Status_BadSerialWriteTimeout : return QStringLiteral("BadSerialWriteTimeout");
case Modbus::Status_BadAscMissColon : return QStringLiteral("BadAscMissColon");
case Modbus::Status_BadAscMissCrLf : return QStringLiteral("BadAscMissCrLf");
case Modbus::Status_BadAscChar : return QStringLiteral("BadAscChar");
Expand Down
10 changes: 10 additions & 0 deletions src/server/gui/dialogs/server_dialogaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ mbServerDialogAction::mbServerDialogAction(QWidget *parent) :

// Action Increment
ui->lnActionIncrement->setText(QString::number(d.incrementValue));
ui->lnActionIncrementMin->setText(QString::number(d.incrementMin));
ui->lnActionIncrementMax->setText(QString::number(d.incrementMax));

// Action Sine
ui->lnActionSinePeriod->setText(QString::number(d.sinePeriod));
Expand Down Expand Up @@ -174,6 +176,8 @@ MBSETTINGS mbServerDialogAction::cachedSettings() const
m[prefix+vs.dataType ] = ui->cmbDataType->currentText();
m[prefix+vs.period ] = ui->spPeriod->value();
m[prefix+vs.incrementValue ] = ui->lnActionIncrement->text();
m[prefix+vs.incrementMin ] = ui->lnActionIncrementMin->text();
m[prefix+vs.incrementMax ] = ui->lnActionIncrementMax->text();
m[prefix+vs.sinePeriod ] = ui->lnActionSinePeriod->text();
m[prefix+vs.sinePhaseShift ] = ui->lnActionSinePhaseShift->text();
m[prefix+vs.sineAmplitude ] = ui->lnActionSineAmplitude->text();
Expand Down Expand Up @@ -220,6 +224,8 @@ void mbServerDialogAction::setCachedSettings(const MBSETTINGS &m)
it = m.find(prefix+vs.dataType ); if (it != end) ui->cmbDataType->setCurrentText(it.value().toString());
it = m.find(prefix+vs.period ); if (it != end) ui->spPeriod ->setValue (it.value().toInt() );
it = m.find(prefix+vs.incrementValue ); if (it != end) ui->lnActionIncrement->setText(it.value().toString());
it = m.find(prefix+vs.incrementMin ); if (it != end) ui->lnActionIncrementMin->setText(it.value().toString());
it = m.find(prefix+vs.incrementMax ); if (it != end) ui->lnActionIncrementMax->setText(it.value().toString());
it = m.find(prefix+vs.sinePeriod ); if (it != end) ui->lnActionSinePeriod->setText(it.value().toString());
it = m.find(prefix+vs.sinePhaseShift ); if (it != end) ui->lnActionSinePhaseShift->setText(it.value().toString());
it = m.find(prefix+vs.sineAmplitude ); if (it != end) ui->lnActionSineAmplitude->setText(it.value().toString());
Expand Down Expand Up @@ -339,6 +345,8 @@ void mbServerDialogAction::fillFormActionType(const MBSETTINGS &settings)
{
case mbServerAction::Increment:
ui->lnActionIncrement->setText(settings.value(sItem.incrementValue).toString());
ui->lnActionIncrementMin->setText(settings.value(sItem.incrementMin).toString());
ui->lnActionIncrementMax->setText(settings.value(sItem.incrementMax).toString());
break;
case mbServerAction::Sine:
it = settings.find(sItem.sinePeriod ); if (it != end) ui->lnActionSinePeriod ->setText(it.value().toString());
Expand Down Expand Up @@ -417,6 +425,8 @@ void mbServerDialogAction::fillDataActionType(MBSETTINGS &settings)
{
case mbServerAction::Increment:
settings[sItem.incrementValue] = ui->lnActionIncrement->text();
settings[sItem.incrementMin ] = ui->lnActionIncrementMin->text();
settings[sItem.incrementMax ] = ui->lnActionIncrementMax->text();
break;
case mbServerAction::Sine:
settings[sItem.sinePeriod ] = ui->lnActionSinePeriod->text();
Expand Down
22 changes: 21 additions & 1 deletion src/server/gui/dialogs/server_dialogaction.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>242</width>
<height>450</height>
<height>454</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -176,6 +176,26 @@
<item row="0" column="1">
<widget class="QLineEdit" name="lnActionIncrement"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_19">
<property name="text">
<string>Minimum</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lnActionIncrementMin"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_18">
<property name="text">
<string>Maximum</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lnActionIncrementMax"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="pgSine">
Expand Down
19 changes: 18 additions & 1 deletion src/server/project/server_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ mbServerAction::Strings::Strings() :
registerOrder (QStringLiteral("registerOrder")),
extended (QStringLiteral("extended")),
incrementValue (QStringLiteral("incrementValue")),
incrementMin (QStringLiteral("min")),
incrementMax (QStringLiteral("max")),
sinePeriod (QStringLiteral("sinePeriod")),
sinePhaseShift (QStringLiteral("sinePhaseShift")),
sineAmplitude (QStringLiteral("sineAmplitude")),
Expand All @@ -59,6 +61,8 @@ mbServerAction::Defaults::Defaults() :
byteOrder (mb::LessSignifiedFirst),
registerOrder (mb::LessSignifiedFirst),
incrementValue (1),
incrementMin (0),
incrementMax (65535),
sinePeriod (10000),
sinePhaseShift (0),
sineAmplitude (100),
Expand Down Expand Up @@ -349,6 +353,8 @@ MBSETTINGS mbServerAction::ActionIncrement::extendedSettings() const
const Strings &s = Strings::instance();
MBSETTINGS p;
p[s.incrementValue] = value;
p[s.incrementMin ] = min ;
p[s.incrementMax ] = max ;
return p;
}

Expand All @@ -357,15 +363,26 @@ void mbServerAction::ActionIncrement::setExtendedSettings(const MBSETTINGS &sett
const Strings &s = Strings::instance();

auto end = settings.end();

auto it = settings.find(s.incrementValue);
if (it != end)
value = it.value();

it = settings.find(s.incrementMin);
if (it != end)
min = it.value();

it = settings.find(s.incrementMax);
if (it != end)
max = it.value();
}

QString mbServerAction::ActionIncrement::extendedSettingsStr() const
{
const Strings &s = Strings::instance();
return QString("%1=%2").arg(s.incrementValue, value.toString());
return QString("%1=%2;%3=%4;%5=%6").arg(s.incrementValue, value.toString(),
s.incrementMin, min.toString(),
s.incrementMax, max.toString());
}

// -----------------------------------------------------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions src/server/project/server_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class mbServerAction : public QObject
const QString registerOrder ;
const QString extended ;
const QString incrementValue ;
const QString incrementMin ;
const QString incrementMax ;
const QString sinePeriod ;
const QString sinePhaseShift ;
const QString sineAmplitude ;
Expand All @@ -76,6 +78,8 @@ class mbServerAction : public QObject
const mb::DataOrder byteOrder ;
const mb::DataOrder registerOrder ;
const int incrementValue ;
const int incrementMin ;
const int incrementMax ;
const int sinePeriod ;
const int sinePhaseShift ;
const int sineAmplitude ;
Expand Down Expand Up @@ -161,13 +165,17 @@ class mbServerAction : public QObject
struct ActionIncrement : public ActionExtended
{
QVariant value;
QVariant min;
QVariant max;
MBSETTINGS extendedSettings() const override;
void setExtendedSettings(const MBSETTINGS &settings) override;
QString extendedSettingsStr() const override;
ActionIncrement()
{
Defaults d = Defaults::instance();
value = d.incrementValue;
min = d.incrementMin;
max = d.incrementMax;
}
};

Expand Down
6 changes: 6 additions & 0 deletions src/server/runtime/server_runaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class mbServerRunActionIncrement : public mbServerRunActionT<T>
mbServerRunActionIncrement(const MBSETTINGS &settings) : mbServerRunActionT<T>(settings)
{
m_increment = settings.value(mbServerAction::Strings::instance().incrementValue).value<T>();
m_min = settings.value(mbServerAction::Strings::instance().incrementMin).value<T>();
m_max = settings.value(mbServerAction::Strings::instance().incrementMax).value<T>();
}

public:
Expand All @@ -87,6 +89,8 @@ class mbServerRunActionIncrement : public mbServerRunActionT<T>
T t = v.value<T>();
mbServerRunAction::trySwap(&t, sizeof(t));
t += m_increment;
if ((t < m_min) || (t > m_max))
t = m_min;
mbServerRunAction::trySwap(&t, sizeof(t));
this->setValue(t);
this->m_last = time;
Expand All @@ -96,6 +100,8 @@ class mbServerRunActionIncrement : public mbServerRunActionT<T>

private:
T m_increment;
T m_min;
T m_max;
};

template <typename T>
Expand Down

0 comments on commit 5bea792

Please sign in to comment.