145 lines
4.4 KiB
C++
145 lines
4.4 KiB
C++
#ifndef ROBOTPROTOCOLSIMPLIFIED_H
|
||
#define ROBOTPROTOCOLSIMPLIFIED_H
|
||
|
||
#include <functional>
|
||
#include "IYModbusTCPServer.h"
|
||
#include "ProtocolCommon.h"
|
||
|
||
/**
|
||
* @brief ModbusTCP简化协议封装类
|
||
* 提供简化的撕裂报警信息和复位功能
|
||
*
|
||
* 寄存器地址分配:
|
||
* - 40001 (地址0): 复位命令 (写入1执行复位)
|
||
* - 40002 (地址1): 撕裂报警标志 (0=无报警,1=撕裂报警)
|
||
* - 40003 (地址2): 最大长度 (mm)
|
||
* - 40004 (地址3): 最大宽度 (mm)
|
||
* - 40005-40006 (地址4-5): 最大撕裂ID (UInt32)
|
||
*/
|
||
class RobotProtocolSimplified
|
||
{
|
||
public:
|
||
/**
|
||
* @brief 撕裂报警数据结构
|
||
*/
|
||
struct TearingAlarmData
|
||
{
|
||
uint16_t alarmFlag; // 报警标志 (0=无报警,1=撕裂报警)
|
||
uint16_t maxLength; // 最大撕裂长度 (mm)
|
||
uint16_t maxWidth; // 最大撕裂宽度 (mm)
|
||
uint32_t maxId; // 最大撕裂对应的ID
|
||
|
||
TearingAlarmData()
|
||
: alarmFlag(0)
|
||
, maxLength(0)
|
||
, maxWidth(0)
|
||
, maxId(0)
|
||
{}
|
||
};
|
||
|
||
/**
|
||
* @brief 复位回调函数类型
|
||
* 当收到复位命令时触发
|
||
*/
|
||
using ResetCallback = std::function<void()>;
|
||
|
||
/**
|
||
* @brief 连接状态枚举(使用公共状态定义)
|
||
*/
|
||
using ConnectionStatus = ::ConnectionStatus;
|
||
|
||
public:
|
||
RobotProtocolSimplified();
|
||
~RobotProtocolSimplified();
|
||
|
||
/**
|
||
* @brief 初始化ModbusTCP服务
|
||
* @param port TCP端口号,默认502
|
||
* @return 0-成功,其他-错误码
|
||
*/
|
||
int Initialize(uint16_t port = 502);
|
||
|
||
/**
|
||
* @brief 反初始化,停止服务
|
||
*/
|
||
void Deinitialize();
|
||
|
||
/**
|
||
* @brief 设置撕裂报警数据
|
||
* @param alarmData 报警数据结构
|
||
* @return 0-成功,其他-错误码
|
||
*/
|
||
int SetAlarmData(const TearingAlarmData& alarmData);
|
||
|
||
/**
|
||
* @brief 清空报警数据(执行复位)
|
||
* @return 0-成功,其他-错误码
|
||
*/
|
||
int ClearAlarmData();
|
||
|
||
/**
|
||
* @brief 设置复位回调函数
|
||
* @param callback 复位回调函数
|
||
*/
|
||
void SetResetCallback(const ResetCallback& callback);
|
||
|
||
/**
|
||
* @brief 设置连接状态回调
|
||
* @param callback 回调函数
|
||
*/
|
||
void SetConnectionCallback(const ConnectionCallback& callback);
|
||
|
||
/**
|
||
* @brief 获取服务运行状态
|
||
* @return true-运行中,false-已停止
|
||
*/
|
||
bool IsRunning() const;
|
||
|
||
private:
|
||
/**
|
||
* @brief 停止ModbusTCP服务器
|
||
*/
|
||
void StopModbusTCPServer();
|
||
|
||
/**
|
||
* @brief 处理ModbusTCP连接状态变化
|
||
* @param connected true-连接,false-断开
|
||
*/
|
||
void OnModbusTCPConnectionChanged(bool connected);
|
||
|
||
/**
|
||
* @brief 处理保持寄存器写入请求
|
||
* @param unitId 单元ID
|
||
* @param startAddress 起始地址
|
||
* @param quantity 数量
|
||
* @param values 值数组
|
||
* @return 错误码
|
||
*/
|
||
IYModbusTCPServer::ErrorCode OnWriteRegisters(uint8_t unitId, uint16_t startAddress,
|
||
uint16_t quantity, const uint16_t* values);
|
||
|
||
private:
|
||
// ModbusTCP相关
|
||
IYModbusTCPServer* m_pModbusServer; // ModbusTCP服务器实例
|
||
bool m_bServerRunning; // 服务器运行状态
|
||
uint16_t m_nPort; // TCP端口
|
||
|
||
// 连接状态
|
||
ConnectionStatus m_connectionStatus; // 连接状态
|
||
|
||
// 回调函数
|
||
ConnectionCallback m_connectionCallback; // 连接状态回调
|
||
ResetCallback m_resetCallback; // 复位回调
|
||
|
||
// Modbus寄存器地址映射(实际寄存器地址,从0开始)
|
||
static const uint16_t RESET_CMD_ADDR = 0; // 复位命令地址(40001)
|
||
static const uint16_t ALARM_FLAG_ADDR = 1; // 报警标志地址(40002)
|
||
static const uint16_t MAX_LENGTH_ADDR = 2; // 最大长度地址(40003)
|
||
static const uint16_t MAX_WIDTH_ADDR = 3; // 最大宽度地址(40004)
|
||
static const uint16_t MAX_ID_ADDR = 4; // 最大撕裂ID地址(40005-40006,占2个寄存器)
|
||
|
||
static const uint16_t TOTAL_REGISTERS = 6; // 总寄存器数量
|
||
};
|
||
|
||
#endif // ROBOTPROTOCOLSIMPLIFIED_H
|