/** * @file modbus.h * @author zhangcheng * @brief modbus数据结构文件 * @version v0.1 * @date 2025-07-25 * * @copyright Copyright (c) 2025 */ /** * @file modbus.h * @author zhangcheng * @brief modbus相关数据结构文件 * @version v0.1 * @date 2025-07-25 * * @copyright Copyright (c) 2025 */ #ifndef _MODBUS_H #define _MODBUS_H /** * @brief 线圈寄存器地址映射,数量,占用字节总量 * @details * @note * @attention */ #define COIL_REGISTER_ADRR (0x10000000) #define COIL_REGISTER_NUMBER (0x2710) #define COIL_REGISTER_SIZE (1250) /** * @brief 数据寄存器地址映射,数量,占用字节总量 * @details * @note * @attention */ #define DATA_REGISTER_ADRR (0x10000000 | 0x04F0) #define DATA_REGISTER_NUMBER (0x2710) #define DATA_REGISTER_SIZE (20000) /** * @brief 保持寄存器地址映射,数量,占用字节总量 * @details * @note * @attention */ #define KEEP_REGISTER_ADRR (0x40024000) #define KEEP_REGISTER_NUMBER (0x800) #define KEEP_REGISTER_SIZE (4096) /** * @brief modbus发送数据帧和接收数据帧返回结果 * @details * @note * @attention */ typedef enum { MODBUS_TURE = 0, ///< 正确 MODBUS_FALSE ///< 错误 } RESUIL; /** * @brief modbus操作对象类型 * @details * @note * @attention */ typedef enum { OPERATE_COIL = 0x00, ///< 线圈 OPERATE_REGISTER = 0x01, ///< 寄存器 } OPERATE_OBJ_TYPE; /** * @brief modbus操作类型 * @details * @note * @attention */ typedef enum { OPERATE_READ = 0x00, ///< 读操作 OPERATE_WRITE = 0x01, ///< 写操作 } OPERATE_TYPE; /** * @brief modbus协议格式 * @details * @note * @attention */ typedef enum { MODBUS_STANDARD = 0, ///< 标准格式 MODBUS_EXT ///< 扩展格式 } MODBUS_FORMAT; /** * @brief modbus功能码类型 * @details * @note * @attention */ typedef enum { READ_COIL = 0x01, ///< 读多个线圈 READ_KEEP_REGISTER = 0x03, ///< 读多个保持寄存器 WRITE_ONE_COIL = 0x05, ///< 写单个线圈 WRITE_COIL = 0x0F, ///< 写多个线圈 WRITE_KEEP_REGISTER = 0x10, ///< 写多个保持寄存器 READ_OUT_SCOPE_REGISTER = 0x64, ///< 读范围外寄存器 READ_ALL_SINGULAR_REGISTER = 0x65, ///< 读所有单数个寄存器 READ_CORRESPONDENCE_HISTORY = 0x66 ///< 读通信历史 } FUNCTION_CODE_TYPE; /** * @brief modbus通信历史类型 * @details * @note * @attention */ typedef struct { uint16_t byteLenght; uint8_t historyCount; uint8_t index; uint8_t data[512]; } CORRESPONDENCE_HISTORY; /** * @brief modbus返回错误码类型 * @details * @note * @attention */ typedef enum { ERROR_NONE = 0, ///< 无错误 ERROR_INVALID_FUNCTION = 0x01, ///< 非法功能 ERROR_INVALID_DATA_ADDR = 0x02, ///< 非法数据地址 ERROR_INVALID_DATA_VALUE = 0x03, ///< 非法数据值 ERROR_SLAVE_DEVICE_FAUIL = 0x04, ///< 从设备故障 ERROR_SLAVE_DEVICE_CONFIRM = 0x05, ///< 从设备确认 ERROR_SLAVE_DEVICE_BUSY = 0x06, ///< 从设备忙 ERROR_SLAVE_DEVICE_NO_ACK = 0x07, ///< 从设备无响应 ERROR_SLAVE_DEVICE_MEM0RY = 0x08, ///< 存储偶然错误 ERROR_DATA_READ_ONLY = 0x09, ///< 只读数据 ERROR_FRAME_FORMAT = 0x10, ///< 帧格式错误 ERROR_DATA_OVER = 0x11 ///< 数据溢出 } ERROR_CODE_TYPE; /** * @brief modbus发送帧结构 * @details * @note * @attention */ typedef struct { uint8_t deviceNumber; ///< 设备号 uint8_t writeByteLenght; ///< 写入字节长度 uint8_t *data; ///< 数据缓冲区 uint16_t memoryAddr; ///< 起始内存地址 uint16_t readCount; ///< 读取数量 uint16_t writeCount; ///< 写入数量 uint16_t CRCValue; ///< CRC校验值 uint32_t extMemoryAddr; ///< 扩展内存地址 MODBUS_FORMAT format; ///< 协议格式 FUNCTION_CODE_TYPE functionCode; ///< 功能码 } REQUEST_FRAME; /** * @brief modbus应答帧结构 * @details * @note * @attention */ typedef struct { uint8_t deviceNumber; ///< 设备号 uint8_t *data; ///< 数据缓冲区 uint16_t memoryAddr; ///< 起始内存地址 uint16_t returnByteCount; ///< 返回字节数 uint16_t CRCValue; ///< CRC校验值 uint32_t extMemoryAddr; ///< 扩展内存地址 MODBUS_FORMAT format; ///< 协议格式 FUNCTION_CODE_TYPE functionCode; ///< 功能码 ERROR_CODE_TYPE errorCode; ///< 错误码 OPERATE_OBJ_TYPE operateObjType; ///< 操作对象类型 OPERATE_TYPE operateType; ///< 操作类型 } ACK_FRAME; /** * @brief 支持的功能数量 * @details * * @param[in] 无 * * @return 无 */ #define FUNCTION_CODE_NUMBER 8 /** * @brief 打印提示信息 * @details * * @param[in] string 提示信息 * * @return 无 */ #define MODBUS_LOG(string) //printf("%s%s, %d\r\n", string, __FILE__, __LINE__) /** * @brief 断言 * @details * * @param[in] x 检测输入参数是否合法 * * @return 无 */ #define MODBUS_ASSERT(x) if(!x) printf("param error, %s, %d\r\n", __FILE__, __LINE__) #endif // !_MODBUS_H