Skip to content

Commit a1da01b

Browse files
committed
Correct working with Ansi data display mode
1 parent f165186 commit a1da01b

9 files changed

Lines changed: 193 additions & 105 deletions

File tree

omodsim/controls/numericlineedit.cpp

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include <QKeyEvent>
21
#include <float.h>
2+
#include <QKeyEvent>
33
#include <QIntValidator>
4+
#include "ansiutils.h"
45
#include "qhexvalidator.h"
56
#include "quintvalidator.h"
67
#include "qint64validator.h"
@@ -79,11 +80,12 @@ void NumericLineEdit::setInputMode(InputMode mode)
7980
switch(mode)
8081
{
8182
case HexMode:
83+
case AnsiMode:
8284
_minValue = (ushort)0;
8385
_maxValue = USHRT_MAX;
8486
break;
8587

86-
case Int32Mode:
88+
case Int32Mode:
8789
_minValue = INT_MIN;
8890
_maxValue = INT_MAX;
8991
break;
@@ -118,6 +120,23 @@ void NumericLineEdit::setInputMode(InputMode mode)
118120
emit rangeChanged(_minValue, _maxValue);
119121
}
120122

123+
///
124+
/// \brief NumericLineEdit::codepage
125+
///
126+
QString NumericLineEdit::codepage() const
127+
{
128+
return _codepage;
129+
}
130+
131+
///
132+
/// \brief NumericLineEdit::setCodepage
133+
/// \param name
134+
///
135+
void NumericLineEdit::setCodepage(const QString& name)
136+
{
137+
_codepage = name;
138+
}
139+
121140
///
122141
/// \brief NumericLineEdit::setText
123142
/// \param text
@@ -150,7 +169,7 @@ void NumericLineEdit::internalSetValue(QVariant value)
150169
if(text != QLineEdit::text())
151170
QLineEdit::setText(text);
152171
}
153-
break;
172+
break;
154173

155174
case UInt32Mode:
156175
value = qBound(_minValue.toUInt(), value.toUInt(), _maxValue.toUInt());
@@ -166,7 +185,7 @@ void NumericLineEdit::internalSetValue(QVariant value)
166185
if(text != QLineEdit::text())
167186
QLineEdit::setText(text);
168187
}
169-
break;
188+
break;
170189

171190
case HexMode:
172191
{
@@ -187,6 +206,15 @@ void NumericLineEdit::internalSetValue(QVariant value)
187206
}
188207
break;
189208

209+
case AnsiMode:
210+
{
211+
value = qBound(_minValue.toInt() > 0 ? _minValue.toUInt() : 0, value.toUInt(), _maxValue.toUInt());
212+
const auto text = printableAnsi(uint16ToAnsi(value.value<quint16>()), _codepage);
213+
if(text != QLineEdit::text())
214+
QLineEdit::setText(text);
215+
}
216+
break;
217+
190218
case FloatMode:
191219
{
192220
value = qBound(_minValue.toFloat(), value.toFloat(), _maxValue.toFloat());
@@ -283,6 +311,15 @@ void NumericLineEdit::updateValue()
283311
}
284312
break;
285313

314+
case AnsiMode:
315+
{
316+
auto codec = QTextCodec::codecForName(_codepage.toUtf8());
317+
if(codec == nullptr) codec = QTextCodec::codecForLocale();
318+
const auto value = uint16FromAnsi(codec->fromUnicode(text()));
319+
internalSetValue(value);
320+
}
321+
break;
322+
286323
case FloatMode:
287324
{
288325
bool ok;
@@ -390,6 +427,13 @@ void NumericLineEdit::on_textChanged(const QString& text)
390427
}
391428
break;
392429

430+
case AnsiMode:
431+
{
432+
const uint valueUInt = uint16FromAnsi(text.toLocal8Bit());
433+
value = qBound(_minValue.toUInt(), valueUInt, _maxValue.toUInt());
434+
}
435+
break;
436+
393437
case FloatMode:
394438
{
395439
bool ok;
@@ -469,11 +513,15 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to
469513
}
470514
break;
471515

516+
case AnsiMode:
517+
setMaxLength(2);
518+
break;
519+
472520
case FloatMode:
473521
case DoubleMode:
474522
setMaxLength(INT16_MAX);
475523
setValidator(new QDoubleValidator(bottom.toDouble(), top.toDouble(), 6, this));
476-
break;
524+
break;
477525

