mirror of
https://github.com/0xFEEDC0DE64/arduino-esp32.git
synced 2025-07-28 09:47:14 +02:00
IDF release/v4.0 08219f3cf
This commit is contained in:
138
tools/sdk/include/freemodbus/esp_modbus_common.h
Normal file
138
tools/sdk/include/freemodbus/esp_modbus_common.h
Normal file
@ -0,0 +1,138 @@
|
||||
/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _MB_IFACE_COMMON_H
|
||||
#define _MB_IFACE_COMMON_H
|
||||
|
||||
#include "driver/uart.h" // for UART types
|
||||
|
||||
#define MB_CONTROLLER_STACK_SIZE (CONFIG_FMB_CONTROLLER_STACK_SIZE) // Stack size for Modbus controller
|
||||
#define MB_CONTROLLER_PRIORITY (CONFIG_FMB_SERIAL_TASK_PRIO - 1) // priority of MB controller task
|
||||
|
||||
// Default port defines
|
||||
#define MB_DEVICE_ADDRESS (1) // Default slave device address in Modbus
|
||||
#define MB_DEVICE_SPEED (115200) // Default Modbus speed for now hard defined
|
||||
#define MB_UART_PORT (UART_NUM_MAX - 1) // Default UART port number
|
||||
#define MB_PAR_INFO_TOUT (10) // Timeout for get parameter info
|
||||
#define MB_PARITY_NONE (UART_PARITY_DISABLE)
|
||||
|
||||
// The Macros below handle the endianness while transfer N byte data into buffer
|
||||
#define _XFER_4_RD(dst, src) { \
|
||||
*(uint8_t *)(dst)++ = *(uint8_t*)(src + 1); \
|
||||
*(uint8_t *)(dst)++ = *(uint8_t*)(src + 0); \
|
||||
*(uint8_t *)(dst)++ = *(uint8_t*)(src + 3); \
|
||||
*(uint8_t *)(dst)++ = *(uint8_t*)(src + 2); \
|
||||
(src) += 4; \
|
||||
}
|
||||
|
||||
#define _XFER_2_RD(dst, src) { \
|
||||
*(uint8_t *)(dst)++ = *(uint8_t *)(src + 1); \
|
||||
*(uint8_t *)(dst)++ = *(uint8_t *)(src + 0); \
|
||||
(src) += 2; \
|
||||
}
|
||||
|
||||
#define _XFER_4_WR(dst, src) { \
|
||||
*(uint8_t *)(dst + 1) = *(uint8_t *)(src)++; \
|
||||
*(uint8_t *)(dst + 0) = *(uint8_t *)(src)++; \
|
||||
*(uint8_t *)(dst + 3) = *(uint8_t *)(src)++; \
|
||||
*(uint8_t *)(dst + 2) = *(uint8_t *)(src)++ ; \
|
||||
}
|
||||
|
||||
#define _XFER_2_WR(dst, src) { \
|
||||
*(uint8_t *)(dst + 1) = *(uint8_t *)(src)++; \
|
||||
*(uint8_t *)(dst + 0) = *(uint8_t *)(src)++; \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Types of actual Modbus implementation
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_PORT_SERIAL_MASTER = 0x00, /*!< Modbus port type serial master. */
|
||||
MB_PORT_SERIAL_SLAVE, /*!< Modbus port type serial slave. */
|
||||
MB_PORT_TCP_MASTER, /*!< Modbus port type TCP master. */
|
||||
MB_PORT_TCP_SLAVE, /*!< Modbus port type TCP slave. */
|
||||
MB_PORT_COUNT /*!< Modbus port count. */
|
||||
} mb_port_type_t;
|
||||
|
||||
/**
|
||||
* @brief Event group for parameters notification
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_EVENT_NO_EVENTS = 0x00,
|
||||
MB_EVENT_HOLDING_REG_WR = BIT0, /*!< Modbus Event Write Holding registers. */
|
||||
MB_EVENT_HOLDING_REG_RD = BIT1, /*!< Modbus Event Read Holding registers. */
|
||||
MB_EVENT_INPUT_REG_RD = BIT3, /*!< Modbus Event Read Input registers. */
|
||||
MB_EVENT_COILS_WR = BIT4, /*!< Modbus Event Write Coils. */
|
||||
MB_EVENT_COILS_RD = BIT5, /*!< Modbus Event Read Coils. */
|
||||
MB_EVENT_DISCRETE_RD = BIT6, /*!< Modbus Event Read Discrete bits. */
|
||||
MB_EVENT_STACK_STARTED = BIT7 /*!< Modbus Event Stack started */
|
||||
} mb_event_group_t;
|
||||
|
||||
/**
|
||||
* @brief Type of Modbus parameter
|
||||
*/
|
||||
typedef enum {
|
||||
MB_PARAM_HOLDING = 0x00, /*!< Modbus Holding register. */
|
||||
MB_PARAM_INPUT, /*!< Modbus Input register. */
|
||||
MB_PARAM_COIL, /*!< Modbus Coils. */
|
||||
MB_PARAM_DISCRETE, /*!< Modbus Discrete bits. */
|
||||
MB_PARAM_COUNT,
|
||||
MB_PARAM_UNKNOWN = 0xFF
|
||||
} mb_param_type_t;
|
||||
|
||||
/*!
|
||||
* \brief Modbus serial transmission modes (RTU/ASCII).
|
||||
*/
|
||||
typedef enum {
|
||||
MB_MODE_RTU, /*!< RTU transmission mode. */
|
||||
MB_MODE_ASCII, /*!< ASCII transmission mode. */
|
||||
MB_MODE_TCP /*!< TCP mode. */
|
||||
} mb_mode_type_t; // Todo: This is common type leave it here for now
|
||||
|
||||
/**
|
||||
* @brief Device communication structure to setup Modbus controller
|
||||
*/
|
||||
typedef union {
|
||||
// Serial communication structure
|
||||
struct {
|
||||
mb_mode_type_t mode; /*!< Modbus communication mode */
|
||||
uint8_t slave_addr; /*!< Modbus slave address field (dummy for master) */
|
||||
uart_port_t port; /*!< Modbus communication port (UART) number */
|
||||
uint32_t baudrate; /*!< Modbus baudrate */
|
||||
uart_parity_t parity; /*!< Modbus UART parity settings */
|
||||
uint16_t dummy_port;
|
||||
};
|
||||
// Tcp communication structure
|
||||
struct {
|
||||
mb_mode_type_t tcp_mode; /*!< Modbus communication mode */
|
||||
uint8_t dummy_addr; /*!< Modbus slave address field (dummy for master) */
|
||||
uart_port_t dummy_uart_port; /*!< Modbus communication port (UART) number */
|
||||
uint32_t dummy_baudrate; /*!< Modbus baudrate */
|
||||
uart_parity_t dummy_parity; /*!< Modbus UART parity settings */
|
||||
uint16_t tcp_port; /*!< Modbus TCP port */
|
||||
};
|
||||
} mb_communication_info_t;
|
||||
|
||||
/**
|
||||
* common interface method types
|
||||
*/
|
||||
typedef esp_err_t (*iface_init)(mb_port_type_t, void**); /*!< Interface method init */
|
||||
typedef esp_err_t (*iface_destroy)(void); /*!< Interface method destroy */
|
||||
typedef esp_err_t (*iface_setup)(void*); /*!< Interface method setup */
|
||||
typedef esp_err_t (*iface_start)(void); /*!< Interface method start */
|
||||
|
||||
#endif // _MB_IFACE_COMMON_H
|
241
tools/sdk/include/freemodbus/esp_modbus_master.h
Normal file
241
tools/sdk/include/freemodbus/esp_modbus_master.h
Normal file
@ -0,0 +1,241 @@
|
||||
/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _ESP_MB_MASTER_INTERFACE_H
|
||||
#define _ESP_MB_MASTER_INTERFACE_H
|
||||
|
||||
#include <stdint.h> // for standard int types definition
|
||||
#include <stddef.h> // for NULL and std defines
|
||||
#include "soc/soc.h" // for BITN definitions
|
||||
#include "esp_modbus_common.h" // for common types
|
||||
|
||||
/*!
|
||||
* \brief Modbus descriptor table parameter type defines.
|
||||
*/
|
||||
typedef enum {
|
||||
PARAM_TYPE_U8 = 0x00, /*!< Unsigned 8 */
|
||||
PARAM_TYPE_U16 = 0x01, /*!< Unsigned 16 */
|
||||
PARAM_TYPE_U32 = 0x02, /*!< Unsigned 32 */
|
||||
PARAM_TYPE_FLOAT = 0x03, /*!< Float type */
|
||||
PARAM_TYPE_ASCII = 0x04 /*!< ASCII type */
|
||||
} mb_descr_type_t;
|
||||
|
||||
/*!
|
||||
* \brief Modbus descriptor table parameter size in bytes.
|
||||
*/
|
||||
typedef enum {
|
||||
PARAM_SIZE_U8 = 0x01, /*!< Unsigned 8 */
|
||||
PARAM_SIZE_U16 = 0x02, /*!< Unsigned 16 */
|
||||
PARAM_SIZE_U32 = 0x04, /*!< Unsigned 32 */
|
||||
PARAM_SIZE_FLOAT = 0x04, /*!< Float size */
|
||||
PARAM_SIZE_ASCII = 0x08, /*!< ASCII size */
|
||||
PARAM_SIZE_ASCII24 = 0x18, /*!< ASCII24 size */
|
||||
PARAM_MAX_SIZE
|
||||
} mb_descr_size_t;
|
||||
|
||||
/*!
|
||||
* \brief Modbus parameter options for description table
|
||||
*/
|
||||
typedef union {
|
||||
struct {
|
||||
int opt1; /*!< Parameter option1 */
|
||||
int opt2; /*!< Parameter option2 */
|
||||
int opt3; /*!< Parameter option3 */
|
||||
};
|
||||
struct {
|
||||
int min; /*!< Parameter minimum value */
|
||||
int max; /*!< Parameter maximum value */
|
||||
int step; /*!< Step of parameter change tracking */
|
||||
};
|
||||
} mb_parameter_opt_t;
|
||||
|
||||
/**
|
||||
* @brief Permissions for the characteristics
|
||||
*/
|
||||
typedef enum {
|
||||
PAR_PERMS_READ = 1 << BIT0, /**< the characteristic of the device are readable */
|
||||
PAR_PERMS_WRITE = 1 << BIT1, /**< the characteristic of the device are writable*/
|
||||
PAR_PERMS_TRIGGER = 1 << BIT2, /**< the characteristic of the device are triggerable */
|
||||
PAR_PERMS_READ_WRITE = PAR_PERMS_READ | PAR_PERMS_WRITE, /**< the characteristic of the device are readable & writable */
|
||||
PAR_PERMS_READ_TRIGGER = PAR_PERMS_READ | PAR_PERMS_TRIGGER, /**< the characteristic of the device are readable & triggerable */
|
||||
PAR_PERMS_WRITE_TRIGGER = PAR_PERMS_WRITE | PAR_PERMS_TRIGGER, /**< the characteristic of the device are writable & triggerable */
|
||||
PAR_PERMS_READ_WRITE_TRIGGER = PAR_PERMS_READ_WRITE | PAR_PERMS_TRIGGER, /**< the characteristic of the device are readable & writable & triggerable */
|
||||
} mb_param_perms_t;
|
||||
|
||||
/**
|
||||
* @brief Characteristics descriptor type is used to describe characteristic and
|
||||
* link it with Modbus parameters that reflect its data.
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t cid; /*!< Characteristic cid */
|
||||
const char* param_key; /*!< The key (name) of the parameter */
|
||||
const char* param_units; /*!< The physical units of the parameter */
|
||||
uint8_t mb_slave_addr; /*!< Slave address of device in the Modbus segment */
|
||||
mb_param_type_t mb_param_type; /*!< Type of modbus parameter */
|
||||
uint16_t mb_reg_start; /*!< This is the Modbus register address. This is the 0 based value. */
|
||||
uint16_t mb_size; /*!< Size of mb parameter in registers */
|
||||
uint16_t param_offset; /*!< Parameter name (OFFSET in the parameter structure) */
|
||||
mb_descr_type_t param_type; /*!< Float, U8, U16, U32, ASCII, etc. */
|
||||
mb_descr_size_t param_size; /*!< Number of bytes in the parameter. */
|
||||
mb_parameter_opt_t param_opts; /*!< Parameter options used to check limits and etc. */
|
||||
mb_param_perms_t access; /*!< Access permissions based on mode */
|
||||
} mb_parameter_descriptor_t;
|
||||
|
||||
/**
|
||||
* @brief Modbus register request type structure
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t slave_addr; /*!< Modbus slave address */
|
||||
uint8_t command; /*!< Modbus command to send */
|
||||
uint16_t reg_start; /*!< Modbus start register */
|
||||
uint16_t reg_size; /*!< Modbus number of registers */
|
||||
} mb_param_request_t;
|
||||
|
||||
// Master interface public functions
|
||||
/**
|
||||
* @brief Initialize Modbus controller and stack
|
||||
*
|
||||
* @param[out] handler handler(pointer) to master data structure
|
||||
* @param[in] port_type the type of port
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_NO_MEM Parameter error
|
||||
*/
|
||||
esp_err_t mbc_master_init(mb_port_type_t port_type, void** handler);
|
||||
|
||||
/**
|
||||
* @brief Destroy Modbus controller and stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_STATE Parameter error
|
||||
*/
|
||||
esp_err_t mbc_master_destroy(void);
|
||||
|
||||
/**
|
||||
* @brief Start Modbus communication stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_ARG Modbus stack start error
|
||||
*/
|
||||
esp_err_t mbc_master_start(void);
|
||||
|
||||
/**
|
||||
* @brief Set Modbus communication parameters for the controller
|
||||
*
|
||||
* @param comm_info Communication parameters structure.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_ARG Incorrect parameter data
|
||||
*/
|
||||
esp_err_t mbc_master_setup(void* comm_info);
|
||||
|
||||
/***************************** Specific interface functions ********************************************
|
||||
* Interface functions below provide basic methods to read/write access to slave devices in Modbus
|
||||
* segment as well as API to read specific supported characteristics linked to Modbus parameters
|
||||
* of devices in Modbus network.
|
||||
*******************************************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Assign parameter description table for Modbus controller interface.
|
||||
*
|
||||
* @param[in] descriptor pointer to parameter description table
|
||||
* @param num_elements number of elements in the table
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - set descriptor successfully
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument in function call
|
||||
*/
|
||||
esp_err_t mbc_master_set_descriptor(const mb_parameter_descriptor_t* descriptor, const uint16_t num_elements);
|
||||
|
||||
/**
|
||||
* @brief Send data request as defined in parameter request, waits response
|
||||
* from slave and returns status of command execution. This function provides standard way
|
||||
* for read/write access to Modbus devices in the network.
|
||||
*
|
||||
* @param[in] request pointer to request structure of type mb_param_request_t
|
||||
* @param[in] data_ptr pointer to data buffer to send or received data (dependent of command field in request)
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - request was successful
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
|
||||
* - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave
|
||||
* - esp_err_t ESP_ERR_TIMEOUT - operation timeout or no response from slave
|
||||
* - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
|
||||
* - esp_err_t ESP_FAIL - slave returned an exception or other failure
|
||||
*/
|
||||
esp_err_t mbc_master_send_request(mb_param_request_t* request, void* data_ptr);
|
||||
|
||||
/**
|
||||
* @brief Get information about supported characteristic defined as cid. Uses parameter description table to get
|
||||
* this information. The function will check if characteristic defined as a cid parameter is supported
|
||||
* and returns its description in param_info. Returns ESP_ERR_NOT_FOUND if characteristic is not supported.
|
||||
*
|
||||
* @param[in] cid characteristic id
|
||||
* @param param_info pointer to pointer of characteristic data.
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - request was successful and buffer contains the supported characteristic name
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
|
||||
* - esp_err_t ESP_ERR_NOT_FOUND - the characteristic (cid) not found
|
||||
* - esp_err_t ESP_FAIL - unknown error during lookup table processing
|
||||
*/
|
||||
esp_err_t mbc_master_get_cid_info(uint16_t cid, const mb_parameter_descriptor_t** param_info);
|
||||
|
||||
/**
|
||||
* @brief Read parameter from modbus slave device whose name is defined by name and has cid.
|
||||
* The additional data for request is taken from parameter description (lookup) table.
|
||||
*
|
||||
* @param[in] cid id of the characteristic for parameter
|
||||
* @param[in] name pointer into string name (key) of parameter (null terminated)
|
||||
* @param[out] value pointer to data buffer of parameter
|
||||
* @param[out] type parameter type associated with the name returned from parameter description table.
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - request was successful and value buffer contains
|
||||
* representation of actual parameter data from slave
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
|
||||
* - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave
|
||||
* - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
|
||||
* - esp_err_t ESP_ERR_TIMEOUT - operation timed out and no response from slave
|
||||
* - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
|
||||
* - esp_err_t ESP_ERR_NOT_FOUND - the parameter is not found in the parameter description table
|
||||
* - esp_err_t ESP_FAIL - slave returned an exception or other failure
|
||||
*/
|
||||
esp_err_t mbc_master_get_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t *type);
|
||||
|
||||
/**
|
||||
* @brief Set characteristic's value defined as a name and cid parameter.
|
||||
* The additional data for cid parameter request is taken from master parameter lookup table.
|
||||
*
|
||||
* @param[in] cid id of the characteristic for parameter
|
||||
* @param[in] name pointer into string name (key) of parameter (null terminated)
|
||||
* @param[out] value pointer to data buffer of parameter (actual representation of json value field in binary form)
|
||||
* @param[out] type pointer to parameter type associated with the name returned from parameter lookup table.
|
||||
*
|
||||
* @return
|
||||
* - esp_err_t ESP_OK - request was successful and value was saved in the slave device registers
|
||||
* - esp_err_t ESP_ERR_INVALID_ARG - invalid argument of function
|
||||
* - esp_err_t ESP_ERR_INVALID_RESPONSE - an invalid response from slave during processing of parameter
|
||||
* - esp_err_t ESP_ERR_INVALID_STATE - invalid state during data processing or allocation failure
|
||||
* - esp_err_t ESP_ERR_TIMEOUT - operation timed out and no response from slave
|
||||
* - esp_err_t ESP_ERR_NOT_SUPPORTED - the request command is not supported by slave
|
||||
* - esp_err_t ESP_FAIL - slave returned an exception or other failure
|
||||
*/
|
||||
esp_err_t mbc_master_set_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t *type);
|
||||
|
||||
#endif // _ESP_MB_MASTER_INTERFACE_H
|
123
tools/sdk/include/freemodbus/esp_modbus_slave.h
Normal file
123
tools/sdk/include/freemodbus/esp_modbus_slave.h
Normal file
@ -0,0 +1,123 @@
|
||||
/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _ESP_MB_SLAVE_INTERFACE_H
|
||||
#define _ESP_MB_SLAVE_INTERFACE_H
|
||||
|
||||
// Public interface header for slave
|
||||
#include <stdint.h> // for standard int types definition
|
||||
#include <stddef.h> // for NULL and std defines
|
||||
#include "soc/soc.h" // for BITN definitions
|
||||
#include "freertos/FreeRTOS.h" // for task creation and queues access
|
||||
#include "freertos/event_groups.h" // for event groups
|
||||
#include "esp_modbus_common.h" // for common types
|
||||
|
||||
/**
|
||||
* @brief Parameter access event information type
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t time_stamp; /*!< Timestamp of Modbus Event (uS)*/
|
||||
uint16_t mb_offset; /*!< Modbus register offset */
|
||||
mb_event_group_t type; /*!< Modbus event type */
|
||||
uint8_t* address; /*!< Modbus data storage address */
|
||||
size_t size; /*!< Modbus event register size (number of registers)*/
|
||||
} mb_param_info_t;
|
||||
|
||||
/**
|
||||
* @brief Parameter storage area descriptor
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t start_offset; /*!< Modbus start address for area descriptor */
|
||||
mb_param_type_t type; /*!< Type of storage area descriptor */
|
||||
void* address; /*!< Instance address for storage area descriptor */
|
||||
size_t size; /*!< Instance size for area descriptor (bytes) */
|
||||
} mb_register_area_descriptor_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize Modbus controller and stack
|
||||
*
|
||||
* @param[out] handler handler(pointer) to master data structure
|
||||
* @param[in] port_type type of stack
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_NO_MEM Parameter error
|
||||
*/
|
||||
//esp_err_t mbc_slave_init(mb_port_type_t port_type, void** handler);
|
||||
esp_err_t mbc_slave_init(mb_port_type_t port_type, void** handler);
|
||||
|
||||
/**
|
||||
* @brief Destroy Modbus controller and stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_STATE Parameter error
|
||||
*/
|
||||
esp_err_t mbc_slave_destroy(void);
|
||||
|
||||
/**
|
||||
* @brief Start Modbus communication stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_ARG Modbus stack start error
|
||||
*/
|
||||
esp_err_t mbc_slave_start(void);
|
||||
|
||||
/**
|
||||
* @brief Set Modbus communication parameters for the controller
|
||||
*
|
||||
* @param comm_info Communication parameters structure.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_ARG Incorrect parameter data
|
||||
*/
|
||||
esp_err_t mbc_slave_setup(void* comm_info);
|
||||
|
||||
/**
|
||||
* @brief Wait for specific event on parameter change.
|
||||
*
|
||||
* @param group Group event bit mask to wait for change
|
||||
*
|
||||
* @return
|
||||
* - mb_event_group_t event bits triggered
|
||||
*/
|
||||
mb_event_group_t mbc_slave_check_event(mb_event_group_t group);
|
||||
|
||||
/**
|
||||
* @brief Get parameter information
|
||||
*
|
||||
* @param[out] reg_info parameter info structure
|
||||
* @param timeout Timeout in milliseconds to read information from
|
||||
* parameter queue
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_TIMEOUT Can not get data from parameter queue
|
||||
* or queue overflow
|
||||
*/
|
||||
esp_err_t mbc_slave_get_param_info(mb_param_info_t* reg_info, uint32_t timeout);
|
||||
|
||||
/**
|
||||
* @brief Set Modbus area descriptor
|
||||
*
|
||||
* @param descr_data Modbus registers area descriptor structure
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: The appropriate descriptor is set
|
||||
* - ESP_ERR_INVALID_ARG: The argument is incorrect
|
||||
*/
|
||||
esp_err_t mbc_slave_set_descriptor(mb_register_area_descriptor_t descr_data);
|
||||
|
||||
#endif
|
@ -1,417 +0,0 @@
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mb.h,v 1.17 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_H
|
||||
#define _MB_H
|
||||
|
||||
#include "port.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
#include "mbport.h"
|
||||
#include "mbproto.h"
|
||||
|
||||
/*! \defgroup modbus Modbus
|
||||
* \code #include "mb.h" \endcode
|
||||
*
|
||||
* This module defines the interface for the application. It contains
|
||||
* the basic functions and types required to use the Modbus protocol stack.
|
||||
* A typical application will want to call eMBInit() first. If the device
|
||||
* is ready to answer network requests it must then call eMBEnable() to activate
|
||||
* the protocol stack. In the main loop the function eMBPoll() must be called
|
||||
* periodically. The time interval between pooling depends on the configured
|
||||
* Modbus timeout. If an RTOS is available a separate task should be created
|
||||
* and the task should always call the function eMBPoll().
|
||||
*
|
||||
* \code
|
||||
* // Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A
|
||||
* eMBInit( MB_RTU, 0x0A, 38400, MB_PAR_EVEN );
|
||||
* // Enable the Modbus Protocol Stack.
|
||||
* eMBEnable( );
|
||||
* for( ;; )
|
||||
* {
|
||||
* // Call the main polling loop of the Modbus protocol stack.
|
||||
* eMBPoll( );
|
||||
* ...
|
||||
* }
|
||||
* \endcode
|
||||
*/
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Use the default Modbus TCP port (502)
|
||||
*/
|
||||
#define MB_TCP_PORT_USE_DEFAULT 0
|
||||
|
||||
/* ----------------------- Type definitions ---------------------------------*/
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Modbus serial transmission modes (RTU/ASCII).
|
||||
*
|
||||
* Modbus serial supports two transmission modes. Either ASCII or RTU. RTU
|
||||
* is faster but has more hardware requirements and requires a network with
|
||||
* a low jitter. ASCII is slower and more reliable on slower links (E.g. modems)
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_RTU, /*!< RTU transmission mode. */
|
||||
MB_ASCII, /*!< ASCII transmission mode. */
|
||||
MB_TCP /*!< TCP mode. */
|
||||
} eMBMode;
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief If register should be written or read.
|
||||
*
|
||||
* This value is passed to the callback functions which support either
|
||||
* reading or writing register values. Writing means that the application
|
||||
* registers should be updated and reading means that the modbus protocol
|
||||
* stack needs to know the current register values.
|
||||
*
|
||||
* \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and
|
||||
* eMBRegInputCB( ).
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_REG_READ, /*!< Read register values and pass to protocol stack. */
|
||||
MB_REG_WRITE /*!< Update register values. */
|
||||
} eMBRegisterMode;
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Errorcodes used by all function in the protocol stack.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_ENOERR, /*!< no error. */
|
||||
MB_ENOREG, /*!< illegal register address. */
|
||||
MB_EINVAL, /*!< illegal argument. */
|
||||
MB_EPORTERR, /*!< porting layer error. */
|
||||
MB_ENORES, /*!< insufficient resources. */
|
||||
MB_EIO, /*!< I/O error. */
|
||||
MB_EILLSTATE, /*!< protocol stack in illegal state. */
|
||||
MB_ETIMEDOUT /*!< timeout error occurred. */
|
||||
} eMBErrorCode;
|
||||
|
||||
|
||||
/* ----------------------- Function prototypes ------------------------------*/
|
||||
/*! \ingroup modbus
|
||||
* \brief Initialize the Modbus protocol stack.
|
||||
*
|
||||
* This functions initializes the ASCII or RTU module and calls the
|
||||
* init functions of the porting layer to prepare the hardware. Please
|
||||
* note that the receiver is still disabled and no Modbus frames are
|
||||
* processed until eMBEnable( ) has been called.
|
||||
*
|
||||
* \param eMode If ASCII or RTU mode should be used.
|
||||
* \param ucSlaveAddress The slave address. Only frames sent to this
|
||||
* address or to the broadcast address are processed.
|
||||
* \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
|
||||
* is platform dependent and some ports simply choose to ignore it.
|
||||
* \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
|
||||
* on the porting layer.
|
||||
* \param eParity Parity used for serial transmission.
|
||||
*
|
||||
* \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
|
||||
* The protocol is then in the disabled state and ready for activation
|
||||
* by calling eMBEnable( ). Otherwise one of the following error codes
|
||||
* is returned:
|
||||
* - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
|
||||
* slave addresses are in the range 1 - 247.
|
||||
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
||||
*/
|
||||
eMBErrorCode eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
|
||||
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Initialize the Modbus protocol stack for Modbus TCP.
|
||||
*
|
||||
* This function initializes the Modbus TCP Module. Please note that
|
||||
* frame processing is still disabled until eMBEnable( ) is called.
|
||||
*
|
||||
* \param usTCPPort The TCP port to listen on.
|
||||
* \return If the protocol stack has been initialized correctly the function
|
||||
* returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
|
||||
* codes is returned:
|
||||
* - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
|
||||
* slave addresses are in the range 1 - 247.
|
||||
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
||||
*/
|
||||
eMBErrorCode eMBTCPInit( USHORT usTCPPort );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Release resources used by the protocol stack.
|
||||
*
|
||||
* This function disables the Modbus protocol stack and release all
|
||||
* hardware resources. It must only be called when the protocol stack
|
||||
* is disabled.
|
||||
*
|
||||
* \note Note all ports implement this function. A port which wants to
|
||||
* get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
|
||||
*
|
||||
* \return If the resources where released it return eMBErrorCode::MB_ENOERR.
|
||||
* If the protocol stack is not in the disabled state it returns
|
||||
* eMBErrorCode::MB_EILLSTATE.
|
||||
*/
|
||||
eMBErrorCode eMBClose( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Enable the Modbus protocol stack.
|
||||
*
|
||||
* This function enables processing of Modbus frames. Enabling the protocol
|
||||
* stack is only possible if it is in the disabled state.
|
||||
*
|
||||
* \return If the protocol stack is now in the state enabled it returns
|
||||
* eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
|
||||
* return eMBErrorCode::MB_EILLSTATE.
|
||||
*/
|
||||
eMBErrorCode eMBEnable( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Disable the Modbus protocol stack.
|
||||
*
|
||||
* This function disables processing of Modbus frames.
|
||||
*
|
||||
* \return If the protocol stack has been disabled it returns
|
||||
* eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
|
||||
* eMBErrorCode::MB_EILLSTATE.
|
||||
*/
|
||||
eMBErrorCode eMBDisable( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief The main pooling loop of the Modbus protocol stack.
|
||||
*
|
||||
* This function must be called periodically. The timer interval required
|
||||
* is given by the application dependent Modbus slave timeout. Internally the
|
||||
* function calls xMBPortEventGet() and waits for an event from the receiver or
|
||||
* transmitter state machines.
|
||||
*
|
||||
* \return If the protocol stack is not in the enabled state the function
|
||||
* returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
|
||||
* eMBErrorCode::MB_ENOERR.
|
||||
*/
|
||||
eMBErrorCode eMBPoll( void );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Configure the slave id of the device.
|
||||
*
|
||||
* This function should be called when the Modbus function <em>Report Slave ID</em>
|
||||
* is enabled ( By defining MB_FUNC_OTHER_REP_SLAVEID_ENABLED in mbconfig.h ).
|
||||
*
|
||||
* \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the
|
||||
* <em>Report Slave ID</em> response.
|
||||
* \param xIsRunning If TRUE the <em>Run Indicator Status</em> byte is set to 0xFF.
|
||||
* otherwise the <em>Run Indicator Status</em> is 0x00.
|
||||
* \param pucAdditional Values which should be returned in the <em>Additional</em>
|
||||
* bytes of the <em> Report Slave ID</em> response.
|
||||
* \param usAdditionalLen Length of the buffer <code>pucAdditonal</code>.
|
||||
*
|
||||
* \return If the static buffer defined by MB_FUNC_OTHER_REP_SLAVEID_BUF in
|
||||
* mbconfig.h is to small it returns eMBErrorCode::MB_ENORES. Otherwise
|
||||
* it returns eMBErrorCode::MB_ENOERR.
|
||||
*/
|
||||
eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
|
||||
UCHAR const *pucAdditional,
|
||||
USHORT usAdditionalLen );
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Registers a callback handler for a given function code.
|
||||
*
|
||||
* This function registers a new callback handler for a given function code.
|
||||
* The callback handler supplied is responsible for interpreting the Modbus PDU and
|
||||
* the creation of an appropriate response. In case of an error it should return
|
||||
* one of the possible Modbus exceptions which results in a Modbus exception frame
|
||||
* sent by the protocol stack.
|
||||
*
|
||||
* \param ucFunctionCode The Modbus function code for which this handler should
|
||||
* be registers. Valid function codes are in the range 1 to 127.
|
||||
* \param pxHandler The function handler which should be called in case
|
||||
* such a frame is received. If \c NULL a previously registered function handler
|
||||
* for this function code is removed.
|
||||
*
|
||||
* \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
|
||||
* more resources are available it returns eMBErrorCode::MB_ENORES. In this
|
||||
* case the values in mbconfig.h should be adjusted. If the argument was not
|
||||
* valid it returns eMBErrorCode::MB_EINVAL.
|
||||
*/
|
||||
eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode,
|
||||
pxMBFunctionHandler pxHandler );
|
||||
|
||||
/* ----------------------- Callback -----------------------------------------*/
|
||||
|
||||
/*! \defgroup modbus_registers Modbus Registers
|
||||
* \code #include "mb.h" \endcode
|
||||
* The protocol stack does not internally allocate any memory for the
|
||||
* registers. This makes the protocol stack very small and also usable on
|
||||
* low end targets. In addition the values don't have to be in the memory
|
||||
* and could for example be stored in a flash.<br>
|
||||
* Whenever the protocol stack requires a value it calls one of the callback
|
||||
* function with the register address and the number of registers to read
|
||||
* as an argument. The application should then read the actual register values
|
||||
* (for example the ADC voltage) and should store the result in the supplied
|
||||
* buffer.<br>
|
||||
* If the protocol stack wants to update a register value because a write
|
||||
* register function was received a buffer with the new register values is
|
||||
* passed to the callback function. The function should then use these values
|
||||
* to update the application register values.
|
||||
*/
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if the value of a <em>Input Register</em>
|
||||
* is required by the protocol stack. The starting register address is given
|
||||
* by \c usAddress and the last register is given by <tt>usAddress +
|
||||
* usNRegs - 1</tt>.
|
||||
*
|
||||
* \param pucRegBuffer A buffer where the callback function should write
|
||||
* the current value of the modbus registers to.
|
||||
* \param usAddress The starting address of the register. Input registers
|
||||
* are in the range 1 - 65535.
|
||||
* \param usNRegs Number of registers the callback function must supply.
|
||||
*
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If the application can not supply values
|
||||
* for registers within this range. In this case a
|
||||
* <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
|
||||
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||
* currently not available and the application dependent response
|
||||
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||
* exception is sent as a response.
|
||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs );
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if a <em>Holding Register</em> value is
|
||||
* read or written by the protocol stack. The starting register address
|
||||
* is given by \c usAddress and the last register is given by
|
||||
* <tt>usAddress + usNRegs - 1</tt>.
|
||||
*
|
||||
* \param pucRegBuffer If the application registers values should be updated the
|
||||
* buffer points to the new registers values. If the protocol stack needs
|
||||
* to now the current values the callback function should write them into
|
||||
* this buffer.
|
||||
* \param usAddress The starting address of the register.
|
||||
* \param usNRegs Number of registers to read or write.
|
||||
* \param eMode If eMBRegisterMode::MB_REG_WRITE the application register
|
||||
* values should be updated from the values in the buffer. For example
|
||||
* this would be the case when the Modbus master has issued an
|
||||
* <b>WRITE SINGLE REGISTER</b> command.
|
||||
* If the value eMBRegisterMode::MB_REG_READ the application should copy
|
||||
* the current values into the buffer \c pucRegBuffer.
|
||||
*
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If the application can not supply values
|
||||
* for registers within this range. In this case a
|
||||
* <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
|
||||
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||
* currently not available and the application dependent response
|
||||
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||
* exception is sent as a response.
|
||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNRegs, eMBRegisterMode eMode );
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if a <em>Coil Register</em> value is
|
||||
* read or written by the protocol stack. If you are going to use
|
||||
* this function you might use the functions xMBUtilSetBits( ) and
|
||||
* xMBUtilGetBits( ) for working with bitfields.
|
||||
*
|
||||
* \param pucRegBuffer The bits are packed in bytes where the first coil
|
||||
* starting at address \c usAddress is stored in the LSB of the
|
||||
* first byte in the buffer <code>pucRegBuffer</code>.
|
||||
* If the buffer should be written by the callback function unused
|
||||
* coil values (I.e. if not a multiple of eight coils is used) should be set
|
||||
* to zero.
|
||||
* \param usAddress The first coil number.
|
||||
* \param usNCoils Number of coil values requested.
|
||||
* \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
|
||||
* be updated from the values supplied in the buffer \c pucRegBuffer.
|
||||
* If eMBRegisterMode::MB_REG_READ the application should store the current
|
||||
* values in the buffer \c pucRegBuffer.
|
||||
*
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
|
||||
* within the requested address range. In this case a
|
||||
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
|
||||
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||
* currently not available and the application dependent response
|
||||
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||
* exception is sent as a response.
|
||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNCoils, eMBRegisterMode eMode );
|
||||
|
||||
/*! \ingroup modbus_registers
|
||||
* \brief Callback function used if a <em>Input Discrete Register</em> value is
|
||||
* read by the protocol stack.
|
||||
*
|
||||
* If you are going to use his function you might use the functions
|
||||
* xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.
|
||||
*
|
||||
* \param pucRegBuffer The buffer should be updated with the current
|
||||
* coil values. The first discrete input starting at \c usAddress must be
|
||||
* stored at the LSB of the first byte in the buffer. If the requested number
|
||||
* is not a multiple of eight the remaining bits should be set to zero.
|
||||
* \param usAddress The starting address of the first discrete input.
|
||||
* \param usNDiscrete Number of discrete input values.
|
||||
* \return The function must return one of the following error codes:
|
||||
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||
* Modbus response is sent.
|
||||
* - eMBErrorCode::MB_ENOREG If no such discrete inputs exists.
|
||||
* In this case a <b>ILLEGAL DATA ADDRESS</b> exception frame is sent
|
||||
* as a response.
|
||||
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||
* currently not available and the application dependent response
|
||||
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||
* exception is sent as a response.
|
||||
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||
*/
|
||||
eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||
USHORT usNDiscrete );
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
@ -1,132 +0,0 @@
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbconfig.h,v 1.15 2010/06/06 13:54:40 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_CONFIG_H
|
||||
#define _MB_CONFIG_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
/*! \defgroup modbus_cfg Modbus Configuration
|
||||
*
|
||||
* Most modules in the protocol stack are completly optional and can be
|
||||
* excluded. This is specially important if target resources are very small
|
||||
* and program memory space should be saved.<br>
|
||||
*
|
||||
* All of these settings are available in the file <code>mbconfig.h</code>
|
||||
*/
|
||||
/*! \addtogroup modbus_cfg
|
||||
* @{
|
||||
*/
|
||||
/*! \brief If Modbus ASCII support is enabled. */
|
||||
#define MB_ASCII_ENABLED ( 0 )
|
||||
|
||||
/*! \brief If Modbus RTU support is enabled. */
|
||||
#define MB_RTU_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If Modbus TCP support is enabled. */
|
||||
#define MB_TCP_ENABLED ( 0 )
|
||||
|
||||
/*! \brief The character timeout value for Modbus ASCII.
|
||||
*
|
||||
* The character timeout value is not fixed for Modbus ASCII and is therefore
|
||||
* a configuration option. It should be set to the maximum expected delay
|
||||
* time of the network.
|
||||
*/
|
||||
#define MB_ASCII_TIMEOUT_SEC ( 1 )
|
||||
|
||||
/*! \brief Timeout to wait in ASCII prior to enabling transmitter.
|
||||
*
|
||||
* If defined the function calls vMBPortSerialDelay with the argument
|
||||
* MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS to allow for a delay before
|
||||
* the serial transmitter is enabled. This is required because some
|
||||
* targets are so fast that there is no time between receiving and
|
||||
* transmitting the frame. If the master is to slow with enabling its
|
||||
* receiver then he will not receive the response correctly.
|
||||
*/
|
||||
#ifndef MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS
|
||||
#define MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ( 0 )
|
||||
#endif
|
||||
|
||||
/*! \brief Maximum number of Modbus functions codes the protocol stack
|
||||
* should support.
|
||||
*
|
||||
* The maximum number of supported Modbus functions must be greater than
|
||||
* the sum of all enabled functions in this file and custom function
|
||||
* handlers. If set to small adding more functions will fail.
|
||||
*/
|
||||
#define MB_FUNC_HANDLERS_MAX ( 16 )
|
||||
|
||||
/*! \brief Number of bytes which should be allocated for the <em>Report Slave ID
|
||||
* </em>command.
|
||||
*
|
||||
* This number limits the maximum size of the additional segment in the
|
||||
* report slave id function. See eMBSetSlaveID( ) for more information on
|
||||
* how to set this value. It is only used if MB_FUNC_OTHER_REP_SLAVEID_ENABLED
|
||||
* is set to <code>1</code>.
|
||||
*/
|
||||
#define MB_FUNC_OTHER_REP_SLAVEID_BUF ( 32 )
|
||||
|
||||
/*! \brief If the <em>Report Slave ID</em> function should be enabled. */
|
||||
#define MB_FUNC_OTHER_REP_SLAVEID_ENABLED ( CONFIG_MB_CONTROLLER_SLAVE_ID_SUPPORT )
|
||||
|
||||
/*! \brief If the <em>Read Input Registers</em> function should be enabled. */
|
||||
#define MB_FUNC_READ_INPUT_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Read Holding Registers</em> function should be enabled. */
|
||||
#define MB_FUNC_READ_HOLDING_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Write Single Register</em> function should be enabled. */
|
||||
#define MB_FUNC_WRITE_HOLDING_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Write Multiple registers</em> function should be enabled. */
|
||||
#define MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Read Coils</em> function should be enabled. */
|
||||
#define MB_FUNC_READ_COILS_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Write Coils</em> function should be enabled. */
|
||||
#define MB_FUNC_WRITE_COIL_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Write Multiple Coils</em> function should be enabled. */
|
||||
#define MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Read Discrete Inputs</em> function should be enabled. */
|
||||
#define MB_FUNC_READ_DISCRETE_INPUTS_ENABLED ( 1 )
|
||||
|
||||
/*! \brief If the <em>Read/Write Multiple Registers</em> function should be enabled. */
|
||||
#define MB_FUNC_READWRITE_HOLDING_ENABLED ( 1 )
|
||||
|
||||
/*! @} */
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
@ -1,186 +1,32 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
// mbcontroller.h
|
||||
// Implementation of the MbController
|
||||
// mbcontroller common header
|
||||
|
||||
#ifndef _MODBUS_CONTROLLER
|
||||
#define _MODBUS_CONTROLLER
|
||||
#ifndef _MODBUS_CONTROLLER_COMMON
|
||||
#define _MODBUS_CONTROLLER_COMMON
|
||||
|
||||
#include <stdint.h> // for standard int types definition
|
||||
#include <stddef.h> // for NULL and std defines
|
||||
#include "soc/soc.h" // for BITN definitions
|
||||
#include "sdkconfig.h" // for KConfig options
|
||||
#include "string.h" // for strerror()
|
||||
#include "errno.h" // for errno
|
||||
#include "esp_err.h" // for error handling
|
||||
#include "driver/uart.h" // for uart port number defines
|
||||
#include "sdkconfig.h" // for KConfig options
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_INST_MIN_SIZE (2) // The minimal size of Modbus registers area in bytes
|
||||
#define MB_INST_MAX_SIZE (65535 * 2) // The maximum size of Modbus area in bytes
|
||||
|
||||
#define MB_CONTROLLER_STACK_SIZE (CONFIG_MB_CONTROLLER_STACK_SIZE) // Stack size for Modbus controller
|
||||
#define MB_CONTROLLER_PRIORITY (CONFIG_MB_SERIAL_TASK_PRIO - 1) // priority of MB controller task
|
||||
#define MB_CONTROLLER_NOTIFY_QUEUE_SIZE (CONFIG_MB_CONTROLLER_NOTIFY_QUEUE_SIZE) // Number of messages in parameter notification queue
|
||||
#define MB_CONTROLLER_NOTIFY_TIMEOUT (pdMS_TO_TICKS(CONFIG_MB_CONTROLLER_NOTIFY_TIMEOUT)) // notification timeout
|
||||
|
||||
// Default port defines
|
||||
#define MB_DEVICE_ADDRESS (1) // Default slave device address in Modbus
|
||||
#define MB_DEVICE_SPEED (115200) // Default Modbus speed for now hard defined
|
||||
#define MB_UART_PORT (UART_NUM_2) // Default UART port number
|
||||
#define MB_PAR_INFO_TOUT (10) // Timeout for get parameter info
|
||||
#define MB_PARITY_NONE (UART_PARITY_DISABLE)
|
||||
|
||||
/**
|
||||
* @brief Event group for parameters notification
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_EVENT_NO_EVENTS = 0x00,
|
||||
MB_EVENT_HOLDING_REG_WR = BIT0, /*!< Modbus Event Write Holding registers. */
|
||||
MB_EVENT_HOLDING_REG_RD = BIT1, /*!< Modbus Event Read Holding registers. */
|
||||
MB_EVENT_INPUT_REG_RD = BIT3, /*!< Modbus Event Read Input registers. */
|
||||
MB_EVENT_COILS_WR = BIT4, /*!< Modbus Event Write Coils. */
|
||||
MB_EVENT_COILS_RD = BIT5, /*!< Modbus Event Read Coils. */
|
||||
MB_EVENT_DISCRETE_RD = BIT6, /*!< Modbus Event Read Discrete bits. */
|
||||
MB_EVENT_STACK_STARTED = BIT7 /*!< Modbus Event Stack started */
|
||||
} mb_event_group_t;
|
||||
|
||||
/**
|
||||
* @brief Type of Modbus parameter
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_PARAM_HOLDING, /*!< Modbus Holding register. */
|
||||
MB_PARAM_INPUT, /*!< Modbus Input register. */
|
||||
MB_PARAM_COIL, /*!< Modbus Coils. */
|
||||
MB_PARAM_DISCRETE, /*!< Modbus Discrete bits. */
|
||||
MB_PARAM_COUNT,
|
||||
MB_PARAM_UNKNOWN = 0xFF
|
||||
} mb_param_type_t;
|
||||
|
||||
/*!
|
||||
* \brief Modbus serial transmission modes (RTU/ASCII).
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_MODE_RTU, /*!< RTU transmission mode. */
|
||||
MB_MODE_ASCII, /*!< ASCII transmission mode. */
|
||||
MB_MODE_TCP /*!< TCP mode. */
|
||||
} mb_mode_type_t;
|
||||
|
||||
/**
|
||||
* @brief Parameter access event information type
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t time_stamp; /*!< Timestamp of Modbus Event (uS)*/
|
||||
uint16_t mb_offset; /*!< Modbus register offset */
|
||||
mb_event_group_t type; /*!< Modbus event type */
|
||||
uint8_t* address; /*!< Modbus data storage address */
|
||||
size_t size; /*!< Modbus event register size (number of registers)*/
|
||||
} mb_param_info_t;
|
||||
|
||||
/**
|
||||
* @brief Parameter storage area descriptor
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t start_offset; /*!< Modbus start address for area descriptor */
|
||||
mb_param_type_t type; /*!< Type of storage area descriptor */
|
||||
void* address; /*!< Instance address for storage area descriptor */
|
||||
size_t size; /*!< Instance size for area descriptor (bytes) */
|
||||
} mb_register_area_descriptor_t;
|
||||
|
||||
/**
|
||||
* @brief Device communication parameters
|
||||
*/
|
||||
typedef struct {
|
||||
mb_mode_type_t mode; /*!< Modbus communication mode */
|
||||
uint8_t slave_addr; /*!< Modbus Slave Address */
|
||||
uart_port_t port; /*!< Modbus communication port (UART) number */
|
||||
uint32_t baudrate; /*!< Modbus baudrate */
|
||||
uart_parity_t parity; /*!< Modbus UART parity settings */
|
||||
} mb_communication_info_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize modbus controller and stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t mbcontroller_init(void);
|
||||
|
||||
/**
|
||||
* @brief Destroy Modbus controller and stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t mbcontroller_destroy(void);
|
||||
|
||||
/**
|
||||
* @brief Start Modbus communication stack
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_ARG Modbus stack start error
|
||||
*/
|
||||
esp_err_t mbcontroller_start(void);
|
||||
|
||||
/**
|
||||
* @brief Set Modbus communication parameters for the controller
|
||||
*
|
||||
* @param comm_info Communication parameters structure.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_INVALID_ARG Incorrect parameter data
|
||||
*/
|
||||
esp_err_t mbcontroller_setup(mb_communication_info_t comm_info);
|
||||
|
||||
/**
|
||||
* @brief Wait for specific event on parameter change.
|
||||
*
|
||||
* @param group Group event bit mask to wait for change
|
||||
*
|
||||
* @return
|
||||
* - mb_event_group_t event bits triggered
|
||||
*/
|
||||
mb_event_group_t mbcontroller_check_event(mb_event_group_t group);
|
||||
|
||||
/**
|
||||
* @brief Get parameter information
|
||||
*
|
||||
* @param[out] reg_info parameter info structure
|
||||
* @param timeout Timeout in milliseconds to read information from
|
||||
* parameter queue
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_ERR_TIMEOUT Can not get data from parameter queue
|
||||
* or queue overflow
|
||||
*/
|
||||
esp_err_t mbcontroller_get_param_info(mb_param_info_t* reg_info, uint32_t timeout);
|
||||
|
||||
/**
|
||||
* @brief Set Modbus area descriptor
|
||||
*
|
||||
* @param descr_data Modbus registers area descriptor structure
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: The appropriate descriptor is set
|
||||
* - ESP_ERR_INVALID_ARG: The argument is incorrect
|
||||
*/
|
||||
esp_err_t mbcontroller_set_descriptor(mb_register_area_descriptor_t descr_data);
|
||||
|
||||
#include "esp_modbus_master.h"
|
||||
#include "esp_modbus_slave.h"
|
||||
#endif
|
||||
|
||||
|
@ -1,87 +0,0 @@
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbframe.h,v 1.9 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_FRAME_H
|
||||
#define _MB_FRAME_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* Constants which defines the format of a modbus frame. The example is
|
||||
* shown for a Modbus RTU/ASCII frame. Note that the Modbus PDU is not
|
||||
* dependent on the underlying transport.
|
||||
*
|
||||
* <code>
|
||||
* <------------------------ MODBUS SERIAL LINE PDU (1) ------------------->
|
||||
* <----------- MODBUS PDU (1') ---------------->
|
||||
* +-----------+---------------+----------------------------+-------------+
|
||||
* | Address | Function Code | Data | CRC/LRC |
|
||||
* +-----------+---------------+----------------------------+-------------+
|
||||
* | | | |
|
||||
* (2) (3/2') (3') (4)
|
||||
*
|
||||
* (1) ... MB_SER_PDU_SIZE_MAX = 256
|
||||
* (2) ... MB_SER_PDU_ADDR_OFF = 0
|
||||
* (3) ... MB_SER_PDU_PDU_OFF = 1
|
||||
* (4) ... MB_SER_PDU_SIZE_CRC = 2
|
||||
*
|
||||
* (1') ... MB_PDU_SIZE_MAX = 253
|
||||
* (2') ... MB_PDU_FUNC_OFF = 0
|
||||
* (3') ... MB_PDU_DATA_OFF = 1
|
||||
* </code>
|
||||
*/
|
||||
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_PDU_SIZE_MAX 253 /*!< Maximum size of a PDU. */
|
||||
#define MB_PDU_SIZE_MIN 1 /*!< Function Code */
|
||||
#define MB_PDU_FUNC_OFF 0 /*!< Offset of function code in PDU. */
|
||||
#define MB_PDU_DATA_OFF 1 /*!< Offset for response data in PDU. */
|
||||
|
||||
/* ----------------------- Prototypes 0-------------------------------------*/
|
||||
typedef void ( *pvMBFrameStart ) ( void );
|
||||
|
||||
typedef void ( *pvMBFrameStop ) ( void );
|
||||
|
||||
typedef eMBErrorCode( *peMBFrameReceive ) ( UCHAR * pucRcvAddress,
|
||||
UCHAR ** pucFrame,
|
||||
USHORT * pusLength );
|
||||
|
||||
typedef eMBErrorCode( *peMBFrameSend ) ( UCHAR slaveAddress,
|
||||
const UCHAR * pucFrame,
|
||||
USHORT usLength );
|
||||
|
||||
typedef void( *pvMBFrameClose ) ( void );
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
@ -1,80 +0,0 @@
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbfunc.h,v 1.12 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_FUNC_H
|
||||
#define _MB_FUNC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
#if MB_FUNC_OTHER_REP_SLAVEID_BUF > 0
|
||||
eMBException eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
||||
eMBException eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||
eMBException eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
|
||||
eMBException eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||
eMBException eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READ_COILS_ENABLED > 0
|
||||
eMBException eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||
eMBException eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||
eMBException eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
|
||||
eMBException eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||
eMBException eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
@ -1,130 +0,0 @@
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbport.h,v 1.19 2010/06/06 13:54:40 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_PORT_H
|
||||
#define _MB_PORT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
|
||||
/* ----------------------- Type definitions ---------------------------------*/
|
||||
|
||||
typedef enum
|
||||
{
|
||||
EV_READY, /*!< Startup finished. */
|
||||
EV_FRAME_RECEIVED, /*!< Frame received. */
|
||||
EV_EXECUTE, /*!< Execute function. */
|
||||
EV_FRAME_SENT /*!< Frame sent. */
|
||||
} eMBEventType;
|
||||
|
||||
/*! \ingroup modbus
|
||||
* \brief Parity used for characters in serial mode.
|
||||
*
|
||||
* The parity which should be applied to the characters sent over the serial
|
||||
* link. Please note that this values are actually passed to the porting
|
||||
* layer and therefore not all parity modes might be available.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MB_PAR_NONE, /*!< No parity. */
|
||||
MB_PAR_ODD, /*!< Odd parity. */
|
||||
MB_PAR_EVEN /*!< Even parity. */
|
||||
} eMBParity;
|
||||
|
||||
/* ----------------------- Supporting functions -----------------------------*/
|
||||
BOOL xMBPortEventInit( void );
|
||||
|
||||
BOOL xMBPortEventPost( eMBEventType eEvent );
|
||||
|
||||
BOOL xMBPortEventGet( /*@out@ */ eMBEventType * eEvent );
|
||||
|
||||
/* ----------------------- Serial port functions ----------------------------*/
|
||||
|
||||
BOOL xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
|
||||
UCHAR ucDataBits, eMBParity eParity );
|
||||
|
||||
void vMBPortClose( void );
|
||||
|
||||
void xMBPortSerialClose( void );
|
||||
|
||||
void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
|
||||
|
||||
BOOL xMBPortSerialGetByte( CHAR * pucByte );
|
||||
|
||||
BOOL xMBPortSerialPutByte( CHAR ucByte );
|
||||
|
||||
/* ----------------------- Timers functions ---------------------------------*/
|
||||
BOOL xMBPortTimersInit( USHORT usTimeOut50us );
|
||||
|
||||
void xMBPortTimersClose( void );
|
||||
|
||||
void vMBPortTimersEnable( void );
|
||||
|
||||
void vMBPortTimersDisable( void );
|
||||
|
||||
void vMBPortTimersDelay( USHORT usTimeOutMS );
|
||||
|
||||
/* ----------------------- Callback for the protocol stack ------------------*/
|
||||
/*!
|
||||
* \brief Callback function for the porting layer when a new byte is
|
||||
* available.
|
||||
*
|
||||
* Depending upon the mode this callback function is used by the RTU or
|
||||
* ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
|
||||
* must immediately return a new character.
|
||||
*
|
||||
* \return <code>TRUE</code> if a event was posted to the queue because
|
||||
* a new byte was received. The port implementation should wake up the
|
||||
* tasks which are currently blocked on the eventqueue.
|
||||
*/
|
||||
extern BOOL( *pxMBFrameCBByteReceived ) ( void );
|
||||
|
||||
extern BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
|
||||
|
||||
extern BOOL( *pxMBPortCBTimerExpired ) ( void );
|
||||
|
||||
/* ----------------------- TCP port functions -------------------------------*/
|
||||
#if MB_TCP_ENABLED == 1
|
||||
BOOL xMBTCPPortInit( USHORT usTCPPort );
|
||||
|
||||
void vMBTCPPortClose( void );
|
||||
|
||||
void vMBTCPPortDisable( void );
|
||||
|
||||
BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
||||
|
||||
BOOL xMBTCPPortSendResponse( const UCHAR *pucMBTCPFrame, USHORT usTCPLength );
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
@ -1,83 +0,0 @@
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbproto.h,v 1.14 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_PROTO_H
|
||||
#define _MB_PROTO_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
/* ----------------------- Defines ------------------------------------------*/
|
||||
#define MB_ADDRESS_BROADCAST ( 0 ) /*! Modbus broadcast address. */
|
||||
#define MB_ADDRESS_MIN ( 1 ) /*! Smallest possible slave address. */
|
||||
#define MB_ADDRESS_MAX ( 247 ) /*! Biggest possible slave address. */
|
||||
#define MB_FUNC_NONE ( 0 )
|
||||
#define MB_FUNC_READ_COILS ( 1 )
|
||||
#define MB_FUNC_READ_DISCRETE_INPUTS ( 2 )
|
||||
#define MB_FUNC_WRITE_SINGLE_COIL ( 5 )
|
||||
#define MB_FUNC_WRITE_MULTIPLE_COILS ( 15 )
|
||||
#define MB_FUNC_READ_HOLDING_REGISTER ( 3 )
|
||||
#define MB_FUNC_READ_INPUT_REGISTER ( 4 )
|
||||
#define MB_FUNC_WRITE_REGISTER ( 6 )
|
||||
#define MB_FUNC_WRITE_MULTIPLE_REGISTERS ( 16 )
|
||||
#define MB_FUNC_READWRITE_MULTIPLE_REGISTERS ( 23 )
|
||||
#define MB_FUNC_DIAG_READ_EXCEPTION ( 7 )
|
||||
#define MB_FUNC_DIAG_DIAGNOSTIC ( 8 )
|
||||
#define MB_FUNC_DIAG_GET_COM_EVENT_CNT ( 11 )
|
||||
#define MB_FUNC_DIAG_GET_COM_EVENT_LOG ( 12 )
|
||||
#define MB_FUNC_OTHER_REPORT_SLAVEID ( 17 )
|
||||
#define MB_FUNC_ERROR ( 128 )
|
||||
/* ----------------------- Type definitions ---------------------------------*/
|
||||
typedef enum
|
||||
{
|
||||
MB_EX_NONE = 0x00,
|
||||
MB_EX_ILLEGAL_FUNCTION = 0x01,
|
||||
MB_EX_ILLEGAL_DATA_ADDRESS = 0x02,
|
||||
MB_EX_ILLEGAL_DATA_VALUE = 0x03,
|
||||
MB_EX_SLAVE_DEVICE_FAILURE = 0x04,
|
||||
MB_EX_ACKNOWLEDGE = 0x05,
|
||||
MB_EX_SLAVE_BUSY = 0x06,
|
||||
MB_EX_MEMORY_PARITY_ERROR = 0x08,
|
||||
MB_EX_GATEWAY_PATH_FAILED = 0x0A,
|
||||
MB_EX_GATEWAY_TGT_FAILED = 0x0B
|
||||
} eMBException;
|
||||
|
||||
typedef eMBException( *pxMBFunctionHandler ) ( UCHAR * pucFrame, USHORT * pusLength );
|
||||
|
||||
typedef struct
|
||||
{
|
||||
UCHAR ucFunctionCode;
|
||||
pxMBFunctionHandler pxHandler;
|
||||
} xMBFunctionHandler;
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
@ -1,108 +0,0 @@
|
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* File: $Id: mbutils.h,v 1.5 2006/12/07 22:10:34 wolti Exp $
|
||||
*/
|
||||
|
||||
#ifndef _MB_UTILS_H
|
||||
#define _MB_UTILS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_BEGIN_EXTERN_C
|
||||
#endif
|
||||
/*! \defgroup modbus_utils Utilities
|
||||
*
|
||||
* This module contains some utility functions which can be used by
|
||||
* the application. It includes some special functions for working with
|
||||
* bitfields backed by a character array buffer.
|
||||
*
|
||||
*/
|
||||
/*! \addtogroup modbus_utils
|
||||
* @{
|
||||
*/
|
||||
/*! \brief Function to set bits in a byte buffer.
|
||||
*
|
||||
* This function allows the efficient use of an array to implement bitfields.
|
||||
* The array used for storing the bits must always be a multiple of two
|
||||
* bytes. Up to eight bits can be set or cleared in one operation.
|
||||
*
|
||||
* \param ucByteBuf A buffer where the bit values are stored. Must be a
|
||||
* multiple of 2 bytes. No length checking is performed and if
|
||||
* usBitOffset / 8 is greater than the size of the buffer memory contents
|
||||
* is overwritten.
|
||||
* \param usBitOffset The starting address of the bits to set. The first
|
||||
* bit has the offset 0.
|
||||
* \param ucNBits Number of bits to modify. The value must always be smaller
|
||||
* than 8.
|
||||
* \param ucValues Thew new values for the bits. The value for the first bit
|
||||
* starting at <code>usBitOffset</code> is the LSB of the value
|
||||
* <code>ucValues</code>
|
||||
*
|
||||
* \code
|
||||
* ucBits[2] = {0, 0};
|
||||
*
|
||||
* // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1)
|
||||
* xMBUtilSetBits( ucBits, 4, 1, 1 );
|
||||
*
|
||||
* // Set bit 7 to 1 and bit 8 to 0.
|
||||
* xMBUtilSetBits( ucBits, 7, 2, 0x01 );
|
||||
*
|
||||
* // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A;
|
||||
* xMBUtilSetBits( ucBits, 8, 8, 0x5A);
|
||||
* \endcode
|
||||
*/
|
||||
void xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
|
||||
UCHAR ucNBits, UCHAR ucValues );
|
||||
|
||||
/*! \brief Function to read bits in a byte buffer.
|
||||
*
|
||||
* This function is used to extract up bit values from an array. Up to eight
|
||||
* bit values can be extracted in one step.
|
||||
*
|
||||
* \param ucByteBuf A buffer where the bit values are stored.
|
||||
* \param usBitOffset The starting address of the bits to set. The first
|
||||
* bit has the offset 0.
|
||||
* \param ucNBits Number of bits to modify. The value must always be smaller
|
||||
* than 8.
|
||||
*
|
||||
* \code
|
||||
* UCHAR ucBits[2] = {0, 0};
|
||||
* UCHAR ucResult;
|
||||
*
|
||||
* // Extract the bits 3 - 10.
|
||||
* ucResult = xMBUtilGetBits( ucBits, 3, 8 );
|
||||
* \endcode
|
||||
*/
|
||||
UCHAR xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
|
||||
UCHAR ucNBits );
|
||||
|
||||
/*! @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
PR_END_EXTERN_C
|
||||
#endif
|
||||
#endif
|
Reference in New Issue
Block a user