|
@@ -77,13 +77,10 @@ typedef enum
|
|
|
MB_MRE_NO_ERR, /*!< no error. */
|
|
|
MB_MRE_NO_REG, /*!< illegal register address. */
|
|
|
MB_MRE_ILL_ARG, /*!< illegal argument. */
|
|
|
- MB_MRE_PORT_ERR, /*!< porting layer error. */
|
|
|
- MB_MRE_NO_RES, /*!< insufficient resources. */
|
|
|
- MB_MRE_IO, /*!< I/O error. */
|
|
|
- MB_MRE_ILL_STATE, /*!< protocol stack in illegal state. */
|
|
|
+ MB_MRE_REV_DATA, /*!< receive data error. */
|
|
|
MB_MRE_TIMEDOUT, /*!< timeout error occurred. */
|
|
|
MB_MRE_MASTER_BUSY, /*!< master is busy now. */
|
|
|
- MB_MRE_SLAVE_EXCE /*!< slave has exception. */
|
|
|
+ MB_MRE_EXE_FUN /*!< execute function error. */
|
|
|
} eMBMasterReqErrCode;
|
|
|
/*! \ingroup modbus
|
|
|
* \brief TimerMode is Master 3 kind of Timer modes.
|
|
@@ -189,31 +186,176 @@ eMBErrorCode eMBMasterDisable( void );
|
|
|
*/
|
|
|
eMBErrorCode eMBMasterPoll( void );
|
|
|
|
|
|
+/*! \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 eMBMasterRegisterCB( UCHAR ucFunctionCode,
|
|
|
+ pxMBFunctionHandler pxHandler );
|
|
|
+
|
|
|
+/* ----------------------- Callback -----------------------------------------*/
|
|
|
+
|
|
|
+/*! \defgroup modbus_master registers Modbus Registers
|
|
|
+ * \code #include "mb_m.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 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 eMBMasterRegInputCB( 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 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 eMBMasterRegHoldingCB( 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 eMBMasterRegCoilsCB( 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 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 eMBMasterRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
|
|
+ USHORT usNDiscrete );
|
|
|
|
|
|
/*! \ingroup modbus
|
|
|
*\brief These Modbus functions are called for user when Modbus run in Master Mode.
|
|
|
*/
|
|
|
eMBMasterReqErrCode
|
|
|
-eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs );
|
|
|
+eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
|
|
|
eMBMasterReqErrCode
|
|
|
-eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData );
|
|
|
+eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut );
|
|
|
eMBMasterReqErrCode
|
|
|
-eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, USHORT * pusDataBuffer );
|
|
|
+eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr,
|
|
|
+ USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut );
|
|
|
eMBMasterReqErrCode
|
|
|
-eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs );
|
|
|
+eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
|
|
|
eMBMasterReqErrCode
|
|
|
eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
|
|
USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
|
|
|
- USHORT usWriteRegAddr, USHORT usNWriteRegs );
|
|
|
+ USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut );
|
|
|
eMBMasterReqErrCode
|
|
|
-eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils );
|
|
|
+eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils, LONG lTimeOut );
|
|
|
eMBMasterReqErrCode
|
|
|
-eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData );
|
|
|
+eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LONG lTimeOut );
|
|
|
eMBMasterReqErrCode
|
|
|
eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
|
|
|
- USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer );
|
|
|
+ USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut );
|
|
|
eMBMasterReqErrCode
|
|
|
-eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn );
|
|
|
+eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn, LONG lTimeOut );
|
|
|
|
|
|
eMBException
|
|
|
eMBMasterFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
|
|
@@ -239,16 +381,18 @@ eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen
|
|
|
/*£¡ \ingroup modbus
|
|
|
*\brief These functions are interface for Modbus Master
|
|
|
*/
|
|
|
-BOOL xMBMasterGetIsBusy( void );
|
|
|
void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame );
|
|
|
UCHAR ucMBMasterGetDestAddress( void );
|
|
|
void vMBMasterSetDestAddress( UCHAR Address );
|
|
|
-void vMBMasterSetIsBusy( BOOL IsBusy );
|
|
|
BOOL xMBMasterGetCBRunInMasterMode( void );
|
|
|
void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode );
|
|
|
-UCHAR ucMBMasterGetPDUSndLength( void );
|
|
|
-void vMBMasterSetPDUSndLength( UCHAR SendPDULength );
|
|
|
+USHORT usMBMasterGetPDUSndLength( void );
|
|
|
+void vMBMasterSetPDUSndLength( USHORT SendPDULength );
|
|
|
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode );
|
|
|
+BOOL xMBMasterRequestIsBroadcast( void );
|
|
|
+eMBMasterErrorEventType eMBMasterGetErrorType( void );
|
|
|
+void vMBMasterSetErrorType( eMBMasterErrorEventType errorType );
|
|
|
+eMBMasterReqErrCode eMBMasterWaitRequestFinish( void );
|
|
|
|
|
|
/* ----------------------- Callback -----------------------------------------*/
|
|
|
|