|
|
|
#ifndef MODBUSMASTER_H
|
|
|
|
#define MODBUSMASTER_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QModbusDataUnit>
|
|
|
|
#include <QModbusRtuSerialMaster>
|
|
|
|
#include <QtSerialBus/qtserialbusglobal.h>
|
|
|
|
#include <QBitArray>
|
|
|
|
#include <QSerialPort>
|
|
|
|
#include "SettingSerialParameters.h"
|
|
|
|
#include "QWaitCondition"
|
|
|
|
#include "qmutex.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);
|
|
|
|
|
|
|
|
QBitArray getCoil(int startAddress, quint16 readSize);
|
|
|
|
QBitArray getInputCoil(int startAddress, quint16 readSize);
|
|
|
|
QVector<quint16> getInputRegister(int startAddress, quint16 readSize);
|
|
|
|
QVector<quint16> getHoldingRegister(int startAddress, quint16 readSize);
|
|
|
|
|
|
|
|
QModbusDataUnit readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
|
|
|
|
void writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
|
|
|
|
|
|
|
|
bool checkForError(QModbusReply *_reply);
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void dataFromClient(QString readedParameter);
|
|
|
|
void modbusConnectionState(bool state);
|
|
|
|
|
|
|
|
private:
|
|
|
|
QModbusReply *lastRequest;
|
|
|
|
QModbusClient *modbusDevice = nullptr;
|
|
|
|
QString _serialPort ;
|
|
|
|
int _slaveAddress;
|
|
|
|
SettingSerialParameters _modbusSettings;
|
|
|
|
QBitArray _coilFlags;
|
|
|
|
QVector<quint16> _registerValues;
|
|
|
|
QWaitCondition modBusAnswerNotReady;
|
|
|
|
QMutex mutex;
|
|
|
|
bool freeThread=false;
|
|
|
|
public:
|
|
|
|
QModbusReply *reply;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MODBUSMASTER_H
|