Skip to content

Commit e40eb95

Browse files
committed
Refactor more modbus register
1 parent b8d0894 commit e40eb95

8 files changed

Lines changed: 71 additions & 259 deletions

File tree

src/communication/datapoint.cpp

Lines changed: 5 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
#include <bit>
44

5-
DataPoint::DataPoint() : DataPoint(QString(), Device::cFirstDeviceId, ModbusDataType::Type::UNSIGNED_16)
5+
DataPoint::DataPoint() : DataPoint(QString(), Device::cFirstDeviceId)
66
{
77
}
88

9-
DataPoint::DataPoint(QString const& address, deviceId_t deviceId, ModbusDataType::Type type)
10-
: _address(address), _deviceId(deviceId), _type(type)
9+
DataPoint::DataPoint(QString const& address, deviceId_t deviceId) : _address(address), _deviceId(deviceId)
1110
{
1211
}
1312

@@ -16,74 +15,16 @@ QString DataPoint::address() const
1615
return _address;
1716
}
1817

19-
void DataPoint::setAddress(const QString& address)
20-
{
21-
_address = address;
22-
}
23-
2418
deviceId_t DataPoint::deviceId() const
2519
{
2620
return _deviceId;
2721
}
2822

29-
void DataPoint::setDeviceId(deviceId_t deviceId)
30-
{
31-
_deviceId = deviceId;
32-
}
33-
34-
void DataPoint::setType(ModbusDataType::Type type)
35-
{
36-
_type = type;
37-
}
38-
39-
ModbusDataType::Type DataPoint::type() const
40-
{
41-
return _type;
42-
}
43-
4423
QString DataPoint::description() const
4524
{
4625
QString connStr = QString("device id %1").arg(deviceId());
4726

48-
return QString("%1, %2, %3").arg(_address, ModbusDataType::description(_type), connStr);
49-
}
50-
51-
double DataPoint::processValue(uint16_t lowerRegister, uint16_t upperRegister, bool int32LittleEndian) const
52-
{
53-
double processedResult = 0u;
54-
55-
if (ModbusDataType::isFloat(_type))
56-
{
57-
uint32_t combinedValue = convertEndianness(int32LittleEndian, lowerRegister, upperRegister);
58-
59-
processedResult = convertUint32ToFloat(combinedValue);
60-
}
61-
else if (ModbusDataType::is32Bit(_type))
62-
{
63-
uint32_t combinedValue = convertEndianness(int32LittleEndian, lowerRegister, upperRegister);
64-
65-
if (ModbusDataType::isUnsigned(_type))
66-
{
67-
processedResult = static_cast<double>(static_cast<quint32>(combinedValue));
68-
}
69-
else
70-
{
71-
processedResult = static_cast<double>(static_cast<qint32>(combinedValue));
72-
}
73-
}
74-
else
75-
{
76-
if (ModbusDataType::isUnsigned(_type))
77-
{
78-
processedResult = static_cast<double>(static_cast<quint16>(lowerRegister));
79-
}
80-
else
81-
{
82-
processedResult = static_cast<double>(static_cast<qint16>(lowerRegister));
83-
}
84-
}
85-
86-
return processedResult;
27+
return QString("%1, %2").arg(_address, connStr);
8728
}
8829

8930
DataPoint& DataPoint::operator=(const DataPoint& dataPoint)
@@ -96,15 +37,14 @@ DataPoint& DataPoint::operator=(const DataPoint& dataPoint)
9637

9738
_address = dataPoint.address();
9839
_deviceId = dataPoint.deviceId();
99-
_type = dataPoint.type();
10040

10141
// return the existing object so we can chain this operator
10242
return *this;
10343
}
10444

