mb_m.h 18 KB

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