BatteryMonitor/modbusrtu.h

81 lines
2.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef MODBUSRTU_H
#define MODBUSRTU_H
#include <QObject>
#include "modbus.h"
#include <QThread>
class QTimer;
class ModbusRTU : public QThread
{
Q_OBJECT
public:
ModbusRTU(QObject *parent = nullptr);
~ModbusRTU();
public slots:
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;
public:
void run() override;
private:
static constexpr int MODBUS_ERR = -1;
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);
public slots:
};
#endif // MODBUSRTU_H