Skip to content

Commit 0511dd1

Browse files
committed
Merge remote-tracking branch 'origin/graph_simulation'
2 parents 6f65d22 + 7faeb48 commit 0511dd1

File tree

10 files changed

+174
-35
lines changed

10 files changed

+174
-35
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ SET(APP_SRCS
4646
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
4747
${CMAKE_CURRENT_SOURCE_DIR}/src/mainwindow.cpp
4848
${CMAKE_CURRENT_SOURCE_DIR}/src/registerdatamodel.cpp
49+
${CMAKE_CURRENT_SOURCE_DIR}/src/incgraph.cpp
50+
${CMAKE_CURRENT_SOURCE_DIR}/src/sinegraph.cpp
4951

5052
${CMAKE_CURRENT_SOURCE_DIR}/src/mainwindow.ui
5153

src/incgraph.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "incgraph.h"
2+
#include <QtMath>
3+
4+
IncGraph::IncGraph(TestSlaveData *pSlaveData, QObject *parent)
5+
: QObject{parent}
6+
{
7+
_pSlaveData = pSlaveData;
8+
_bIncEnabled = false;
9+
_periodMs = 1000;
10+
11+
connect(&_tickTimer, &QTimer::timeout, this, QOverload<>::of(&IncGraph::timerTick));
12+
_tickTimer.start(_periodMs);
13+
}
14+
15+
void IncGraph::setState(bool bState)
16+
{
17+
_bIncEnabled = bState;
18+
}
19+
20+
void IncGraph::setRegisters(uint32_t start, uint32_t count)
21+
{
22+
_start = start;
23+
_count = count;
24+
}
25+
26+
void IncGraph::timerTick()
27+
{
28+
if (_bIncEnabled)
29+
{
30+
for(uint32_t idx = 0; idx < _count; idx++)
31+
{
32+
quint32 regAddr = _start + idx;
33+
quint16 value = _pSlaveData->registerValue(regAddr) + 1;
34+
_pSlaveData->setRegisterValue(regAddr, value);
35+
}
36+
}
37+
38+
_tickTimer.start(_periodMs);
39+
}

src/incgraph.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef INCGRAPH_H
2+
#define INCGRAPH_H
3+
4+
#include <QObject>
5+
#include <QTimer>
6+
#include "testslavedata.h"
7+
8+
class IncGraph : public QObject
9+
{
10+
Q_OBJECT
11+
public:
12+
explicit IncGraph(TestSlaveData *pSlaveData, QObject *parent = nullptr);
13+
14+
void setRegisters(uint32_t start, uint32_t count);
15+
void setState(bool bState);
16+
17+
private slots:
18+
void timerTick();
19+
20+
private:
21+
22+
bool _bIncEnabled;
23+
QTimer _tickTimer;
24+
quint32 _periodMs;
25+
26+
uint32_t _start;
27+
uint32_t _count;
28+
29+
TestSlaveData *_pSlaveData;
30+
};
31+
32+
#endif // INCGRAPH_H

src/mainwindow.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ MainWindow::MainWindow(QWidget *parent) :
1515
_pSlaveData = new TestSlaveData();
1616
_pSlaveModbus = new TestSlaveModbus(_pSlaveData);
1717

18-
QList<uint> registerList = QList<uint>() << 0 << 1 << 2 << 3 << 4;
19-
_pSlaveData->setRegisterState(registerList, true);
20-
2118
_pRegisterDataModel = new RegisterDataModel(_pSlaveData);
2219

20+
_pIncGraph = new IncGraph(_pSlaveData);
21+
_pSineGraph = new SineGraph(_pSlaveData);
22+
2323
connect(_pSlaveData, &TestSlaveData::dataChanged, _pRegisterDataModel, &RegisterDataModel::handleDataChange);
2424

2525
connect(_pUi->btnListen, &QPushButton::clicked, this, &MainWindow::onConnectClicked);
@@ -57,12 +57,11 @@ MainWindow::MainWindow(QWidget *parent) :
5757
connect(_pSlaveModbus, &TestSlaveModbus::requestProcessed, this, &MainWindow::handleRequestProcessed);
5858

5959
/** Auto increment **/
60-
connect(&_autoIncTimer, &QTimer::timeout, this, &MainWindow::handleAutoIncTick);
61-
_autoIncTimer.start(1000);
6260
_bAutoInc = false;
6361
connect(_pUi->checkAutoIncrement, &QCheckBox::stateChanged, this,
6462
[=](int state){
6563
_bAutoInc = (state == Qt::Checked);
64+
_pIncGraph->setState(_bAutoInc);
6665
});
6766

6867
/** Setup registerView **/
@@ -73,16 +72,23 @@ MainWindow::MainWindow(QWidget *parent) :
7372
/* Don't stretch columns, resize to contents */
7473
_pUi->tblRegData->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
7574

75+
QList<uint> registerList = QList<uint>() << 0 << 1 << 2 << 3 << 4;
76+
_pSlaveData->setRegisterState(registerList, true);
77+
78+
_pIncGraph->setRegisters(0, 10);
79+
80+
_pSlaveData->setRegisterState(10, true);
81+
_pSineGraph->setRegister(10);
82+
_pSineGraph->setPeriod(10000);
83+
7684
QString windowCaption;
77-
windowCaption = QString("ModbusTestSlave");
85+
windowCaption = QString("ModbusTestSim");
7886

7987
setWindowTitle(windowCaption);
8088
}
8189