10545
bool operator==(const DataPoint& dp1, const DataPoint& dp2)
10646
{
107-
if ((dp1._address == dp2._address) && (dp1._deviceId == dp2._deviceId) && (dp1._type == dp2._type))
47+
if ((dp1._address == dp2._address) && (dp1._deviceId == dp2._deviceId))
10848
{
10949
return true;
11050
}
@@ -131,36 +71,4 @@ QString DataPoint::dumpListToString(QList<DataPoint> list)
13171
dStream << list;
13272

13373
return str;
134-
}
135-
136-
uint32_t DataPoint::convertEndianness(bool bLittleEndian, quint16 value, quint16 nextValue) const
137-
{
138-
uint32_t combinedValue;
139-
if (bLittleEndian)
140-
{
141-
combinedValue = (static_cast<uint32_t>(nextValue) << 16) | value;
142-
}
143-
else
144-
{
145-
combinedValue = (static_cast<uint32_t>(value) << 16) | nextValue;
146-
}
147-
148-
return combinedValue;
149-
}
150-
151-
double DataPoint::convertUint32ToFloat(quint32 value) const
152-
{
153-
const double doubleValue = std::bit_cast<float>(value);
154-
155-
switch (std::fpclassify(doubleValue))
156-
{
157-
case FP_INFINITE:
158-
case FP_NAN:
159-
case FP_ZERO:
160-
return 0.0f;
161-
case FP_NORMAL:
162-
case FP_SUBNORMAL:
163-
default:
164-
return doubleValue;
165-
}
166-
}
74+
}

src/communication/datapoint.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define DATAPOINT_H
33

44
#include "models/device.h"
5-
#include "util/modbusdatatype.h"
65
#include <QDebug>
76
#include <QObject>
87
#include <QString>
@@ -11,22 +10,15 @@ class DataPoint
1110
{
1211
public:
1312
DataPoint();
14-
DataPoint(QString const& address, deviceId_t deviceId, ModbusDataType::Type type);
13+
DataPoint(QString const& address, deviceId_t deviceId);
1514

1615
QString address() const;
17-
void setAddress(const QString& address);
1816

1917
deviceId_t deviceId() const;
20-
void setDeviceId(deviceId_t deviceId);
21-
22-
void setType(ModbusDataType::Type type);
23-
ModbusDataType::Type type() const;
2418

2519
QString description() const;
2620

27-
double processValue(uint16_t lowerRegister, uint16_t upperRegister, bool int32LittleEndian) const;
28-
29-
DataPoint(const DataPoint& copy) : _address{ copy.address() }, _deviceId{ copy.deviceId() }, _type{ copy.type() }
21+
DataPoint(const DataPoint& copy) : _address{ copy.address() }, _deviceId{ copy.deviceId() }
3022
{
3123
}
3224

@@ -37,12 +29,8 @@ class DataPoint
3729
static QString dumpListToString(QList<DataPoint> list);
3830

3931
private:
40-
uint32_t convertEndianness(bool bLittleEndian, quint16 value, quint16 nextValue) const;
41-
double convertUint32ToFloat(quint32 value) const;
42-
4332
QString _address;
4433
deviceId_t _deviceId;
45-
ModbusDataType::Type _type;
4634
};
4735

4836
QDebug operator<<(QDebug debug, const DataPoint& dp);

src/communication/modbuspoll.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ QStringList ModbusPoll::buildRegisterExpressions(const QList<DataPoint>& registe
124124
QStringList expressions;
125125
for (const DataPoint& reg : registerList)
126126
{
127-
QString expr =
128-
QString("${%1 @ %2 : %3}").arg(reg.address()).arg(reg.deviceId()).arg(ModbusDataType::typeString(reg.type()));
129-
expressions.append(expr);
127+
expressions.append(reg.address());
130128
}
131129
return expressions;
132130
}
Lines changed: 20 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "expressionparser.h"
22

3+
#include "models/device.h"
34
#include "util/expressionregex.h"
4-
#include "util/modbusdatatype.h"
55
#include "util/scopelogging.h"
66

77
const QString ExpressionParser::_cRegisterFunctionTemplate = "r(%1%2)";
@@ -11,9 +11,6 @@ ExpressionParser::ExpressionParser(QStringList& expressions)
1111
_findRegRegex.setPattern(ExpressionRegex::cMatchRegister);
1212
_findRegRegex.optimize();
1313

14-
_regParseRegex.setPattern(ExpressionRegex::cParseReg);
15-
_regParseRegex.optimize();
16-
1714
parseExpressions(expressions);
1815
}
1916

@@ -70,33 +67,31 @@ QString ExpressionParser::processExpression(QString const& graphExpr)
7067

