Skip to content

Commit 5265f61

Browse files
authored
Merge pull request #18 from ModbusScope/dev/modbus_register
Refactor modbus register
2 parents 77f791f + 8946ff9 commit 5265f61

16 files changed

Lines changed: 301 additions & 527 deletions

src/communication/datapoint.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "datapoint.h"
2+
3+
DataPoint::DataPoint() : DataPoint(QString(), Device::cFirstDeviceId)
4+
{
5+
}
6+
7+
DataPoint::DataPoint(QString const& address, deviceId_t deviceId) : _address(address), _deviceId(deviceId)
8+
{
9+
}
10+
11+
QString DataPoint::address() const
12+
{
13+
return _address;
14+
}
15+
16+
deviceId_t DataPoint::deviceId() const
17+
{
18+
return _deviceId;
19+
}
20+
21+
QString DataPoint::description() const
22+
{
23+
QString connStr = QString("device id %1").arg(deviceId());
24+
25+
return QString("%1, %2").arg(_address, connStr);
26+
}
27+
28+
DataPoint& DataPoint::operator=(const DataPoint& dataPoint)
29+
{
30+
// self-assignment guard
31+
if (this == &dataPoint)
32+
{
33+
return *this;
34+
}
35+
36+
_address = dataPoint.address();
37+
_deviceId = dataPoint.deviceId();
38+
39+
// return the existing object so we can chain this operator
40+
return *this;
41+
}
42+
43+
bool operator==(const DataPoint& dp1, const DataPoint& dp2)
44+
{
45+
if ((dp1._address == dp2._address) && (dp1._deviceId == dp2._deviceId))
46+
{
47+
return true;
48+
}
49+
else
50+
{
51+
return false;
52+
}
53+
}
54+
55+
QDebug operator<<(QDebug debug, const DataPoint& dp)
56+
{
57+
QDebugStateSaver saver(debug);
58+
59+
debug.nospace().noquote() << '[' << dp.description() << ']';
60+
61+
return debug;
62+
}
63+
64+
QString DataPoint::dumpListToString(QList<DataPoint> const& list)
65+
{
66+
QString str;
67+
QDebug dStream(&str);
68+
69+
dStream << list;
70+
71+
return str;
72+
}

src/communication/datapoint.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef DATAPOINT_H
2+
#define DATAPOINT_H
3+
4+
#include "models/device.h"
5+
#include <QDebug>
6+
#include <QString>
7+
8+
class DataPoint
9+
{
10+
public:
11+
DataPoint();
12+
DataPoint(QString const& address, deviceId_t deviceId);
13+
14+
QString address() const;
15+
16+
deviceId_t deviceId() const;
17+
18+
QString description() const;
19+
20+
DataPoint(const DataPoint& copy) : _address{ copy.address() }, _deviceId{ copy.deviceId() }
21+
{
22+
}
23+
24+
DataPoint& operator=(const DataPoint& dataPoint);
25+
26+
friend bool operator==(const DataPoint& dp1, const DataPoint& dp2);
27+
28+
static QString dumpListToString(const QList<DataPoint>& list);
29+
30+
private:
31+
QString _address;
32+
deviceId_t _deviceId;
33+
};
34+
35+
QDebug operator<<(QDebug debug, const DataPoint& dp);
36+
37+
#endif // DATAPOINT_H

src/communication/modbuspoll.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void ModbusPoll::initAdapter()
4747
_pAdapterClient->prepareAdapter(adapterPath);
4848
}
4949

50-
void ModbusPoll::startCommunication(QList<ModbusRegister>& registerList)
50+
void ModbusPoll::startCommunication(QList<DataPoint>& registerList)
5151
{
5252
_registerList = registerList;
5353
_bPollActive = true;
@@ -119,16 +119,12 @@ void ModbusPoll::onDescribeResult(const QJsonObject& description)
119119
_pSettingsModel->updateAdapterFromDescribe("modbus", description);
120120
}
121121

122-
QStringList ModbusPoll::buildRegisterExpressions(const QList<ModbusRegister>& registerList)
122+
QStringList ModbusPoll::buildRegisterExpressions(const QList<DataPoint>& registerList)
123123
{
124124
QStringList expressions;
125-
for (const ModbusRegister& reg : registerList)
125+
for (const DataPoint& reg : registerList)
126126
{
127-
QString expr = QString("${%1 @ %2 : %3}")
128-
.arg(reg.address().fullAddress())
129-
.arg(reg.deviceId())
130-
.arg(ModbusDataType::typeString(reg.type()));
131-
expressions.append(expr);
127+
expressions.append(reg.address());
132128
}
133129
return expressions;
134130
}

src/communication/modbuspoll.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#ifndef COMMUNICATION_MANAGER_H
2-
#define COMMUNICATION_MANAGER_H
1+
#ifndef MODBUSPOLL_H
2+
#define MODBUSPOLL_H
33

44
#include "ProtocolAdapter/adapterclient.h"
55
#include "ProtocolAdapter/adapterprocess.h"
6-
#include "communication/modbusregister.h"
6+
#include "communication/datapoint.h"
77
#include "util/result.h"
88

99
#include <QJsonObject>
@@ -21,7 +21,7 @@ class ModbusPoll : public QObject
2121
~ModbusPoll();
2222

2323
void initAdapter();
24-
void startCommunication(QList<ModbusRegister>& registerList);
24+
void startCommunication(QList<DataPoint>& registerList);
2525
void stopCommunication();
2626

2727
bool isActive();
@@ -36,9 +36,9 @@ private slots:
3636
void onDescribeResult(const QJsonObject& description);
3737

3838
private:
39-
QStringList buildRegisterExpressions(const QList<ModbusRegister>& registerList);
39+
QStringList buildRegisterExpressions(const QList<DataPoint>& registerList);
4040

41-
QList<ModbusRegister> _registerList;
41+
QList<DataPoint> _registerList;
4242

4343
bool _bPollActive;
4444
QTimer* _pPollTimer;
@@ -49,4 +49,4 @@ private slots:
4949
AdapterClient* _pAdapterClient;
5050
};
5151

52-
#endif // COMMUNICATION_MANAGER_H
52+
#endif // MODBUSPOLL_H

src/communication/modbusregister.cpp

Lines changed: 0 additions & 172 deletions
This file was deleted.

0 commit comments

Comments
 (0)