mb_m.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
  3. * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * File: $Id: mb_m.h,v 1.60 2013/09/03 10:20:05 Armink Add Master Functions $
  29. */
  30. #ifndef _MB_M_H
  31. #define _MB_M_H
  32. #ifdef __cplusplus
  33. PR_BEGIN_EXTERN_C
  34. #endif
  35. /*! \defgroup modbus Modbus
  36. * \code #include "mb_m.h" \endcode
  37. *
  38. * This module defines the interface for the application. It contains
  39. * the basic functions and types required to use the Modbus Master protocol stack.
  40. * A typical application will want to call eMBMasterInit() first. If the device
  41. * is ready to answer network requests it must then call eMBEnable() to activate
  42. * the protocol stack. In the main loop the function eMBMasterPoll() must be called
  43. * periodically. The time interval between pooling depends on the configured
  44. * Modbus timeout. If an RTOS is available a separate task should be created
  45. * and the task should always call the function eMBMasterPoll().
  46. *
  47. * \code
  48. * // Initialize protocol stack in RTU mode for a Master
  49. * eMBMasterInit( MB_RTU, 38400, MB_PAR_EVEN );
  50. * // Enable the Modbus Protocol Stack.
  51. * eMBMasterEnable( );
  52. * for( ;; )
  53. * {
  54. * // Call the main polling loop of the Modbus Master protocol stack.
  55. * eMBMasterPoll( );
  56. * ...
  57. * }
  58. * \endcode
  59. */
  60. /* ----------------------- Defines ------------------------------------------*/
  61. /*! \ingroup modbus
  62. * \brief Use the default Modbus Master TCP port (502)
  63. */
  64. #define MB_MASTER_TCP_PORT_USE_DEFAULT 0
  65. /* ----------------------- Type definitions ---------------------------------*/
  66. /*! \ingroup modbus
  67. * \brief Errorcodes used by all function in the Master request.
  68. */
  69. typedef enum
  70. {
  71. MB_MRE_NO_ERR, /*!< no error. */
  72. MB_MRE_NO_REG, /*!< illegal register address. */
  73. MB_MRE_ILL_ARG, /*!< illegal argument. */
  74. MB_MRE_PORT_ERR, /*!< porting layer error. */
  75. MB_MRE_NO_RES, /*!< insufficient resources. */
  76. MB_MRE_IO, /*!< I/O error. */
  77. MB_MRE_ILL_STATE, /*!< protocol stack in illegal state. */
  78. MB_MRE_TIMEDOUT, /*!< timeout error occurred. */
  79. MB_MRE_MASTER_BUSY, /*!< master is busy now. */
  80. MB_MRE_SLAVE_EXCE /*!< slave has exception. */
  81. } eMBMasterReqErrCode;
  82. /*! \ingroup modbus
  83. * \brief TimerMode is Master 3 kind of Timer modes.
  84. */
  85. typedef enum
  86. {
  87. MB_TMODE_T35, /*!< Master receive frame T3.5 timeout. */
  88. MB_TMODE_RESPOND_TIMEOUT, /*!< Master wait respond for slave. */
  89. MB_TMODE_CONVERT_DELAY /*!< Master sent broadcast ,then delay sometime.*/
  90. }eMBMasterTimerMode;
  91. /* ----------------------- Function prototypes ------------------------------*/
  92. /*! \ingroup modbus
  93. * \brief Initialize the Modbus Master protocol stack.
  94. *
  95. * This functions initializes the ASCII or RTU module and calls the
  96. * init functions of the porting layer to prepare the hardware. Please
  97. * note that the receiver is still disabled and no Modbus frames are
  98. * processed until eMBMasterEnable( ) has been called.
  99. *
  100. * \param eMode If ASCII or RTU mode should be used.
  101. * \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
  102. * is platform dependent and some ports simply choose to ignore it.
  103. * \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
  104. * on the porting layer.
  105. * \param eParity Parity used for serial transmission.
  106. *
  107. * \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
  108. * The protocol is then in the disabled state and ready for activation
  109. * by calling eMBMasterEnable( ). Otherwise one of the following error codes
  110. * is returned:
  111. * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
  112. */
  113. eMBErrorCode eMBMasterInit( eMBMode eMode, UCHAR ucPort,
  114. ULONG ulBaudRate, eMBParity eParity );
  115. /*! \ingroup modbus
  116. * \brief Initialize the Modbus Master protocol stack for Modbus TCP.
  117. *
  118. * This function initializes the Modbus TCP Module. Please note that
  119. * frame processing is still disabled until eMBEnable( ) is called.
  120. *
  121. * \param usTCPPort The TCP port to listen on.
  122. * \return If the protocol stack has been initialized correctly the function
  123. * returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
  124. * codes is returned:
  125. * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
  126. * slave addresses are in the range 1 - 247.
  127. * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
  128. */
  129. eMBErrorCode eMBMasterTCPInit( USHORT usTCPPort );
  130. /*! \ingroup modbus
  131. * \brief Release resources used by the protocol stack.
  132. *
  133. * This function disables the Modbus Master protocol stack and release all
  134. * hardware resources. It must only be called when the protocol stack
  135. * is disabled.
  136. *
  137. * \note Note all ports implement this function. A port which wants to
  138. * get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
  139. *
  140. * \return If the resources where released it return eMBErrorCode::MB_ENOERR.
  141. * If the protocol stack is not in the disabled state it returns
  142. * eMBErrorCode::MB_EILLSTATE.
  143. */
  144. eMBErrorCode eMBMasterClose( void );
  145. /*! \ingroup modbus
  146. * \brief Enable the Modbus Master protocol stack.
  147. *
  148. * This function enables processing of Modbus Master frames. Enabling the protocol
  149. * stack is only possible if it is in the disabled state.
  150. *
  151. * \return If the protocol stack is now in the state enabled it returns
  152. * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
  153. * return eMBErrorCode::MB_EILLSTATE.
  154. */
  155. eMBErrorCode eMBMasterEnable( void );
  156. /*! \ingroup modbus
  157. * \brief Disable the Modbus Master protocol stack.
  158. *
  159. * This function disables processing of Modbus frames.
  160. *
  161. * \return If the protocol stack has been disabled it returns
  162. * eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
  163. * eMBErrorCode::MB_EILLSTATE.
  164. */
  165. eMBErrorCode eMBMasterDisable( void );
  166. /*! \ingroup modbus
  167. * \brief The main pooling loop of the Modbus Master protocol stack.
  168. *
  169. * This function must be called periodically. The timer interval required
  170. * is given by the application dependent Modbus slave timeout. Internally the
  171. * function calls xMBMasterPortEventGet() and waits for an event from the receiver or
  172. * transmitter state machines.
  173. *
  174. * \return If the protocol stack is not in the enabled state the function
  175. * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
  176. * eMBErrorCode::MB_ENOERR.
  177. */
  178. eMBErrorCode eMBMasterPoll( void );
  179. /*! \ingroup modbus
  180. *\brief These Modbus functions are called for user when Modbus run in Master Mode.
  181. */
  182. eMBMasterReqErrCode
  183. eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs );
  184. eMBMasterReqErrCode
  185. eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData );
  186. eMBMasterReqErrCode
  187. eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, USHORT * pusDataBuffer );
  188. eMBMasterReqErrCode
  189. eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs );
  190. eMBMasterReqErrCode
  191. eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
  192. USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
  193. USHORT usWriteRegAddr, USHORT usNWriteRegs );
  194. eMBMasterReqErrCode
  195. eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils );
  196. eMBMasterReqErrCode
  197. eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData );
  198. eMBMasterReqErrCode
  199. eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
  200. USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer );
  201. eMBMasterReqErrCode
  202. eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn );
  203. eMBException
  204. eMBMasterFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
  205. eMBException
  206. eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen );
  207. eMBException
  208. eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
  209. eMBException
  210. eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
  211. eMBException
  212. eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
  213. eMBException
  214. eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen );
  215. eMBException
  216. eMBMasterFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen );
  217. eMBException
  218. eMBMasterFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen );
  219. eMBException
  220. eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
  221. eMBException
  222. eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
  223. /*£¡ \ingroup modbus
  224. *\brief These functions are interface for Modbus Master
  225. */
  226. BOOL xMBMasterGetIsBusy( void );
  227. void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame );
  228. UCHAR ucMBMasterGetDestAddress( void );
  229. void vMBMasterSetDestAddress( UCHAR Address );
  230. void vMBMasterSetIsBusy( BOOL IsBusy );
  231. BOOL xMBMasterGetCBRunInMasterMode( void );
  232. void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode );
  233. UCHAR ucMBMasterGetPDUSndLength( void );
  234. void vMBMasterSetPDUSndLength( UCHAR SendPDULength );
  235. void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode );
  236. /* ----------------------- Callback -----------------------------------------*/
  237. #ifdef __cplusplus
  238. PR_END_EXTERN_C
  239. #endif
  240. #endif