Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
sanny committed Sep 6, 2023
2 parents 9a8e4a2 + 1791e4f commit 3d1aee0
Show file tree
Hide file tree
Showing 20 changed files with 452 additions and 28 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Open ModSim
Open ModSim is a free implimentation of modbus slave (server) utility for modbus-tcp and modbus-rtu protocols.

![image](https://user-images.githubusercontent.com/13627951/230102997-d6578945-bac0-4ca4-9210-da80e4f8783c.png)
![image](https://github.com/sanny32/OpenModSim/assets/13627951/43d1b90d-b56b-400d-8dca-2903e23e56a9)

![image](https://user-images.githubusercontent.com/13627951/230103505-f446cf6d-a925-4a51-bbde-8bb77d3da5b1.png)
![image](https://github.com/sanny32/OpenModSim/assets/13627951/e805d429-7657-4355-869b-7559f11ea3d9)



Expand Down Expand Up @@ -42,8 +42,8 @@ Registers
## Scripting
From version 1.2.0 Open ModSim supports scripting. Qt runtime implements the [ECMAScript Language Specification](http://www.ecma-international.org/publications/standards/Ecma-262.htm) standard, so Javascript is used to write code.

![image](https://user-images.githubusercontent.com/13627951/230098131-55b4ef69-e01f-459f-a6d4-11f755978bcb.png)
![image](https://github.com/sanny32/OpenModSim/assets/13627951/c03a7d23-3c69-43aa-a1d3-ae5096da4f54)

Scripts can be launched in two modes: Once or Periodically. If you run script in Once mode the script will stop after it finishes executing. In Periodically mode, the script will start after a certain period of time until the user stops it or the method is called
```javascript
Script.stop();
Expand Down
48 changes: 47 additions & 1 deletion omodsim/controls/numericlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <float.h>
#include <QIntValidator>
#include "qhexvalidator.h"
#include "quintvalidator.h"
#include "numericlineedit.h"

///
Expand Down Expand Up @@ -83,6 +84,11 @@ void NumericLineEdit::setInputMode(InputMode mode)
_maxValue = INT_MAX;
break;

case UnsignedMode:
_minValue = 0U;
_maxValue = UINT_MAX;
break;

case FloatMode:
_minValue = -FLT_MAX;
_maxValue = FLT_MAX;
Expand Down Expand Up @@ -129,9 +135,23 @@ void NumericLineEdit::internalSetValue(QVariant value)
}
break;

case HexMode:
case UnsignedMode:
value = qBound(_minValue.toUInt(), value.toUInt(), _maxValue.toUInt());
if(_paddingZeroes)
{
const auto text = QStringLiteral("%1").arg(value.toUInt(), _paddingZeroWidth, 10, QLatin1Char('0'));
QLineEdit::setText(text);
}
else
{
const auto text = QString::number(value.toUInt());
QLineEdit::setText(text);
}
break;

case HexMode:
value = qBound(_minValue.toInt() > 0 ? _minValue.toUInt() : 0, value.toUInt(), _maxValue.toUInt());
if(_paddingZeroes)
{
const auto text = QStringLiteral("%1").arg(value.toUInt(), _paddingZeroWidth, 16, QLatin1Char('0'));
QLineEdit::setText(text.toUpper());
Expand Down Expand Up @@ -177,6 +197,15 @@ void NumericLineEdit::updateValue()
}
break;

case UnsignedMode:
{
bool ok;
const auto value = text().toUInt(&ok);
if(ok) internalSetValue(value);
else internalSetValue(_value);
}
break;

case HexMode:
{
bool ok;
Expand Down Expand Up @@ -240,6 +269,14 @@ void NumericLineEdit::on_textChanged(const QString& text)
}
break;

case UnsignedMode:
{
bool ok;
const auto valueInt = text.toUInt(&ok);
if(ok) value = qBound(_minValue.toUInt(), valueInt, _maxValue.toUInt());
}
break;

case HexMode:
{
bool ok;
Expand Down Expand Up @@ -293,6 +330,15 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to
}
break;

case UnsignedMode:
{
const int nums = QString::number(top.toUInt()).length();
_paddingZeroWidth = qMax(1, nums);
setMaxLength(qMax(1, nums));
setValidator(new QUIntValidator(bottom.toUInt(), top.toUInt(), this));
}
break;

case HexMode:
{
const int nums = QString::number(top.toUInt(), 16).length();
Expand Down
1 change: 1 addition & 0 deletions omodsim/controls/numericlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class NumericLineEdit : public QLineEdit
enum InputMode
{
DecMode = 0,
UnsignedMode,
HexMode,
FloatMode,
DoubleMode
Expand Down
96 changes: 96 additions & 0 deletions omodsim/controls/outputwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,78 @@ QString formatHexValue(QModbusDataUnit::RegisterType pointType, quint16 value, B
return result.toUpper();
}

///
/// \brief formatLongValue
/// \param pointType
/// \param value1
/// \param value2
/// \param order
/// \param flag
/// \param outValue
/// \return
///
QString formatLongValue(QModbusDataUnit::RegisterType pointType, quint16 value1, quint16 value2, ByteOrder order, bool flag, QVariant& outValue)
{
QString result;
switch(pointType)
{
case QModbusDataUnit::Coils:
case QModbusDataUnit::DiscreteInputs:
outValue = value1;
result = QString("<%1>").arg(value1);
break;
case QModbusDataUnit::HoldingRegisters:
case QModbusDataUnit::InputRegisters:
{
if(flag) break;

const qint32 value = makeLong(value1, value2, order);
outValue = value;
result = result = QString("<%1>").arg(value, 10, 10, QLatin1Char(' '));
}
break;
default:
break;
}
return result;
}

///
/// \brief formatUnsignedLongValue
/// \param pointType
/// \param value1
/// \param value2
/// \param order
/// \param flag
/// \param outValue
/// \return
///
QString formatUnsignedLongValue(QModbusDataUnit::RegisterType pointType, quint16 value1, quint16 value2, ByteOrder order, bool flag, QVariant& outValue)
{
QString result;
switch(pointType)
{
case QModbusDataUnit::Coils:
case QModbusDataUnit::DiscreteInputs:
outValue = value1;
result = QString("<%1>").arg(value1);
break;
case QModbusDataUnit::HoldingRegisters:
case QModbusDataUnit::InputRegisters:
{
if(flag) break;

const quint32 value = makeULong(value1, value2, order);
outValue = value;
result = result = QString("<%1>").arg(value, 10, 10, QLatin1Char('0'));
}
break;
default:
break;
}
return result;
}

///
/// \brief formatFloatValue
/// \param pointType
Expand Down Expand Up @@ -456,6 +528,26 @@ void OutputListModel::updateData(const QModbusDataUnit& data)
itemData.ValueStr = formatDoubleValue(pointType, _lastData.value(i+3), _lastData.value(i+2), _lastData.value(i+1), value,
byteOrder, (i%4) || (i+3>=rowCount()), itemData.Value);
break;

case DataDisplayMode::LongInteger:
itemData.ValueStr = formatLongValue(pointType, value, _lastData.value(i+1), byteOrder,
(i%2) || (i+1>=rowCount()), itemData.Value);
break;

case DataDisplayMode::SwappedLI:
itemData.ValueStr = formatLongValue(pointType, _lastData.value(i+1), value, byteOrder,
(i%2) || (i+1>=rowCount()), itemData.Value);
break;

case DataDisplayMode::UnsignedLongInteger:
itemData.ValueStr = formatUnsignedLongValue(pointType, value, _lastData.value(i+1), byteOrder,
(i%2) || (i+1>=rowCount()), itemData.Value);
break;

case DataDisplayMode::SwappedUnsignedLI:
itemData.ValueStr = formatUnsignedLongValue(pointType, _lastData.value(i+1), value, byteOrder,
(i%2) || (i+1>=rowCount()), itemData.Value);
break;
}
}

Expand Down Expand Up @@ -892,6 +984,10 @@ void OutputWidget::on_listView_doubleClicked(const QModelIndex& index)
{
case DataDisplayMode::FloatingPt:
case DataDisplayMode::SwappedFP:
case DataDisplayMode::LongInteger:
case DataDisplayMode::SwappedLI:
case DataDisplayMode::UnsignedLongInteger:
case DataDisplayMode::SwappedUnsignedLI:
if(index.row() % 2)
idx = _listModel->index(index.row() - 1);
break;
Expand Down
13 changes: 13 additions & 0 deletions omodsim/dialogs/dialogwriteholdingregister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ DialogWriteHoldingRegister::DialogWriteHoldingRegister(ModbusWriteParams& writeP
ui->lineEditValue->setInputMode(NumericLineEdit::DoubleMode);
ui->lineEditValue->setValue(_writeParams.Value.toDouble());
break;

case DataDisplayMode::LongInteger:
case DataDisplayMode::SwappedLI:
ui->lineEditValue->setInputRange(INT_MIN, INT_MAX);
ui->lineEditValue->setValue(_writeParams.Value.toInt());
break;

case DataDisplayMode::UnsignedLongInteger:
case DataDisplayMode::SwappedUnsignedLI:
ui->lineEditValue->setInputRange(0U, UINT_MAX);
ui->lineEditValue->setInputMode(NumericLineEdit::UnsignedMode);
ui->lineEditValue->setValue(_writeParams.Value.toUInt());
break;
}
ui->buttonBox->setFocus();
}
Expand Down
6 changes: 5 additions & 1 deletion omodsim/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ enum class DataDisplayMode
FloatingPt,
SwappedFP,
DblFloat,
SwappedDbl
SwappedDbl,
LongInteger,
SwappedLI,
UnsignedLongInteger,
SwappedUnsignedLI
};
Q_DECLARE_METATYPE(DataDisplayMode);

Expand Down
51 changes: 51 additions & 0 deletions omodsim/floatutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ inline void breakFloat(float value, quint16& lo, quint16& hi, ByteOrder order)
hi = toByteOrderValue(v.asUint16[1], order);
}

///
/// \brief breakLong
/// \param value
/// \param lo
/// \param hi
/// \param order
///
inline void breakLong(qint32 value, quint16& lo, quint16& hi, ByteOrder order)
{
union {
quint16 asUint16[2];
qint32 asInt32;
} v;
v.asInt32 = value;

lo = toByteOrderValue(v.asUint16[0], order);
hi = toByteOrderValue(v.asUint16[1], order);
}

///
/// \brief breakDouble
/// \param value
Expand Down Expand Up @@ -67,6 +86,38 @@ inline float makeFloat(quint16 lo, quint16 hi, ByteOrder order)
return v.asFloat;
}

///
/// \brief makeLong
/// \param lo
/// \param hi
/// \param order
/// \return
///
inline qint32 makeLong(quint16 lo, quint16 hi, ByteOrder order)
{
union {
quint16 asUint16[2];
qint32 asInt32;
} v;

v.asUint16[0] = toByteOrderValue(lo, order);
v.asUint16[1] = toByteOrderValue(hi, order);

return v.asInt32;
}

///
/// \brief makeULong
/// \param lo
/// \param hi
/// \param order
/// \return
///
inline quint32 makeULong(quint16 lo, quint16 hi, ByteOrder order)
{
return (quint32)makeLong(lo, hi, order);
}

///
/// \brief makeDouble
/// \param lolo
Expand Down
2 changes: 1 addition & 1 deletion omodsim/formmodsim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "formmodsim.h"
#include "ui_formmodsim.h"

QVersionNumber FormModSim::VERSION = QVersionNumber(1, 3);
QVersionNumber FormModSim::VERSION = QVersionNumber(1, 4);

///
/// \brief FormModSim::FormModSim
Expand Down
33 changes: 33 additions & 0 deletions omodsim/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ void MainWindow::on_awake()
ui->actionBinary->setChecked(ddm == DataDisplayMode::Binary);
ui->actionUnsignedDecimal->setChecked(ddm == DataDisplayMode::Decimal);
ui->actionInteger->setChecked(ddm == DataDisplayMode::Integer);
ui->actionLongInteger->setChecked(ddm == DataDisplayMode::LongInteger);
ui->actionSwappedLI->setChecked(ddm == DataDisplayMode::SwappedLI);
ui->actionUnsignedLongInteger->setChecked(ddm == DataDisplayMode::UnsignedLongInteger);
ui->actionSwappedUnsignedLI->setChecked(ddm == DataDisplayMode::SwappedUnsignedLI);
ui->actionHex->setChecked(ddm == DataDisplayMode::Hex);
ui->actionFloatingPt->setChecked(ddm == DataDisplayMode::FloatingPt);
ui->actionSwappedFP->setChecked(ddm == DataDisplayMode::SwappedFP);
Expand Down Expand Up @@ -530,6 +534,35 @@ void MainWindow::on_actionInteger_triggered()
updateDataDisplayMode(DataDisplayMode::Integer);
}

///
/// \brief MainWindow::on_actionLongInteger_triggered
///
void MainWindow::on_actionLongInteger_triggered()
{
updateDataDisplayMode(DataDisplayMode::LongInteger);
}

///
/// \brief MainWindow::on_actionSwappedLI_triggered
///
void MainWindow::on_actionSwappedLI_triggered()
{
updateDataDisplayMode(DataDisplayMode::SwappedLI);
}

///
/// \brief MainWindow::on_actionUnsignedLongInteger_triggered
///
void MainWindow::on_actionUnsignedLongInteger_triggered()
{
updateDataDisplayMode(DataDisplayMode::UnsignedLongInteger);
}

void MainWindow::on_actionSwappedUnsignedLI_triggered()
{
updateDataDisplayMode(DataDisplayMode::SwappedUnsignedLI);
}

///
/// \brief MainWindow::on_actionHex_triggered
///
Expand Down
Loading

0 comments on commit 3d1aee0

Please sign in to comment.