mb_m.h 18 KB

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