8290
MainWindow::~MainWindow()
8391
{
84-
_autoIncTimer.stop();
85-
8692
if (_pSlaveModbus)
8793
{
8894
_pSlaveModbus->disconnectDevice();
@@ -139,14 +145,6 @@ void MainWindow::onStateChanged(QModbusDevice::State state)
139145
_pUi->btnDisconnect->setEnabled(connected);
140146
}
141147

142-
void MainWindow::handleAutoIncTick()
143-
{
144-
if (_bAutoInc)
145-
{
146-
_pSlaveData->incrementAllEnabledRegisters();
147-
}
148-
}
149-
150148
void MainWindow::handleRequestProcessed()
151149
{
152150
if (_bErrorOnce)

src/mainwindow.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
#define MAINWINDOW_H
33

44
#include <QMainWindow>
5-
#include <QTimer>
65
#include <QButtonGroup>
76

87
#include "testslavemodbus.h"
98
#include "testslavedata.h"
109

1110
#include "registerdatamodel.h"
11+
#include "incgraph.h"
12+
#include "sinegraph.h"
1213

1314
namespace Ui {
1415
class MainWindow;
@@ -30,18 +31,19 @@ private slots:
3031
void handleDeviceError(QModbusDevice::Error newError);
3132
void onStateChanged(QModbusDevice::State state);
3233

33-
void handleAutoIncTick();
3434
void handleRequestProcessed();
3535

3636
private:
3737

3838
Ui::MainWindow *_pUi;
3939

40-
QTimer _autoIncTimer;
4140
bool _bAutoInc;
4241

4342
bool _bErrorOnce;
4443

44+
IncGraph* _pIncGraph;
45+
SineGraph* _pSineGraph;
46+
4547
QButtonGroup _exceptionGroup;
4648
QButtonGroup _errorRecurrenceGroup;
4749

src/mainwindow.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
<item row="0" column="0">
222222
<widget class="QCheckBox" name="checkAutoIncrement">
223223
<property name="text">
224-
<string>Auto increment data (1/sec)</string>
224+
<string>Auto increment data (1/sec) (first 10 registers)</string>
225225
</property>
226226
</widget>
227227
</item>

src/sinegraph.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "sinegraph.h"
2+
#include <QtMath>
3+
4+
SineGraph::SineGraph(TestSlaveData *pSlaveData, QObject *parent)
5+
: QObject{parent}
6+
{
7+
_currentTick = 0;
8+
_tickTime = 1;
9+
_periodMs = 1000;
10+
_pSlaveData = pSlaveData;
11+
12+
_address = 10;
13+
14+
connect(&_tickTimer, &QTimer::timeout, this, QOverload<>::of(&SineGraph::timerTick));
15+
_tickTimer.start(_tickTime);
16+
}
17+
18+
void SineGraph::timerTick()
19+
{
20+
_currentTick++;
21+
_currentTick = _currentTick >= _periodMs ? 0: _currentTick;
22+
23+
const double angle = (2 * M_PI) * _currentTick / _periodMs;
24+
double value = sin(angle)
25+
+ sin(3 * angle) / 3
26+
+ sin(5 * angle) / 5
27+
+ sin(7 * angle) / 7
28+
+ sin(9 * angle) / 9
29+
+ sin(11 * angle) / 11
30+
+ sin(13 * angle) / 13;
31+
32+
_pSlaveData->setRegisterValue(_address, value * 1000);
33+
34+
_tickTimer.start(_tickTime);
35+
}
36+
37+
void SineGraph::setPeriod(uint32_t period)
38+
{
39+
_currentTick = 0;
40+
_periodMs = period;
41+
}
42+
43+
void SineGraph::setRegister(uint32_t address)
44+
{
45+
_address = address;
46+
}

src/sinegraph.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef SINEGRAPH_H
2+
#define SINEGRAPH_H
3+
4+
#include <QObject>
5+
#include <QTimer>
6+
#include "testslavedata.h"
7+
8+
class SineGraph : public QObject
9+
{
10+
Q_OBJECT
11+
public:
12+
explicit SineGraph(TestSlaveData *pSlaveData, QObject *parent = nullptr);
13+
14+
void setPeriod(uint32_t period);
15+
void setRegister(uint32_t address);
16+
17+
signals:
18+
void valueUpdate();
19+
20+
private slots:
21+
void timerTick();
22+
23+
private:
24+
25+
QTimer _tickTimer;
26+
quint32 _tickTime;
27+
quint32 _currentTick;
28+
quint32 _periodMs;
29+
30+
uint32_t _address;
31+
TestSlaveData *_pSlaveData;
32+
33+
};
34+
35+
#endif // SINEGRAPH_H

testslave/testslavedata.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,3 @@ quint16 TestSlaveData::registerValue(uint registerAddress)
8585

8686
return 0;
8787
}
88-
89-
void TestSlaveData::incrementAllEnabledRegisters()
90-
{
91-
for(uint idx = 0u; idx < _registerList.size(); idx++)
92-
{
93-
if (_registerList[idx].bState)
94-
{
95-
_registerList[idx].value++;
96-
}
97-
}
98-
99-
emit dataChanged();
100-
}

testslave/testslavedata.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class TestSlaveData : public QObject
2222
bool registerState(uint registerAddress);
2323
quint16 registerValue(uint registerAddress);
2424

25-
void incrementAllEnabledRegisters();
26-
2725
signals:
2826
void dataChanged();
2927

0 commit comments

Comments
 (0)