-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
148 lines (115 loc) · 4.54 KB
/
mainwindow.cpp
File metadata and controls
148 lines (115 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QModbusTcpServer>
#include <QUrl>
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), _pUi(new Ui::MainWindow)
{
_pUi->setupUi(this);
_pSlaveData = new TestSlaveData();
_pSlaveModbus = new TestSlaveModbus(_pSlaveData);
_pRegisterDataModel = new RegisterDataModel(_pSlaveData);
_pIncGraph = new IncGraph(_pSlaveData);
_pSineGraph = new SineGraph(_pSlaveData);
connect(_pSlaveData, &TestSlaveData::dataChanged, _pRegisterDataModel, &RegisterDataModel::handleDataChange);
connect(_pUi->btnListen, &QPushButton::clicked, this, &MainWindow::onConnectClicked);
connect(_pUi->btnDisconnect, &QPushButton::clicked, this, &MainWindow::onDisconnectClicked);
connect(_pSlaveModbus, &QModbusServer::stateChanged, this, &MainWindow::onStateChanged);
connect(_pSlaveModbus, &QModbusServer::errorOccurred, this, &MainWindow::handleDeviceError);
/** Set exception group **/
_exceptionGroup.addButton(_pUi->chkNone, 0);
_exceptionGroup.addButton(_pUi->chkIllegalFunction, QModbusPdu::IllegalFunction);
_exceptionGroup.addButton(_pUi->chkIllegalDataAddress, QModbusPdu::IllegalDataAddress);
_exceptionGroup.addButton(_pUi->chkIllegalDataValue, QModbusPdu::IllegalDataValue);
_exceptionGroup.addButton(_pUi->chkTargetNoResponse, QModbusPdu::GatewayTargetDeviceFailedToRespond);
_exceptionGroup.addButton(_pUi->chkGatewayPathUnavailable, QModbusPdu::GatewayPathUnavailable);
connect(&_exceptionGroup, QOverload<int, bool>::of(&QButtonGroup::idToggled), this,
[=, this](int id, bool checked) {
if (checked)
{
_pSlaveModbus->setException(static_cast<QModbusPdu::ExceptionCode>(id));
}
});
/** Handle error recurrence group **/
_bErrorOnce = true;
_errorRecurrenceGroup.addButton(_pUi->optErrorOnce, true);
_errorRecurrenceGroup.addButton(_pUi->optErrorPersistent, false);
connect(&_errorRecurrenceGroup, QOverload<int>::of(&QButtonGroup::idClicked), this,
[=, this](int id) { _bErrorOnce = static_cast<bool>(id); });
connect(_pSlaveModbus, &TestSlaveModbus::requestProcessed, this, &MainWindow::handleRequestProcessed);
/** Auto increment **/
_bAutoInc = false;
connect(_pUi->checkAutoIncrement, &QCheckBox::checkStateChanged, this, [this](Qt::CheckState state) {
_bAutoInc = state;
_pIncGraph->setState(_bAutoInc);
});
/** Setup registerView **/
_pUi->tblRegData->setModel(_pRegisterDataModel);
_pUi->tblRegData->verticalHeader()->show();
_pUi->tblRegData->horizontalHeader()->show();
_pUi->tblRegData->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
QList<uint> registerList = QList<uint>() << 0 << 1 << 2 << 3 << 4;
_pSlaveData->setRegisterState(registerList, true);
_pIncGraph->setRegisters(0, 10);
_pSlaveData->setRegisterState(10, true);
_pSineGraph->setRegister(10);
_pSineGraph->setPeriod(10000);
QString windowCaption;
windowCaption = QString("ModbusSim");
setWindowTitle(windowCaption);
}
MainWindow::~MainWindow()
{
if (_pSlaveModbus)
{
_pSlaveModbus->disconnectDevice();
}
delete _pSlaveData;
delete _pSlaveModbus;
delete _pUi;
}
void MainWindow::onConnectClicked(void)
{
QUrl hostUrl;
hostUrl.setPort(_pUi->spinSlavePort->value());
hostUrl.setHost("127.0.0.1");
if (_pSlaveModbus->connect(hostUrl, _pUi->spinSlaveId->value()))
{
_pUi->btnListen->setEnabled(false);
_pUi->btnDisconnect->setEnabled(true);
}
else
{
qDebug() << "Connect failed: " << _pSlaveModbus->errorString();
}
}
void MainWindow::onDisconnectClicked(void)
{
_pSlaveModbus->disconnectDevice();
_pUi->btnListen->setEnabled(true);
_pUi->btnDisconnect->setEnabled(false);
}
void MainWindow::handleDeviceError(QModbusDevice::Error newError)
{
if (newError == QModbusDevice::NoError || !_pSlaveModbus)
{
return;
}
qDebug() << "Error: " << _pSlaveModbus->errorString();
}
void MainWindow::onStateChanged(QModbusDevice::State state)
{
qDebug() << "State: " << state;
bool connected = (state != QModbusDevice::UnconnectedState);
_pUi->btnListen->setEnabled(!connected);
_pUi->spinSlaveId->setEnabled(!connected);
_pUi->spinSlavePort->setEnabled(!connected);
_pUi->btnDisconnect->setEnabled(connected);
}
void MainWindow::handleRequestProcessed()
{
if (_bErrorOnce)
{
_pUi->chkNone->click();
}
}