-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.cpp
262 lines (217 loc) · 6.8 KB
/
backend.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "backend.h"
#include <QModbusTcpClient>
#include <QModbusRtuSerialMaster>
#include <QUrl>
#include <QThread>
BackEnd::BackEnd(QObject *parent) :
QObject(parent)
, lastRequest(nullptr)
, modbusDevice(nullptr)
{
modbusDevice = new QModbusTcpClient(this);
connect(modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error) {
qDebug() << modbusDevice->errorString();
});
if (!modbusDevice) {
qDebug() << "Could not create Modbus client.";
} else {
connect(modbusDevice, &QModbusClient::stateChanged,
this, &BackEnd::onStateChanged);
}
m_currentSpeed = 0;
m_currentAirPressure = 0;
m_currentAirPressureDG0 = 0;
m_currentAirPressureDG1 = 0;
m_connected = false;
}
BackEnd::~BackEnd()
{
if (modbusDevice)
modbusDevice->disconnectDevice();
delete modbusDevice;
}
void BackEnd::setSpeed(const float speed)
{
if (speed == m_speed)
return;
qDebug() << "New Speed: " << speed;
sendData(1,(quint16) speed);
m_speed = speed;
}
void BackEnd::setBreakLevel(const float breakLevel)
{
if (breakLevel == m_breakLevel)
return;
qDebug() << "New breakLevel: " << breakLevel;
sendData(0,(quint16) (breakLevel + 1000)); // Offset + 1000
m_breakLevel = breakLevel;
}
void BackEnd::setDirection(const bool dir)
{
if (dir == m_direction)
return;
qDebug() << "New Direction: " << dir;
sendData(1,dir);
m_direction = dir;
}
void BackEnd::setHorn(const bool on)
{
qDebug() << "setHorn: " << on;
sendData(6,on);
}
void BackEnd::setCompressor(const bool on)
{
qDebug() << "setCompressor: " << on;
sendData(8,on);
}
void BackEnd::setCurrentSpeed(const float speed)
{
m_currentSpeed = speed;
}
float BackEnd::currentSpeed()
{
return m_currentSpeed;
}
void BackEnd::setCurrentAirPressure(const float pressure)
{
m_currentAirPressure = pressure;
}
float BackEnd::currentAirPressure()
{
return m_currentAirPressure;
}
void BackEnd::setCurrentAirPressureDG0(const float pressure)
{
m_currentAirPressureDG0 = pressure;
}
float BackEnd::currentAirPressureDG0()
{
return m_currentAirPressureDG0;
}
void BackEnd::setCurrentAirPressureDG1(const float pressure)
{
m_currentAirPressureDG1 = pressure;
}
float BackEnd::currentAirPressureDG1()
{
return m_currentAirPressureDG1;
}
bool BackEnd::connected()
{
return this->m_connected;
}
void BackEnd::on_connectButton_clicked()
{
qDebug() << "Click";
if (!modbusDevice)
return;
if (modbusDevice->state() != QModbusDevice::ConnectedState) {
#ifdef LOCAL
const QUrl url = QUrl::fromUserInput("127.0.0.1:5020");
#else
const QUrl url = QUrl::fromUserInput("192.168.1.17:5020");
#endif
modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter, url.port());
modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter, url.host());
qDebug() << "Try to connect to " << url.host() << ":" << url.port();
modbusDevice->setTimeout(1000);
modbusDevice->setNumberOfRetries(5);
if (!modbusDevice->connectDevice()) {
qDebug() << "Connect failed: " << modbusDevice->errorString();
this->m_connected = false;
}
} else {
//modbusDevice->disconnectDevice();
}
}
void BackEnd::on_update()
{
if (!modbusDevice)
return;
QModbusDataUnit readUnit = QModbusDataUnit(QModbusDataUnit::InputRegisters, 0, 10);
if (auto *reply = modbusDevice->sendReadRequest(readUnit, 1)) {
if (!reply->isFinished())
connect(reply, &QModbusReply::finished, this, &BackEnd::readReady);
else
delete reply; // broadcast replies return immediately
} else {
qDebug() << "Read error: " << modbusDevice->errorString();
}
}
void BackEnd::readReady()
{
auto reply = qobject_cast<QModbusReply *>(sender());
if (!reply)
return;
if (reply->error() == QModbusDevice::NoError) {
const QModbusDataUnit unit = reply->result();
for (uint i = 0; i < unit.valueCount(); i++) {
const QString entry = tr("Address: %1, Value: %2").arg(unit.startAddress() + i)
.arg(QString::number(unit.value(i),
unit.registerType() <= QModbusDataUnit::Coils ? 10 : 16));
//qDebug() << entry;
}
this->setCurrentSpeed(unit.value(3));
this->setCurrentAirPressure(unit.value(4));
this->setCurrentAirPressureDG0(unit.value(5));
this->setCurrentAirPressureDG1(unit.value(6));
emit BackEnd::currentValuesChanged();
} else if (reply->error() == QModbusDevice::ProtocolError) {
qDebug() << "Read response error: "
<< reply->errorString()
<< " (Mobus exception: "
<< reply->rawResult().exceptionCode()
<< ")";
} else {
qDebug() << "Read response error: "
<< reply->errorString()
<< " (code: "
<< reply->error()
<<")";
}
reply->deleteLater();
QThread::msleep(100);
emit on_update();
}
void BackEnd::onStateChanged(int state)
{
this->m_connected = (state == QModbusDevice::ConnectedState);
if (state == QModbusDevice::UnconnectedState)
qDebug() << "Disonnect";
else if (state == QModbusDevice::ConnectedState)
{
emit on_update();
qDebug() << "Connect";
}
emit connectionChanged();
}
void BackEnd::sendData(int address, bool data)
{
QModbusDataUnit writeUnit = QModbusDataUnit(QModbusDataUnit::Coils, address, 1);
writeUnit.setValue(0, data);
sendData(writeUnit);
}
void BackEnd::sendData(int address, quint16 data)
{
QModbusDataUnit writeUnit = QModbusDataUnit(QModbusDataUnit::HoldingRegisters, address, 1);
writeUnit.setValue(0, data);
sendData(writeUnit);
}
void BackEnd::sendData(QModbusDataUnit writeUnit)
{
if (auto *reply = modbusDevice->sendWriteRequest(writeUnit, 1)) {
if (!reply->isFinished()) {
connect(reply, &QModbusReply::finished, this, [this, reply]() {
if (reply->error() == QModbusDevice::ProtocolError) {
} else if (reply->error() != QModbusDevice::NoError) {
}
reply->deleteLater();
});
} else {
// broadcast replies return immediately
reply->deleteLater();
}
} else {
qDebug() << "Write error: " << modbusDevice->errorString();
}
}