You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

111 lines
2.8 KiB

#ifndef MYMODBUSTCP_H
#define MYMODBUSTCP_H
#include <QModbusTcpClient>
#include <QModbusReply>
#include <QDebug>
#include <QTimer>
#include <QDateTime>
#include <QApplication>
class MyModbusTcp : public QObject
{
public:
MyModbusTcp();
//声明modbusDevice
QModbusClient *modbusDevice;
//modbustcp接收的原始数据,以word为单位
unsigned int wordRCV[40];
//用于存放读取的数据的数组,float类型
float dataRCV[30];
// QTimer *timer = nullptr;
/**
* @brief timeGoBy 等待t毫秒
* @param time
* @param t
*/
void timeGoBy(QTime time, int t);
/**
* @brief connectModbusTcp 连接modbustcp
*/
void connectModbusTcp();
/**
* @brief disconnectModbusTcp 断开连接
*/
void disconnectModbusTcp();
/**
* @brief writeSingleHR 将单个float写入保持性寄存器
* @param address 写入的保持性寄存器地址
* @param dataSend 写入的数据
* @param serverID id
*/
void writeSingleFloat(int address, float dataSend, int serverID);
/**
* @brief writeSingleWord 写入单个word(int)
* @param i 地址
* @param w 写入的数据
*/
void writeSingleWord(int i, int w);
/**
* @brief writeMultipleHR 写多个保持性寄存器HR
* @param address 起始地址
* @param num 数量
* @param dataSend 写入的数组
* @param serverID id
*/
void writeMultipleHR(int address, int num, float *dataSend, int serverID);
/**
* @brief readMultipleHR 读多个保持性寄存器,并保存到dataRCV数组中
* @param startAddress 起始地址
* @param num 数量
* @param serverID id
*/
void readMultipleHR(int startAddress, int num, int serverID);
// /**
// * @brief onTimeReadHR 定时读HR
// */
// void onTimeReadHR();
// /**
// * @brief stopOnTimeReadHR 停止定时获取数据
// */
// void stopOnTimeReadHR();
private:
/**
* @brief float2Word 将浮点数f转化为2个8位数据存放在word数组中
* @param f 待转换的浮点数
* @param word 转换后的word数组
*/
void float2Word(float f, unsigned int *word);
/**
* @brief word2Float 将2个word数据转化为浮点数
* @param p 指向word数组的指针
* @param dataTransformed 转化后的浮点数
*/
void word2Float(unsigned int *p, float dataTransformed);
/**
* @brief errorOccurred_Slot 错误发生
*/
void errorOccurred_Slot();
/**
* @brief stateChanged_SLot 状态改变
*/
void stateChanged_SLot();
/**
* @brief onReadReadyHR_Slot HR读取准备就绪
*/
void onReadReadyHR_Slot();
};
#endif // MYMODBUSTCP_H