|
|
|
#ifndef MODBUSMASTER_H
|
|
|
|
#define MODBUSMASTER_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QModbusDataUnit>
|
|
|
|
#include <QModbusRtuSerialMaster>
|
|
|
|
#include <QtSerialBus/qtserialbusglobal.h>
|
|
|
|
#include <QBitArray>
|
|
|
|
#if QT_CONFIG(modbus_serialport)
|
|
|
|
#include <QSerialPort>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class QModbusClient;
|
|
|
|
class QModbusReply;
|
|
|
|
|
|
|
|
|
|
|
|
class modBusMaster : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit modBusMaster(QObject *parent = nullptr);
|
|
|
|
void init();
|
|
|
|
void open(QString _serialPort , int _serverId);
|
|
|
|
void start();
|
|
|
|
void deinit();
|
|
|
|
void close();
|
|
|
|
void stop();
|
|
|
|
void setSingleCoil(int startAddress,bool coilFlag);
|
|
|
|
void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray someCoilFlags);
|
|
|
|
void setSingleRgister(int startAddress, quint16 RegisterValue);
|
|
|
|
void setMultipleRegister(int startAddress, quint16 writeSize, QVector<quint16> someRegisterValue);
|
|
|
|
void getCoil(int startAddress, quint16 readSize);
|
|
|
|
void getInputCoil(int startAddress, quint16 readSize);
|
|
|
|
void getInputRegister(int startAddress, quint16 readSize);
|
|
|
|
void getHoldingRegister(int startAddress, quint16 readSize);
|
|
|
|
void readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
|
|
|
|
void writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
|
|
|
|
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void dataFromClient(QString readedParameter);
|
|
|
|
public slots:
|
|
|
|
void readReady();
|
|
|
|
private:
|
|
|
|
QModbusReply *lastRequest;
|
|
|
|
QModbusClient *modbusDevice=nullptr;
|
|
|
|
QString serialPort ;
|
|
|
|
int serverId;
|
|
|
|
public:
|
|
|
|
QModbusReply *reply;
|
|
|
|
QBitArray m_coils;
|
|
|
|
QVector<quint16> m_holdingRegisters;
|
|
|
|
struct Settings {
|
|
|
|
#if QT_CONFIG(modbus_serialport)
|
|
|
|
int parity = QSerialPort::EvenParity;
|
|
|
|
int baud = QSerialPort::Baud19200;
|
|
|
|
int dataBits = QSerialPort::Data8;
|
|
|
|
int stopBits = QSerialPort::OneStop;
|
|
|
|
#endif
|
|
|
|
int responseTime = 1000;
|
|
|
|
int numberOfRetries = 3;
|
|
|
|
} modbusSettings;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MODBUSMASTER_H
|