478526
case Int64Mode:
479527
{

omodsim/controls/numericlineedit.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class NumericLineEdit : public QLineEdit
1919
FloatMode,
2020
DoubleMode,
2121
Int64Mode,
22-
UInt64Mode
22+
UInt64Mode,
23+
AnsiMode
2324
};
2425

2526
explicit NumericLineEdit(QWidget* parent = nullptr);
@@ -54,6 +55,9 @@ class NumericLineEdit : public QLineEdit
5455
InputMode inputMode() const;
5556
void setInputMode(InputMode mode);
5657

58+
QString codepage() const;
59+
void setCodepage(const QString& name);
60+
5761
void setText(const QString& text);
5862

5963
signals:
@@ -81,6 +85,7 @@ private slots:
8185
InputMode _inputMode;
8286
bool _paddingZeroes;
8387
int _paddingZeroWidth;
88+
QString _codepage;
8489
};
8590

8691
#endif // NUMERICLINEEDIT_H

omodsim/dialogs/dialogwriteholdingregister.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ DialogWriteHoldingRegister::DialogWriteHoldingRegister(ModbusWriteParams& writeP
6464
ui->lineEditValue->setValue(_writeParams.Value.toUInt());
6565
break;
6666

67+
case DataDisplayMode::Ansi:
68+
ui->labelValue->setText(tr("Value, (ANSI): "));
69+
ui->lineEditValue->setInputMode(NumericLineEdit::AnsiMode);
70+
ui->lineEditValue->setCodepage(_writeParams.Codepage);
71+
ui->lineEditValue->setValue(_writeParams.Value.toUInt());
72+
break;
73+
6774
case DataDisplayMode::FloatingPt:
6875
case DataDisplayMode::SwappedFP:
6976
ui->lineEditValue->setInputMode(NumericLineEdit::FloatMode);

omodsim/formmodsim.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "formmodsim.h"
1313
#include "ui_formmodsim.h"
1414

15-
QVersionNumber FormModSim::VERSION = QVersionNumber(1, 6);
15+
QVersionNumber FormModSim::VERSION = QVersionNumber(1, 7);
1616

1717
///
1818
/// \brief FormModSim::FormModSim
@@ -747,7 +747,7 @@ void FormModSim::on_outputWidget_itemDoubleClicked(quint16 addr, const QVariant&
747747
case QModbusDataUnit::Coils:
748748
case QModbusDataUnit::DiscreteInputs:
749749
{
750-
ModbusWriteParams params = { addr, value, mode, byteOrder(), zeroBasedAddress };
750+
ModbusWriteParams params = { addr, value, mode, byteOrder(), codepage(), zeroBasedAddress };
751751
DialogWriteCoilRegister dlg(params, simParams, this);
752752
switch(dlg.exec())
753753
{
@@ -766,7 +766,7 @@ void FormModSim::on_outputWidget_itemDoubleClicked(quint16 addr, const QVariant&
766766
case QModbusDataUnit::InputRegisters:
767767
case QModbusDataUnit::HoldingRegisters:
768768
{
769-
ModbusWriteParams params = { addr, value, mode, byteOrder(), zeroBasedAddress };
769+
ModbusWriteParams params = { addr, value, mode, byteOrder(), codepage(), zeroBasedAddress };
770770
if(mode == DataDisplayMode::Binary)
771771
{
772772
DialogWriteHoldingRegisterBits dlg(params, this);
@@ -890,7 +890,7 @@ void FormModSim::on_dataSimulated(DataDisplayMode mode, QModbusDataUnit::Registe
890890
const auto pointAddr = dd.PointAddress - (dd.ZeroBasedAddress ? 0 : 1);
891891
if(type == dd.PointType && addr >= pointAddr && addr <= pointAddr + dd.Length)
892892
{
893-
_mbMultiServer.writeRegister(type, { addr, value, mode, byteOrder(), true });
893+
_mbMultiServer.writeRegister(type, { addr, value, mode, byteOrder(), codepage(), true });
894894
}
895895
}
896896

omodsim/formmodsim.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ inline QSettings& operator <<(QSettings& out, FormModSim* frm)
187187
out << frm->byteOrder();
188188
out << frm->displayDefinition();
189189
out.setValue("DisplayHexAddresses", frm->displayHexAddresses());
190+
out.setValue("Codepage", frm->codepage());
190191
out << frm->scriptSettings();
191192
out << frm->scriptControl();
192193

@@ -240,6 +241,7 @@ inline QSettings& operator >>(QSettings& in, FormModSim* frm)
240241
frm->setByteOrder(byteOrder);
241242
frm->setDisplayDefinition(displayDefinition);
242243
frm->setDisplayHexAddresses(in.value("DisplayHexAddresses").toBool());
244+
frm->setCodepage(in.value("Codepage").toString());
243245
frm->setScriptSettings(scriptSettings);
244246

245247
return in;
@@ -285,6 +287,7 @@ inline QDataStream& operator <<(QDataStream& out, FormModSim* frm)
285287
out << frm->scriptControl();
286288
out << frm->scriptSettings();
287289
out << frm->descriptionMap();
290+
out << frm->codepage();
288291

289292
const auto unit = frm->serializeModbusDataUnit(dd.PointType, dd.PointAddress, dd.Length);
290293
out << unit.registerType();
@@ -367,6 +370,12 @@ inline QDataStream& operator >>(QDataStream& in, FormModSim* frm)
367370
in >> descriptionMap;
368371
}
369372

373+
QString codepage;
374+
if(ver >= QVersionNumber(1, 7))
375+
{
376+
in >> codepage;
377+
}
378+
370379
if(in.status() != QDataStream::Ok)
371380
return in;
372381

@@ -385,6 +394,7 @@ inline QDataStream& operator >>(QDataStream& in, FormModSim* frm)
385394
frm->setFont(font);
386395
frm->setDisplayDefinition(dd);
387396
frm->setByteOrder(byteOrder);
397+
frm->setCodepage(codepage);
388398
frm->setScriptSettings(scriptSettings);
389399

390400
for(auto&& k : simulationMap.keys())

omodsim/mainwindow.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ void MainWindow::on_awake()
196196
ui->actionUInt16->setEnabled(frm != nullptr);
197197
ui->actionInt16->setEnabled(frm != nullptr);
198198
ui->actionHex->setEnabled(frm != nullptr);
199+
ui->actionAnsi->setEnabled(frm != nullptr);
199200
ui->actionFloatingPt->setEnabled(frm != nullptr);
200201
ui->actionSwappedFP->setEnabled(frm != nullptr);
201202
ui->actionDblFloat->setEnabled(frm != nullptr);
@@ -255,6 +256,7 @@ void MainWindow::on_awake()
255256
ui->actionUInt64->setChecked(ddm == DataDisplayMode::UInt64);
256257
ui->actionSwappedUInt64->setChecked(ddm == DataDisplayMode::SwappedUInt64);
257258
ui->actionHex->setChecked(ddm == DataDisplayMode::Hex);
259+
ui->actionAnsi->setChecked(ddm == DataDisplayMode::Ansi);
258260
ui->actionFloatingPt->setChecked(ddm == DataDisplayMode::FloatingPt);
259261
ui->actionSwappedFP->setChecked(ddm == DataDisplayMode::SwappedFP);
260262
ui->actionDblFloat->setChecked(ddm == DataDisplayMode::DblFloat);
@@ -1115,6 +1117,7 @@ void MainWindow::presetRegs(QModbusDataUnit::RegisterType type)
11151117
params.Address = presetParams.PointAddress;
11161118
params.DisplayMode = frm->dataDisplayMode();
11171119
params.Order = frm->byteOrder();
1120+
params.Codepage = frm->codepage();
11181121
params.ZeroBasedAddress = dd.ZeroBasedAddress;
11191122

11201123
if(dd.PointType == type)

omodsim/modbusmultiserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ void ModbusMultiServer::writeRegister(QModbusDataUnit::RegisterType pointType, c
674674
case DataDisplayMode::UInt16:
675675
case DataDisplayMode::Int16:
676676
case DataDisplayMode::Hex:
677+
case DataDisplayMode::Ansi:
677678
data = createDataUnit(pointType, addr, params.Value.toUInt(), params.Order);
678679
break;
679680
case DataDisplayMode::FloatingPt:

omodsim/modbuswriteparams.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct ModbusWriteParams
1313
QVariant Value;
1414
DataDisplayMode DisplayMode;
1515
ByteOrder Order;
16+
QString Codepage;
1617
bool ZeroBasedAddress;
1718
};
1819
Q_DECLARE_METATYPE(ModbusWriteParams)

0 commit comments

Comments
 (0)