-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
169 lines (144 loc) · 4.88 KB
/
client.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
#include <QThread>
#include <QModbusRtuSerialMaster>
#include <QModbusTcpClient>
#include <QDebug>
#include <QMetaEnum>
#include "client.h"
namespace Modbus_Cli {
Client::Client(const QString &conn_string, int timeout, int number_of_retries, bool quiet) :
_quiet(quiet),
_config(conn_string, timeout, number_of_retries)
{
if (_config._tcp._address.isEmpty())
_dev = std::make_shared<QModbusRtuSerialMaster>();
else
_dev = std::make_shared<QModbusTcpClient>();
connect(_dev.get(), &QModbusClient::errorOccurred, this, &Client::error_occurred);
connect(_dev.get(), &QModbusClient::stateChanged, this, &Client::state_changed);
connect(&_timer, &QTimer::timeout, this, &Client::timeout);
_timer.setSingleShot(true);
_timer.setInterval(10000);
}
bool Client::connect_device()
{
if (_dev->state() != QModbusClient::UnconnectedState)
{
if (_dev->state() == QModbusClient::ConnectedState)
emit connected();
return true;
}
if (!_quiet)
{
auto dbg = qInfo().noquote() << "Connecting to modbus device:";
if (_config._tcp._address.isEmpty())
{
dbg << _config._rtu._name
<< "Speed:" << _config._rtu._baud_rate
<< QMetaEnum::fromType<QSerialPort::DataBits>().valueToKey(_config._rtu._data_bits)
<< QMetaEnum::fromType<QSerialPort::Parity>().valueToKey(_config._rtu._parity)
<< QMetaEnum::fromType<QSerialPort::StopBits>().valueToKey(_config._rtu._stop_bits)
<< QMetaEnum::fromType<QSerialPort::FlowControl>().valueToKey(_config._rtu._flow_control);
}
else
dbg.nospace() << _config._tcp._address << ':' << _config._tcp._port;
}
Das::Modbus::Config::set(_config, _dev.get());
_timer.start();
return _dev->connectDevice();
}
void Client::read(int address, QModbusDataUnit::RegisterType type, int start_address, int count)
{
QModbusDataUnit data(type, start_address, count);
QModbusReply* reply = _dev->sendReadRequest(data, address);
process_reply(reply);
}
void Client::write(int address, QModbusPdu::FunctionCode func, const QByteArray &data)
{
QModbusRequest request{func, data};
QModbusReply* reply = _dev->sendRawRequest(request, address);
process_reply(reply);
}
void Client::write(int address, QModbusDataUnit::RegisterType type, int start_address, const QVector<quint16> &values)
{
if (!_quiet)
qDebug() << "Write:" << values;
QModbusDataUnit data(type, start_address, values);
QModbusReply* reply = _dev->sendWriteRequest(data, address);
process_reply(reply);
}
void Client::readwrite(int address, QModbusDataUnit::RegisterType type, int start_address, int count, const QVector<quint16> &values)
{
if (!_quiet)
qDebug() << "Write:" << values;
QModbusDataUnit read_data(type, start_address, count);
QModbusDataUnit write_data(type, start_address, values);
QModbusReply* reply = _dev->sendReadWriteRequest(read_data, write_data, address);
process_reply(reply);
}
void Client::timeout()
{
if (!_quiet)
qCritical() << "Connection timeout";
emit finished();
}
void Client::error_occurred(QModbusDevice::Error e)
{
if (!_quiet)
qCritical().noquote() << "Occurred:" << e << _dev->errorString();
if (e == QModbusDevice::ConnectionError)
{
_dev->disconnectDevice();
emit finished();
}
}
void Client::state_changed(QModbusDevice::State state)
{
if (state == QModbusDevice::ConnectedState)
{
_timer.stop();
emit connected();
}
}
void Client::reply_finished_slot()
{
reply_finished(qobject_cast<QModbusReply*>(sender()));
}
void Client::process_reply(QModbusReply *reply)
{
if (reply)
{
if (reply->isFinished())
reply_finished(reply);
else
connect(reply, &QModbusReply::finished, this, &Client::reply_finished_slot);
}
else if (!_quiet)
qCritical().noquote() << tr("Reply error: ") + _dev->errorString();
}
void Client::reply_finished(QModbusReply *reply)
{
if (reply->error() != QModbusDevice::NoError)
{
if (!_quiet)
qCritical().noquote() << "Reply error:" << reply->error() << reply->errorString()
<< (reply->error() == QModbusDevice::ProtocolError ?
tr("Mobus exception: 0x%1").arg(reply->rawResult().exceptionCode(), -1, 16) :
tr("code: 0x%1").arg(reply->error(), -1, 16));
}
else
{
const QModbusDataUnit unit = reply->result();
if (unit.valueCount() == 1 && _quiet)
qInfo() << unit.value(0);
else
{
for (uint i = 0; i < unit.valueCount(); ++i)
qInfo() << (unit.startAddress() + i) << "=" << unit.value(i);
QModbusResponse response = reply->rawResult();
qDebug() << "Raw response:" << response.data().toHex().toUpper();
}
}
reply->deleteLater();
emit finished();
}
} // namespace Modbus_Cli