mb_m.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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_REV_DATA, /*!< receive data error. */
  75. MB_MRE_TIMEDOUT, /*!< timeout error occurred. */
  76. MB_MRE_MASTER_BUSY, /*!< master is busy now. */
  77. MB_MRE_EXE_FUN /*!< execute function error. */
  78. } eMBMasterReqErrCode;
  79. /*! \ingroup modbus
  80. * \brief TimerMode is Master 3 kind of Timer modes.
  81. */
  82. typedef enum
  83. {
  84. MB_TMODE_T35, /*!< Master receive frame T3.5 timeout. */
  85. MB_TMODE_RESPOND_TIMEOUT, /*!< Master wait respond for slave. */
  86. MB_TMODE_CONVERT_DELAY /*!< Master sent broadcast ,then delay sometime.*/
  87. }eMBMasterTimerMode;
  88. /* ----------------------- Function prototypes ------------------------------*/
  89. /*! \ingroup modbus
  90. * \brief Initialize the Modbus Master protocol stack.
  91. *
  92. * This functions initializes the ASCII or RTU module and calls the
  93. * init functions of the porting layer to prepare the hardware. Please
  94. * note that the receiver is still disabled and no Modbus frames are
  95. * processed until eMBMasterEnable( ) has been called.
  96. *
  97. * \param eMode If ASCII or RTU mode should be used.
  98. * \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
  99. * is platform dependent and some ports simply choose to ignore it.
  100. * \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
  101. * on the porting layer.
  102. * \param eParity Parity used for serial transmission.
  103. *
  104. * \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
  105. * The protocol is then in the disabled state and ready for activation
  106. * by calling eMBMasterEnable( ). Otherwise one of the following error codes
  107. * is returned:
  108. * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
  109. */
  110. eMBErrorCode eMBMasterInit( eMBMode eMode, UCHAR ucPort,
  111. ULONG ulBaudRate, eMBParity eParity );
  112. /*! \ingroup modbus
  113. * \brief Initialize the Modbus Master protocol stack for Modbus TCP.
  114. *
  115. * This function initializes the Modbus TCP Module. Please note that
  116. * frame processing is still disabled until eMBEnable( ) is called.
  117. *
  118. * \param usTCPPort The TCP port to listen on.
  119. * \return If the protocol stack has been initialized correctly the function
  120. * returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
  121. * codes is returned:
  122. * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
  123. * slave addresses are in the range 1 - 247.
  124. * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
  125. */
  126. eMBErrorCode eMBMasterTCPInit( USHORT usTCPPort );
  127. /*! \ingroup modbus
  128. * \brief Release resources used by the protocol stack.
  129. *
  130. * This function disables the Modbus Master protocol stack and release all
  131. * hardware resources. It must only be called when the protocol stack
  132. * is disabled.
  133. *
  134. * \note Note all ports implement this function. A port which wants to
  135. * get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
  136. *
  137. * \return If the resources where released it return eMBErrorCode::MB_ENOERR.
  138. * If the protocol stack is not in the disabled state it returns
  139. * eMBErrorCode::MB_EILLSTATE.
  140. */
  141. eMBErrorCode eMBMasterClose( void );
  142. /*! \ingroup modbus
  143. * \brief Enable the Modbus Master protocol stack.
  144. *
  145. * This function enables processing of Modbus Master frames. Enabling the protocol
  146. * stack is only possible if it is in the disabled state.
  147. *
  148. * \return If the protocol stack is now in the state enabled it returns
  149. * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
  150. * return eMBErrorCode::MB_EILLSTATE.
  151. */
  152. eMBErrorCode eMBMasterEnable( void );
  153. /*! \ingroup modbus
  154. * \brief Disable the Modbus Master protocol stack.
  155. *
  156. * This function disables processing of Modbus frames.
  157. *
  158. * \return If the protocol stack has been disabled it returns
  159. * eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
  160. * eMBErrorCode::MB_EILLSTATE.
  161. */
  162. eMBErrorCode eMBMasterDisable( void );
  163. /*! \ingroup modbus
  164. * \brief The main pooling loop of the Modbus Master protocol stack.
  165. *
  166. * This function must be called periodically. The timer interval required
  167. * is given by the application dependent Modbus slave timeout. Internally the
  168. * function calls xMBMasterPortEventGet() and waits for an event from the receiver or
  169. * transmitter state machines.
  170. *
  171. * \return If the protocol stack is not in the enabled state the function
  172. * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
  173. * eMBErrorCode::MB_ENOERR.
  174. */
  175. eMBErrorCode eMBMasterPoll( void );
  176. /*! \ingroup modbus
  177. * \brief Registers a callback handler for a given function code.
  178. *
  179. * This function registers a new callback handler for a given function code.
  180. * The callback handler supplied is responsible for interpreting the Modbus PDU and
  181. * the creation of an appropriate response. In case of an error it should return
  182. * one of the possible Modbus exceptions which results in a Modbus exception frame
  183. * sent by the protocol stack.
  184. *
  185. * \param ucFunctionCode The Modbus function code for which this handler should
  186. * be registers. Valid function codes are in the range 1 to 127.
  187. * \param pxHandler The function handler which should be called in case
  188. * such a frame is received. If \c NULL a previously registered function handler
  189. * for this function code is removed.
  190. *
  191. * \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
  192. * more resources are available it returns eMBErrorCode::MB_ENORES. In this
  193. * case the values in mbconfig.h should be adjusted. If the argument was not
  194. * valid it returns eMBErrorCode::MB_EINVAL.
  195. */
  196. eMBErrorCode eMBMasterRegisterCB( UCHAR ucFunctionCode,
  197. pxMBFunctionHandler pxHandler );
  198. /* ----------------------- Callback -----------------------------------------*/
  199. /*! \defgroup modbus_master registers Modbus Registers
  200. * \code #include "mb_m.h" \endcode
  201. * The protocol stack does not internally allocate any memory for the
  202. * registers. This makes the protocol stack very small and also usable on
  203. * low end targets. In addition the values don't have to be in the memory
  204. * and could for example be stored in a flash.<br>
  205. * Whenever the protocol stack requires a value it calls one of the callback
  206. * function with the register address and the number of registers to read
  207. * as an argument. The application should then read the actual register values
  208. * (for example the ADC voltage) and should store the result in the supplied
  209. * buffer.<br>
  210. * If the protocol stack wants to update a register value because a write
  211. * register function was received a buffer with the new register values is
  212. * passed to the callback function. The function should then use these values
  213. * to update the application register values.
  214. */
  215. /*! \ingroup modbus_registers
  216. * \brief Callback function used if the value of a <em>Input Register</em>
  217. * is required by the protocol stack. The starting register address is given
  218. * by \c usAddress and the last register is given by <tt>usAddress +
  219. * usNRegs - 1</tt>.
  220. *
  221. * \param pucRegBuffer A buffer where the callback function should write
  222. * the current value of the modbus registers to.
  223. * \param usAddress The starting address of the register. Input registers
  224. * are in the range 1 - 65535.
  225. * \param usNRegs Number of registers the callback function must supply.
  226. *
  227. * \return The function must return one of the following error codes:
  228. * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
  229. * Modbus response is sent.
  230. * - eMBErrorCode::MB_ENOREG If the application does not map an coils
  231. * within the requested address range. In this case a
  232. * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
  233. */
  234. eMBErrorCode eMBMasterRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
  235. USHORT usNRegs );
  236. /*! \ingroup modbus_registers
  237. * \brief Callback function used if a <em>Holding Register</em> value is
  238. * read or written by the protocol stack. The starting register address
  239. * is given by \c usAddress and the last register is given by
  240. * <tt>usAddress + usNRegs - 1</tt>.
  241. *
  242. * \param pucRegBuffer If the application registers values should be updated the
  243. * buffer points to the new registers values. If the protocol stack needs
  244. * to now the current values the callback function should write them into
  245. * this buffer.
  246. * \param usAddress The starting address of the register.
  247. * \param usNRegs Number of registers to read or write.
  248. * \param eMode If eMBRegisterMode::MB_REG_WRITE the application register
  249. * values should be updated from the values in the buffer. For example
  250. * this would be the case when the Modbus master has issued an
  251. * <b>WRITE SINGLE REGISTER</b> command.
  252. * If the value eMBRegisterMode::MB_REG_READ the application should copy
  253. * the current values into the buffer \c pucRegBuffer.
  254. *
  255. * \return The function must return one of the following error codes:
  256. * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
  257. * Modbus response is sent.
  258. * - eMBErrorCode::MB_ENOREG If the application does not map an coils
  259. * within the requested address range. In this case a
  260. * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
  261. */
  262. eMBErrorCode eMBMasterRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
  263. USHORT usNRegs, eMBRegisterMode eMode );
  264. /*! \ingroup modbus_registers
  265. * \brief Callback function used if a <em>Coil Register</em> value is
  266. * read or written by the protocol stack. If you are going to use
  267. * this function you might use the functions xMBUtilSetBits( ) and
  268. * xMBUtilGetBits( ) for working with bitfields.
  269. *
  270. * \param pucRegBuffer The bits are packed in bytes where the first coil
  271. * starting at address \c usAddress is stored in the LSB of the
  272. * first byte in the buffer <code>pucRegBuffer</code>.
  273. * If the buffer should be written by the callback function unused
  274. * coil values (I.e. if not a multiple of eight coils is used) should be set
  275. * to zero.
  276. * \param usAddress The first coil number.
  277. * \param usNCoils Number of coil values requested.
  278. * \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
  279. * be updated from the values supplied in the buffer \c pucRegBuffer.
  280. * If eMBRegisterMode::MB_REG_READ the application should store the current
  281. * values in the buffer \c pucRegBuffer.
  282. *
  283. * \return The function must return one of the following error codes:
  284. * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
  285. * Modbus response is sent.
  286. * - eMBErrorCode::MB_ENOREG If the application does not map an coils
  287. * within the requested address range. In this case a
  288. * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
  289. */
  290. eMBErrorCode eMBMasterRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
  291. USHORT usNCoils, eMBRegisterMode eMode );
  292. /*! \ingroup modbus_registers
  293. * \brief Callback function used if a <em>Input Discrete Register</em> value is
  294. * read by the protocol stack.
  295. *
  296. * If you are going to use his function you might use the functions
  297. * xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.
  298. *
  299. * \param pucRegBuffer The buffer should be updated with the current
  300. * coil values. The first discrete input starting at \c usAddress must be
  301. * stored at the LSB of the first byte in the buffer. If the requested number
  302. * is not a multiple of eight the remaining bits should be set to zero.
  303. * \param usAddress The starting address of the first discrete input.
  304. * \param usNDiscrete Number of discrete input values.
  305. * \return The function must return one of the following error codes:
  306. * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
  307. * Modbus response is sent.
  308. * - eMBErrorCode::MB_ENOREG If the application does not map an coils
  309. * within the requested address range. In this case a
  310. * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
  311. */
  312. eMBErrorCode eMBMasterRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
  313. USHORT usNDiscrete );
  314. /*! \ingroup modbus
  315. *\brief These Modbus functions are called for user when Modbus run in Master Mode.
  316. */
  317. eMBMasterReqErrCode
  318. eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
  319. eMBMasterReqErrCode
  320. eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut );
  321. eMBMasterReqErrCode
  322. eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr,
  323. USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut );
  324. eMBMasterReqErrCode
  325. eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
  326. eMBMasterReqErrCode
  327. eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
  328. USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
  329. USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut );
  330. eMBMasterReqErrCode
  331. eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils, LONG lTimeOut );
  332. eMBMasterReqErrCode
  333. eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LONG lTimeOut );
  334. eMBMasterReqErrCode
  335. eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
  336. USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut );
  337. eMBMasterReqErrCode
  338. eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn, LONG lTimeOut );
  339. eMBException
  340. eMBMasterFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
  341. eMBException
  342. eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen );
  343. eMBException
  344. eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
  345. eMBException
  346. eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
  347. eMBException
  348. eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
  349. eMBException
  350. eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen );
  351. eMBException
  352. eMBMasterFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen );
  353. eMBException
  354. eMBMasterFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen );
  355. eMBException
  356. eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
  357. eMBException
  358. eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
  359. /*£¡ \ingroup modbus
  360. *\brief These functions are interface for Modbus Master
  361. */
  362. void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame );
  363. UCHAR ucMBMasterGetDestAddress( void );
  364. void vMBMasterSetDestAddress( UCHAR Address );
  365. BOOL xMBMasterGetCBRunInMasterMode( void );
  366. void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode );
  367. USHORT usMBMasterGetPDUSndLength( void );
  368. void vMBMasterSetPDUSndLength( USHORT SendPDULength );
  369. void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode );
  370. BOOL xMBMasterRequestIsBroadcast( void );
  371. eMBMasterErrorEventType eMBMasterGetErrorType( void );
  372. void vMBMasterSetErrorType( eMBMasterErrorEventType errorType );
  373. eMBMasterReqErrCode eMBMasterWaitRequestFinish( void );
  374. /* ----------------------- Callback -----------------------------------------*/
  375. #ifdef __cplusplus
  376. PR_END_EXTERN_C
  377. #endif
  378. #endif