BatteryMonitor/modbusrtu.h

81 lines
2.7 KiB
C
Raw Normal View History

2024-10-31 16:48:38 +08:00
#ifndef MODBUSRTU_H
#define MODBUSRTU_H
#include <QObject>
#include "modbus.h"
#include <QThread>
2024-10-31 16:48:38 +08:00
class QTimer;
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:
void run() override;
2024-10-31 16:48:38 +08:00
private:
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);
//0x0F15写多个线圈: 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);
//0x1016写多个保持寄存器: 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