|
|
|
#ifndef MODBUSMASTER_H
|
|
|
|
#define MODBUSMASTER_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QModbusDataUnit>
|
|
|
|
#include <QModbusRtuSerialMaster>
|
|
|
|
#include <QtSerialBus/qtserialbusglobal.h>
|
|
|
|
#include <QBitArray>
|
|
|
|
#include <QSerialPort>
|
|
|
|
#include "SettingSerialParameters.h"
|
|
|
|
|
|
|
|
|
|
|
|
class QModbusClient;
|
|
|
|
class QModbusReply;
|
|
|
|
|
|
|
|
|
|
|
|
class modBusMaster : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit modBusMaster(QObject *parent = nullptr);
|
|
|
|
void init();
|
|
|
|
void open(QString serialPort, int slaveAddress);
|
|
|
|
void start();
|
|
|
|
void deinit();
|
|
|
|
void close();
|
|
|
|
void stop();
|
|
|
|
void setSingleCoil(int startAddress, bool coilFlag);
|
|
|
|
void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags);
|
|
|
|
void setSingleRegister(int startAddress, quint16 registerValue);
|
|
|
|
void setMultipleRegister(int startAddress, quint16 writeSize, QVector<quint16> registerValues);
|
|
|
|
void getCoil(int startAddress, quint16 readSize);
|
|
|
|
void getInputCoil(int startAddress, quint16 readSize);
|
|
|
|
void getInputRegister(int startAddress, quint16 readSize);
|
|
|
|
void getHoldingRegister(int startAddress, quint16 readSize);
|
|
|
|
auto readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
|
|
|
|
void writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
|
|
|
|
bool checkForError(QModbusReply *_reply);
|
|
|
|
QBitArray retriveGetCoilValues(QModbusReply *_reply);
|
|
|
|
QVector<quint16> retriveGetRegisterValues(QModbusReply *_reply);
|
|
|
|
signals:
|
|
|
|
void dataFromClient(QString readedParameter);
|
|
|
|
void modbusConnectionState(bool state);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
|
|
|
private:
|
|
|
|
QModbusReply *lastRequest;
|
|
|
|
QModbusClient *modbusDevice = nullptr;
|
|
|
|
QString _serialPort ;
|
|
|
|
int _slaveAddress;
|
|
|
|
SettingSerialParameters _modbusSettings;
|
|
|
|
public:
|
|
|
|
QModbusReply *reply;
|
|
|
|
QBitArray m_coils;
|
|
|
|
QVector<quint16> m_holdingRegisters;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MODBUSMASTER_H
|