/** * @file agile_modbus.h * @brief Agile Modbus software package common header file * @author Ma Longwei (2544047213@qq.com) * @date 2022-07-28 * * @attention * *

© Copyright (c) 2021 Ma Longwei. * All rights reserved.

* */ #ifndef __PKG_AGILE_MODBUS_H #define __PKG_AGILE_MODBUS_H #ifdef __cplusplus extern "C" { #endif #include /** @addtogroup COMMON * @{ */ /** @defgroup Modbus_Module_Definition Modbus Module Definition * @{ */ #ifndef AGILE_MODBUS_USING_RTU #define AGILE_MODBUS_USING_RTU 1 #endif /* AGILE_MODBUS_USING_RTU */ #ifndef AGILE_MODBUS_USING_TCP #define AGILE_MODBUS_USING_TCP 1 #endif /* AGILE_MODBUS_USING_TCP */ /** * @} */ /** @defgroup COMMON_Exported_Constants Common Exported Constants * @{ */ /** @defgroup Modbus_Function_Codes Modbus Function Codes * @{ */ #define AGILE_MODBUS_FC_READ_COILS 0x01 #define AGILE_MODBUS_FC_READ_DISCRETE_INPUTS 0x02 #define AGILE_MODBUS_FC_READ_HOLDING_REGISTERS 0x03 #define AGILE_MODBUS_FC_READ_INPUT_REGISTERS 0x04 #define AGILE_MODBUS_FC_WRITE_SINGLE_COIL 0x05 #define AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER 0x06 #define AGILE_MODBUS_FC_READ_EXCEPTION_STATUS 0x07 #define AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F #define AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10 #define AGILE_MODBUS_FC_REPORT_SLAVE_ID 0x11 #define AGILE_MODBUS_FC_MASK_WRITE_REGISTER 0x16 #define AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17 /** * @} */ /** @defgroup Modbus_Constants Modbus Constants * @{ */ #define AGILE_MODBUS_VERSION_STRING "AMB_1.1.4" /**< Agile Modbus version number */ #define AGILE_MODBUS_BROADCAST_ADDRESS 0 /**< Modbus broadcast address */ /** @name Quantity limit of Coils @verbatim Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12) Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0) (chapter 6 section 11 page 29) Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0) @endverbatim * @{ */ #define AGILE_MODBUS_MAX_READ_BITS 2000 #define AGILE_MODBUS_MAX_WRITE_BITS 1968 /** * @} */ /** @name Quantity limit of Registers @verbatim Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15) Quantity of Registers to read (2 bytes): 1 to 125 (0x7D) (chapter 6 section 12 page 31) Quantity of Registers to write (2 bytes) 1 to 123 (0x7B) (chapter 6 section 17 page 38) Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79) @endverbatim * @{ */ #define AGILE_MODBUS_MAX_READ_REGISTERS 125 #define AGILE_MODBUS_MAX_WRITE_REGISTERS 123 #define AGILE_MODBUS_MAX_WR_WRITE_REGISTERS 121 #define AGILE_MODBUS_MAX_WR_READ_REGISTERS 125 /** * @} */ /** @verbatim The size of the MODBUS PDU is limited by the size constraint inherited from the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256 bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server address (1 byte) - CRC (2 bytes) = 253 bytes. @endverbatim */ #define AGILE_MODBUS_MAX_PDU_LENGTH 253 /** @verbatim Consequently: - RTU MODBUS ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256 bytes. - TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes. so the maximum of both backend in 260 bytes. This size can used to allocate an array of bytes to store responses and it will be compatible with the two backends. @endverbatim */ #define AGILE_MODBUS_MAX_ADU_LENGTH 260 /** * @} */ /** * @} */ /** @defgroup COMMON_Exported_Types Common Exported Types * @{ */ /** * @brief Modbus exception code */ enum { AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01, AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS, AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE, AGILE_MODBUS_EXCEPTION_ACKNOWLEDGE, AGILE_MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY, AGILE_MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE, AGILE_MODBUS_EXCEPTION_MEMORY_PARITY, AGILE_MODBUS_EXCEPTION_NOT_DEFINED, AGILE_MODBUS_EXCEPTION_GATEWAY_PATH, AGILE_MODBUS_EXCEPTION_GATEWAY_TARGET, AGILE_MODBUS_EXCEPTION_UNKNOW = 0xff }; /** *@ brief Modbus backend type */ typedef enum { AGILE_MODBUS_BACKEND_TYPE_RTU = 0, /**< RTU */ AGILE_MODBUS_BACKEND_TYPE_TCP /**< TCP */ } agile_modbus_backend_type_t; /** * @brief Modbus received message type * @verbatim ---------- Request Indication ---------- | Client | ---------------------->| Server | ---------- Confirmation Response ---------- @endverbatim */ typedef enum { AGILE_MODBUS_MSG_INDICATION, /**< Host-side request message */ AGILE_MODBUS_MSG_CONFIRMATION /**< Server-side request message */ } agile_modbus_msg_type_t; /** * @brief contains the modbus header parameter structure */ typedef struct agile_modbus_sft { int slave; /**< slave address */ int function; /**< function code */ int t_id; /**< Transaction identifier */ } agile_modbus_sft_t; typedef struct agile_modbus agile_modbus_t; /**< Agile Modbus structure */ /** * @brief Agile Modbus backend interface structure */ typedef struct agile_modbus_backend { uint32_t backend_type; /**< Backend type */ uint32_t header_length; /**