2024-10-31 16:48:38 +08:00
|
|
|
|
#ifndef MODBUSRTU_H
|
|
|
|
|
#define MODBUSRTU_H
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
#include "modbus.h"
|
2024-11-01 11:24:09 +08:00
|
|
|
|
#include <QThread>
|
2024-10-31 16:48:38 +08:00
|
|
|
|
|
|
|
|
|
class QTimer;
|
|
|
|
|
|
2024-11-01 11:24:09 +08:00
|
|
|
|
class ModbusRTU : public QThread
|
2024-10-31 16:48:38 +08:00
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
ModbusRTU(QObject *parent = nullptr);
|
|
|
|
|
~ModbusRTU();
|
2024-11-04 18:07:54 +08:00
|
|
|
|
|
|
|
|
|
public slots:
|
2024-10-31 16:48:38 +08:00
|
|
|
|
void write_regs_slot(int addr, const uint16_t data);
|
|
|
|
|
void read_regs_slot(int addr, int nb, uint16_t *data, int row);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
modbus_t *m_ModbusRTUDevice;
|
|
|
|
|
bool isConnect;
|
|
|
|
|
const char *comPort;
|
|
|
|
|
int baudRate;
|
|
|
|
|
char parity;
|
|
|
|
|
int dataBit;
|
|
|
|
|
int stopBit;
|
|
|
|
|
|
2024-11-04 18:07:54 +08:00
|
|
|
|
public:
|
2024-11-01 11:24:09 +08:00
|
|
|
|
void run() override;
|
|
|
|
|
|
2024-10-31 16:48:38 +08:00
|
|
|
|
private:
|
|
|
|
|
|
2024-11-01 11:24:09 +08:00
|
|
|
|
static constexpr int MODBUS_ERR = -1;
|
2024-10-31 16:48:38 +08:00
|
|
|
|
|
|
|
|
|
enum DataType{
|
|
|
|
|
TypeRecvData,
|
|
|
|
|
TypeSendData
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
void init();
|
|
|
|
|
bool modbusRTUConnect(const char *comPort, int baudRate, char parity, int dataBit, int stopBit);
|
|
|
|
|
void modbusRTUDisconnect();
|
|
|
|
|
|
|
|
|
|
//0x01:读线圈状态: 00 01 00 00 00 06 01 01 67 89 00 05
|
|
|
|
|
int read_coil_status(int addr, int nb, uint8_t *dest);
|
|
|
|
|
//0x02:读离散量输入: 00 01 00 00 00 06 01 02 67 89 00 05
|
|
|
|
|
int read_input_status(int addr, int nb, uint8_t *dest);
|
|
|
|
|
//0x03:读保持寄存器: 00 01 00 00 00 06 01 03 67 89 00 05
|
|
|
|
|
int read_holding_register(int addr, int nb, uint16_t *dest);
|
|
|
|
|
//0x04:读输入寄存器: 00 01 00 00 00 06 01 04 67 89 00 05
|
|
|
|
|
int read_input_register(int addr, int nb, uint16_t *dest);
|
|
|
|
|
//0x05:写单个线圈: 00 01 00 00 00 06 01 05 67 89 FF 00
|
|
|
|
|
int write_single_coil(int coil_addr, int status);
|
|
|
|
|
//0x06:写单个保持寄存器: 00 01 00 00 00 06 01 06 67 89 12 34
|
|
|
|
|
int write_single_register(int reg_addr, const uint16_t value);
|
|
|
|
|
//0x0F(15):写多个线圈: 00 01 00 00 00 08 01 0F 67 89 00 05 01 1F
|
|
|
|
|
int write_multiple_coil(int addr, int nb, const uint8_t *data);
|
|
|
|
|
//0x10(16):写多个保持寄存器: 00 01 00 00 00 11 01 10 67 89 00 05 0A 00 00 00 00 00 00 00 00 00 05
|
|
|
|
|
int write_multiple_registers(int addr, int nb, const uint16_t *data);
|
|
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
void sig_ModbusRTUConnected();
|
|
|
|
|
void sig_showModbusData(QByteArray byteArray, int dataType);
|
|
|
|
|
void sig_showModbusData_sgl_tempt(QByteArray byteArray, int dataType);
|
|
|
|
|
void sig_showModbusData_base_info1_45(QByteArray byteArray,int dataType);
|
|
|
|
|
void sig_showModbusData_base_info57_78(QByteArray byteArray,int dataType);
|
|
|
|
|
void sig_showModbusData_base_info83_106(QByteArray byteArray,int dataType);
|
|
|
|
|
void sig_showModbusData_base_info47_49_51_53_55(QByteArray byteArray,int dataType);
|
|
|
|
|
void sig_showModbusData_base_info_list_47_49_51_53_55(QList<int> byteArray,int dataType);
|
|
|
|
|
void sig_showReadHoldingRegs(QByteArray byteArray,int dataType, int row, int nb);
|
|
|
|
|
|
2024-11-04 18:07:54 +08:00
|
|
|
|
|
2024-10-31 16:48:38 +08:00
|
|
|
|
public slots:
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // MODBUSRTU_H
|