// SPDX-License-Identifier: LGPL-3.0-or-later
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (C) 2013 - 2024, nymea GmbH
* Copyright (C) 2024 - 2025, chargebyte austria GmbH
*
* This file is part of libnymea-modbus.
*
* libnymea-modbus is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* libnymea-modbus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libnymea-modbus. If not, see .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef MODBUSDATAUTILS_H
#define MODBUSDATAUTILS_H
#include
#include
#include
#include
#include
class ModbusDataUtils
{
Q_GADGET
public:
enum Access {
AccessReadOnly,
AccessWriteOnly,
AccessReadWrite
};
Q_ENUM(Access)
enum ByteOrder {
ByteOrderLittleEndian,
ByteOrderBigEndian
};
Q_ENUM(ByteOrder)
enum DataType {
UInt8,
UInt16,
Uint32,
Uint64,
Int8,
Int16,
Int32,
Int64,
Float,
Float64,
String,
Bool
};
Q_ENUM(DataType)
typedef struct ModbusRegister {
quint16 address;
quint16 size;
DataType dataType;
Access access;
QString description;
QString unit;
QVector rawData;
} ModbusRegister;
typedef QVector ModbusRegisters;
explicit ModbusDataUtils();
// Convert to
static quint16 convertToUInt16(const QVector ®isters);
static qint16 convertToInt16(const QVector ®isters);
static quint32 convertToUInt32(const QVector ®isters, ByteOrder byteOrder = ByteOrderLittleEndian);
static qint32 convertToInt32(const QVector ®isters, ByteOrder byteOrder = ByteOrderLittleEndian);
static quint64 convertToUInt64(const QVector ®isters, ByteOrder byteOrder = ByteOrderLittleEndian);
static qint64 convertToInt64(const QVector ®isters, ByteOrder byteOrder = ByteOrderLittleEndian);
static QString convertToString(const QVector ®isters, ByteOrder characterByteOrder = ByteOrderLittleEndian);
static QByteArray convertToByteArray(const QVector ®isters);
static float convertToFloat32(const QVector ®isters, ByteOrder byteOrder = ByteOrderLittleEndian);
static double convertToFloat64(const QVector ®isters, ByteOrder byteOrder = ByteOrderLittleEndian);
// Convert from
static QVector convertFromUInt16(quint16 value);
static QVector convertFromInt16(qint16 value);
static QVector convertFromUInt32(quint32 value, ByteOrder byteOrder = ByteOrderLittleEndian);
static QVector convertFromInt32(qint32 value, ByteOrder byteOrder = ByteOrderLittleEndian);
static QVector convertFromUInt64(quint64 value, ByteOrder byteOrder = ByteOrderLittleEndian);
static QVector convertFromInt64(qint64 value, ByteOrder byteOrder = ByteOrderLittleEndian);
static QVector convertFromString(const QString &value, quint16 stringLength, ByteOrder characterByteOrder = ByteOrderLittleEndian);
static QVector convertFromFloat32(float value, ByteOrder byteOrder = ByteOrderLittleEndian);
static QVector convertFromFloat64(double value, ByteOrder byteOrder = ByteOrderLittleEndian);
static QString exceptionCodeToString(QModbusPdu::ExceptionCode exception);
static QString registerTypeToString(QModbusDataUnit::RegisterType registerType);
};
#endif // MODBUSDATAUTILS_H