424 lines
18 KiB
C
424 lines
18 KiB
C
#ifndef __MB_H__
|
|
#define __MB_H__
|
|
|
|
|
|
#include <rtthread.h>
|
|
#include <assert.h>
|
|
//#include <inttypes.h>
|
|
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
|
|
/*! \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;
|
|
|
|
typedef enum {
|
|
EV_READY = 1<<0, /*!< Startup finished. */
|
|
EV_FRAME_RECEIVED = 1<<1, /*!< Frame received. */
|
|
EV_EXECUTE = 1<<2, /*!< Execute function. */
|
|
EV_FRAME_SENT = 1<<3 /*!< 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;
|
|
|
|
#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 )
|
|
|
|
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 ) ( rt_uint8_t * pucFrame, rt_uint16_t * pusLength );
|
|
|
|
typedef struct {
|
|
rt_uint8_t ucFunctionCode;
|
|
pxMBFunctionHandler pxHandler;
|
|
} xMBFunctionHandler;
|
|
|
|
|
|
/*! \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(rt_uint8_t ucSlaveAddress, rt_uint8_t ucPort,
|
|
rt_uint32_t ulBaudRate, eMBParity eParity );
|
|
|
|
/*! \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( rt_uint8_t ucSlaveID, rt_uint8_t xIsRunning,
|
|
rt_uint8_t const *pucAdditional, rt_uint16_t 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( rt_uint8_t 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( rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress,
|
|
rt_uint16_t 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( rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress,
|
|
rt_uint16_t 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( rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress,
|
|
rt_uint16_t 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( rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress,
|
|
rt_uint16_t usNDiscrete );
|
|
|
|
/*!
|
|
* 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>
|
|
*/
|
|
|
|
#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. */
|
|
|
|
typedef void ( *pvMBFrameStart ) ( void );
|
|
|
|
typedef void ( *pvMBFrameStop ) ( void );
|
|
|
|
typedef eMBErrorCode( *peMBFrameReceive ) ( rt_uint8_t * pucRcvAddress,
|
|
rt_uint8_t ** pucFrame, rt_uint16_t * pusLength );
|
|
|
|
typedef eMBErrorCode( *peMBFrameSend ) ( rt_uint8_t slaveAddress,
|
|
const rt_uint8_t * pucFrame, rt_uint16_t usLength );
|
|
|
|
typedef void( *pvMBFrameClose ) ( void );
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
|