7168
bool ExpressionParser::processRegisterExpression(QString regExpr, DataPoint& dataPoint)
7269
{
73-
bool bRet = false;
74-
75-
QRegularExpressionMatch match = _regParseRegex.match(regExpr);
76-
77-
QString strAddress;
78-
QString strDeviceId;
79-
QString strType;
70+
static const QRegularExpression regParseRegex(ExpressionRegex::cParseReg);
71+
QRegularExpressionMatch match = regParseRegex.match(regExpr);
8072

81-
if (match.hasMatch())
73+
if (!match.hasMatch())
8274
{
83-
strAddress = match.captured(1);
84-
strDeviceId = match.captured(2);
85-
strType = match.captured(3);
86-
87-
bRet = true;
88-
bRet = bRet && parseAddress(strAddress, dataPoint);
89-
bRet = bRet && parseDeviceId(strDeviceId, dataPoint);
90-
bRet = bRet && parseType(strType, dataPoint);
75+
qCWarning(scopeComm) << QString("Part of expression evaluation parsing failed (\"%1\")").arg(regExpr);
76+
return false;
9177
}
92-
else
78+
79+
const QString strDeviceId = match.captured(2);
80+
81+
deviceId_t deviceId = Device::cFirstDeviceId;
82+
if (!strDeviceId.isEmpty())
9383
{
94-
auto msg = QString("Part of expression evaluation parsing failed (\"%1\")").arg(regExpr);
95-
qCWarning(scopeComm) << msg;
96-
bRet = false;
84+
bool ok;
85+
deviceId = strDeviceId.toUInt(&ok);
86+
if (!ok)
87+
{
88+
qCWarning(scopeComm) << QString("Parsing device \"%1\" failed").arg(strDeviceId);
89+
return false;
90+
}
9791
}
9892

99-
return bRet;
93+
dataPoint = DataPoint(regExpr, deviceId);
94+
return true;
10095
}
10196

10297
QString ExpressionParser::constructInternalRegisterFunction(DataPoint const& dataPoint, int size)
@@ -119,67 +114,3 @@ QString ExpressionParser::constructInternalRegisterFunction(DataPoint const& dat
119114

120115
return QString(_cRegisterFunctionTemplate).arg(idx).arg(spaces);
121116
}
122-
123-
bool ExpressionParser::parseAddress(QString strAddr, DataPoint& dataPoint)
124-
{
125-
bool bRet = false;
126-
127-
if (strAddr.isEmpty())
128-
{
129-
/* Required field */
130-
bRet = false;
131-
qCWarning(scopeComm) << QString("No address specified");
132-
}
133-
else
134-
{
135-
bRet = true;
136-
dataPoint.setAddress(strAddr);
137-
}
138-
139-
return bRet;
140-
}
141-
142-
bool ExpressionParser::parseDeviceId(QString strDeviceId, DataPoint& dataPoint)
143-
{
144-
bool bRet = false;
145-
146-
if (strDeviceId.isEmpty())
147-
{
148-
/* Keep default */
149-
bRet = true;
150-
}
151-
else
152-
{
153-
auto deviceId = strDeviceId.toUInt(&bRet);
154-
if (bRet)
155-
{
156-
dataPoint.setDeviceId(deviceId);
157-
}
158-
else
159-
{
160-
qCWarning(scopeComm) << QString("Parsing device \"%1\" failed").arg(strDeviceId);
161-
}
162-
}
163-
164-
return bRet;
165-
}
166-
167-
bool ExpressionParser::parseType(QString strType, DataPoint& dataPoint)
168-
{
169-
bool bRet;
170-
bool bOk;
171-
ModbusDataType::Type type = ModbusDataType::convertString(strType, bOk);
172-
173-
if (bOk)
174-
{
175-
dataPoint.setType(type);
176-
bRet = true;
177-
}
178-
else
179-
{
180-
bRet = false;
181-
qCWarning(scopeComm) << QString("Unknown type \"%1\"").arg(strType);
182-
}
183-
184-
return bRet;
185-
}

src/datahandling/expressionparser.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ class ExpressionParser : public QObject
1717
private:
1818
void parseExpressions(QStringList& expressions);
1919

20-
bool parseAddress(QString strAddr, DataPoint& dataPoint);
21-
bool parseDeviceId(QString strDeviceId, DataPoint& dataPoint);
22-
bool parseType(QString strType, DataPoint& dataPoint);
23-
2420
QString processExpression(QString const& expr);
2521
bool processRegisterExpression(QString regExpr, DataPoint& dataPoint);
2622
QString constructInternalRegisterFunction(DataPoint const& dataPoint, int size);
@@ -29,7 +25,6 @@ class ExpressionParser : public QObject
2925
QList<DataPoint> _dataPoints;
3026

3127
QRegularExpression _findRegRegex;
32-
QRegularExpression _regParseRegex;
3328

3429
static const QString _cRegisterFunctionTemplate;
3530
};

0 commit comments

Comments
 (0)