summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2018-05-08 13:46:39 -0700
committerJani Heikkinen <jani.heikkinen@qt.io>2018-05-09 10:18:09 +0000
commitc66a364cf34b4945208f07362deabe0020e5b596 (patch)
treef78b4f76df488a53e98ba43e7e3249e369756915
parentde70bbb517839b552648d7590d240c3d2f7e646a (diff)
Fix compilation with MSVC 2017v5.11.0-rc2v5.11.0
std::bitset<8> fails to compile with MSVC 2017. I don't know why nor do I care. Just use a regular byte for one byte, QBitArray for multiple. bitset(271): error C2666: 'operator /': 10 overloads have similar conversions [...] bitset(271): note: or 'built-in C++ operator/(size_t, )' bitset(271): note: while trying to match the argument list '(size_t, )' bitset(267): note: while compiling class template member function 'std::bitset<8> &std::bitset<8>::set(size_t,bool)' bitset(40): note: see reference to function template instantiation 'std::bitset<8> &std::bitset<8>::set(size_t,bool)' being compiled Change-Id: I5d0ee9389a794d80983efffd152cc64b1e99d236 Reviewed-by: Karsten Heimrich <karsten.heimrich@qt.io>
-rw-r--r--src/serialbus/qmodbusclient.cpp13
-rw-r--r--src/serialbus/qmodbusserver.cpp35
2 files changed, 22 insertions, 26 deletions
diff --git a/src/serialbus/qmodbusclient.cpp b/src/serialbus/qmodbusclient.cpp
index 182a212..077b2ec 100644
--- a/src/serialbus/qmodbusclient.cpp
+++ b/src/serialbus/qmodbusclient.cpp
@@ -41,8 +41,6 @@
#include <QtCore/qdebug.h>
#include <QtCore/qloggingcategory.h>
-#include <bitset>
-
QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(QT_MODBUS)
@@ -304,10 +302,11 @@ QModbusRequest QModbusClientPrivate::createWriteRequest(const QModbusDataUnit &d
quint8 address = 0;
QVector<quint8> bytes;
for (quint8 i = 0; i < byteCount; ++i) {
- std::bitset<8> byte;
+ quint8 byte = 0;
for (int currentBit = 0; currentBit < 8; ++currentBit)
- byte[currentBit] = data.value(address++);
- bytes.append(static_cast<quint8> (byte.to_ulong()));
+ if (data.value(address++))
+ byte |= (1U << currentBit);
+ bytes.append(byte);
}
return QModbusRequest(QModbusRequest::WriteMultipleCoils, quint16(data.startAddress()),
@@ -454,9 +453,9 @@ bool QModbusClientPrivate::collateBits(const QModbusPdu &response,
if (data) {
uint value = 0;
for (qint32 i = 1; i < payload.size(); ++i) {
- const std::bitset<8> byte = payload[i];
+ const quint8 byte = quint8(payload[i]);
for (qint32 currentBit = 0; currentBit < 8 && value < data->valueCount(); ++currentBit)
- data->setValue(value++, byte[currentBit]);
+ data->setValue(value++, byte & (1U << currentBit) ? 1 : 0);
}
data->setRegisterType(type);
}
diff --git a/src/serialbus/qmodbusserver.cpp b/src/serialbus/qmodbusserver.cpp
index 4e16e1b..5c0f8dd 100644
--- a/src/serialbus/qmodbusserver.cpp
+++ b/src/serialbus/qmodbusserver.cpp
@@ -39,12 +39,12 @@
#include "qmodbusserver_p.h"
#include "qmodbus_symbols_p.h"
+#include <QtCore/qbitarray.h>
#include <QtCore/qdebug.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qvector.h>
#include <algorithm>
-#include <bitset>
QT_BEGIN_NAMESPACE
@@ -731,18 +731,16 @@ QModbusResponse QModbusServerPrivate::readBits(const QModbusPdu &request,
unit.setValueCount(byteCount * 8);
}
+ // Using byteCount * 8 so the remaining bits in the last byte are zero
+ QBitArray bytes(byteCount * 8);
+
address = 0; // The data range now starts with zero.
- QVector<quint8> bytes;
- for (int i = 0; i < byteCount; ++i) {
- std::bitset<8> byte;
- // According to the spec: If the returned quantity is not a multiple of eight,
- // the remaining bits in the final data byte will be padded with zeros.
- for (int currentBit = 0; currentBit < 8; ++currentBit)
- byte[currentBit] = unit.value(address++); // The padding happens inside value().
- bytes.append(static_cast<quint8> (byte.to_ulong()));
- }
+ for ( ; address < count; ++address)
+ bytes.setBit(address, unit.value(address));
- return QModbusResponse(request.functionCode(), byteCount, bytes);
+ QByteArray payload = QByteArray::fromRawData(bytes.bits(), byteCount);
+ payload.prepend(char(byteCount));
+ return QModbusResponse(request.functionCode(), payload);
}
QModbusResponse QModbusServerPrivate::processReadHoldingRegistersRequest(const QModbusRequest &rqst)
@@ -831,13 +829,12 @@ QModbusResponse QModbusServerPrivate::processReadExceptionStatusRequest(const QM
}
quint16 address = 0;
- QVector<quint8> bytes;
- std::bitset<8> byte;
+ quint8 byte = 0;
for (int currentBit = 0; currentBit < 8; ++currentBit)
- byte[currentBit] = coils.value(address++); // The padding happens inside value().
- bytes.append(static_cast<quint8> (byte.to_ulong()));
+ if (coils.value(address++)) // The padding happens inside value().
+ byte |= (1U << currentBit);
- return QModbusResponse(request.functionCode(), bytes);
+ return QModbusResponse(request.functionCode(), byte);
}
QModbusResponse QModbusServerPrivate::processDiagnosticsRequest(const QModbusRequest &request)
@@ -985,7 +982,7 @@ QModbusResponse QModbusServerPrivate::processWriteMultipleCoilsRequest(const QMo
QModbusExceptionResponse::IllegalDataAddress);
}
- QVector<std::bitset<8>> bytes;
+ QVector<quint8> bytes;
const QByteArray payload = request.data().mid(5);
for (qint32 i = payload.size() - 1; i >= 0; --i)
bytes.append(quint8(payload[i]));
@@ -994,9 +991,9 @@ QModbusResponse QModbusServerPrivate::processWriteMultipleCoilsRequest(const QMo
// range is numberOfCoils and therefore index too.
quint16 coil = numberOfCoils;
qint32 currentBit = 8 - ((byteCount * 8) - numberOfCoils);
- for (const auto &currentByte : qAsConst(bytes)) {
+ for (quint8 currentByte : qAsConst(bytes)) {
for (currentBit -= 1; currentBit >= 0; --currentBit)
- coils.setValue(--coil, currentByte[currentBit]);
+ coils.setValue(--coil, currentByte & (1U << currentBit) ? 1 : 0);
currentBit = 8;
}