mb.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
  3. * Copyright (c) 2006 Christian Walter <wolti@sil.at>
  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.h,v 1.17 2006/12/07 22:10:34 wolti Exp $
  29. */
  30. #ifndef _MB_H
  31. #define _MB_H
  32. #include "port.h"
  33. #ifdef __cplusplus
  34. PR_BEGIN_EXTERN_C
  35. #endif
  36. #include "mbport.h"
  37. #include "mbproto.h"
  38. /*! \defgroup modbus Modbus
  39. * \code #include "mb.h" \endcode
  40. *
  41. * This module defines the interface for the application. It contains
  42. * the basic functions and types required to use the Modbus protocol stack.
  43. * A typical application will want to call eMBInit() first. If the device
  44. * is ready to answer network requests it must then call eMBEnable() to activate
  45. * the protocol stack. In the main loop the function eMBPoll() must be called
  46. * periodically. The time interval between pooling depends on the configured
  47. * Modbus timeout. If an RTOS is available a separate task should be created
  48. * and the task should always call the function eMBPoll().
  49. *
  50. * \code
  51. * // Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A
  52. * eMBInit( MB_RTU, 0x0A, 38400, MB_PAR_EVEN );
  53. * // Enable the Modbus Protocol Stack.
  54. * eMBEnable( );
  55. * for( ;; )
  56. * {
  57. * // Call the main polling loop of the Modbus protocol stack.
  58. * eMBPoll( );
  59. * ...
  60. * }
  61. * \endcode
  62. */
  63. /* ----------------------- Defines ------------------------------------------*/
  64. /*! \ingroup modbus
  65. * \brief Use the default Modbus TCP port (502)
  66. */
  67. #define MB_TCP_PORT_USE_DEFAULT 0
  68. /* ----------------------- Type definitions ---------------------------------*/
  69. /*! \ingroup modbus
  70. * \brief Modbus serial transmission modes (RTU/ASCII).
  71. *
  72. * Modbus serial supports two transmission modes. Either ASCII or RTU. RTU
  73. * is faster but has more hardware requirements and requires a network with
  74. * a low jitter. ASCII is slower and more reliable on slower links (E.g. modems)
  75. */
  76. typedef enum
  77. {
  78. MB_RTU, /*!< RTU transmission mode. */
  79. MB_ASCII, /*!< ASCII transmission mode. */
  80. MB_TCP /*!< TCP mode. */
  81. } eMBMode;
  82. /*! \ingroup modbus
  83. * \brief If register should be written or read.
  84. *
  85. * This value is passed to the callback functions which support either
  86. * reading or writing register values. Writing means that the application
  87. * registers should be updated and reading means that the modbus protocol
  88. * stack needs to know the current register values.
  89. *
  90. * \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and
  91. * eMBRegInputCB( ).
  92. */
  93. typedef enum
  94. {
  95. MB_REG_READ, /*!< Read register values and pass to protocol stack. */
  96. MB_REG_WRITE /*!< Update register values. */
  97. } eMBRegisterMode;
  98. /*! \ingroup modbus
  99. * \brief Errorcodes used by all function in the protocol stack.
  100. */
  101. typedef enum
  102. {
  103. MB_ENOERR, /*!< no error. */
  104. MB_ENOREG, /*!< illegal register address. */
  105. MB_EINVAL, /*!< illegal argument. */
  106. MB_EPORTERR, /*!< porting layer error. */
  107. MB_ENORES, /*!< insufficient resources. */
  108. MB_EIO, /*!< I/O error. */
  109. MB_EILLSTATE, /*!< protocol stack in illegal state. */
  110. MB_ETIMEDOUT /*!< timeout error occurred. */
  111. } eMBErrorCode;
  112. /* ----------------------- Function prototypes ------------------------------*/
  113. /*! \ingroup modbus
  114. * \brief Initialize the Modbus protocol stack.
  115. *
  116. * This functions initializes the ASCII or RTU module and calls the
  117. * init functions of the porting layer to prepare the hardware. Please
  118. * note that the receiver is still disabled and no Modbus frames are
  119. * processed until eMBEnable( ) has been called.
  120. *
  121. * \param eMode If ASCII or RTU mode should be used.
  122. * \param ucSlaveAddress The slave address. Only frames sent to this
  123. * address or to the broadcast address are processed.
  124. * \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
  125. * is platform dependent and some ports simply choose to ignore it.
  126. * \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
  127. * on the porting layer.
  128. * \param eParity Parity used for serial transmission.
  129. *
  130. * \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
  131. * The protocol is then in the disabled state and ready for activation
  132. * by calling eMBEnable( ). Otherwise one of the following error codes
  133. * is returned:
  134. * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
  135. * slave addresses are in the range 1 - 247.
  136. * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
  137. */
  138. eMBErrorCode eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
  139. UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
  140. /*! \ingroup modbus
  141. * \brief Initialize the Modbus protocol stack for Modbus TCP.
  142. *
  143. * This function initializes the Modbus TCP Module. Please note that
  144. * frame processing is still disabled until eMBEnable( ) is called.
  145. *
  146. * \param usTCPPort The TCP port to listen on.
  147. * \return If the protocol stack has been initialized correctly the function
  148. * returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
  149. * codes is returned:
  150. * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
  151. * slave addresses are in the range 1 - 247.
  152. * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
  153. */
  154. eMBErrorCode eMBTCPInit( USHORT usTCPPort );
  155. /*! \ingroup modbus
  156. * \brief Release resources used by the protocol stack.
  157. *
  158. * This function disables the Modbus protocol stack and release all
  159. * hardware resources. It must only be called when the protocol stack
  160. * is disabled.
  161. *
  162. * \note Note all ports implement this function. A port which wants to
  163. * get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
  164. *
  165. * \return If the resources where released it return eMBErrorCode::MB_ENOERR.
  166. * If the protocol stack is not in the disabled state it returns
  167. * eMBErrorCode::MB_EILLSTATE.
  168. */
  169. eMBErrorCode eMBClose( void );
  170. /*! \ingroup modbus
  171. * \brief Enable the Modbus protocol stack.
  172. *
  173. * This function enables processing of Modbus frames. Enabling the protocol
  174. * stack is only possible if it is in the disabled state.
  175. *
  176. * \return If the protocol stack is now in the state enabled it returns
  177. * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
  178. * return eMBErrorCode::MB_EILLSTATE.
  179. */
  180. eMBErrorCode eMBEnable( void );
  181. /*! \ingroup modbus
  182. * \brief Disable the Modbus protocol stack.
  183. *
  184. * This function disables processing of Modbus frames.
  185. *
  186. * \return If the protocol stack has been disabled it returns
  187. * eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
  188. * eMBErrorCode::MB_EILLSTATE.
  189. */
  190. eMBErrorCode eMBDisable( void );
  191. /*! \ingroup modbus
  192. * \brief The main pooling loop of the Modbus protocol stack.
  193. *
  194. * This function must be called periodically. The timer interval required
  195. * is given by the application dependent Modbus slave timeout. Internally the
  196. * function calls xMBPortEventGet() and waits for an event from the receiver or
  197. * transmitter state machines.
  198. *
  199. * \return If the protocol stack is not in the enabled state the function
  200. * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
  201. * eMBErrorCode::MB_ENOERR.
  202. */
  203. eMBErrorCode eMBPoll( void );
  204. /*! \ingroup modbus
  205. * \brief Configure the slave id of the device.
  206. *
  207. * This function should be called when the Modbus function <em>Report Slave ID</em>
  208. * is enabled ( By defining MB_FUNC_OTHER_REP_SLAVEID_ENABLED in mbconfig.h ).
  209. *
  210. * \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the
  211. * <em>Report Slave ID</em> response.
  212. * \param xIsRunning If TRUE the <em>Run Indicator Status</em> byte is set to 0xFF.
  213. * otherwise the <em>Run Indicator Status</em> is 0x00.
  214. * \param pucAdditional Values which should be returned in the <em>Additional</em>
  215. * bytes of the <em> Report Slave ID</em> response.
  216. * \param usAdditionalLen Length of the buffer <code>pucAdditonal</code>.
  217. *
  218. * \return If the static buffer defined by MB_FUNC_OTHER_REP_SLAVEID_BUF in
  219. * mbconfig.h is to small it returns eMBErrorCode::MB_ENORES. Otherwise
  220. * it returns eMBErrorCode::MB_ENOERR.
  221. */
  222. eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
  223. UCHAR const *pucAdditional,
  224. USHORT usAdditionalLen );
  225. /*! \ingroup modbus
  226. * \brief Registers a callback handler for a given function code.
  227. *
  228. * This function registers a new callback handler for a given function code.
  229. * The callback handler supplied is responsible for interpreting the Modbus PDU and
  230. * the creation of an appropriate response. In case of an error it should return
  231. * one of the possible Modbus exceptions which results in a Modbus exception frame
  232. * sent by the protocol stack.
  233. *
  234. * \param ucFunctionCode The Modbus function code for which this handler should
  235. * be registers. Valid function codes are in the range 1 to 127.
  236. * \param pxHandler The function handler which should be called in case
  237. * such a frame is received. If \c NULL a previously registered function handler
  238. * for this function code is removed.
  239. *
  240. * \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
  241. * more resources are available it returns eMBErrorCode::MB_ENORES. In this
  242. * case the values in mbconfig.h should be adjusted. If the argument was not
  243. * valid it returns eMBErrorCode::MB_EINVAL.
  244. */
  245. eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode,
  246. pxMBFunctionHandler pxHandler );
  247. /* ----------------------- Callback -----------------------------------------*/
  248. /*! \defgroup modbus_registers Modbus Registers
  249. * \code #include "mb.h" \endcode
  250. * The protocol stack does not internally allocate any memory for the
  251. * registers. This makes the protocol stack very small and also usable on
  252. * low end targets. In addition the values don't have to be in the memory
  253. * and could for example be stored in a flash.<br>
  254. * Whenever the protocol stack requires a value it calls one of the callback
  255. * function with the register address and the number of registers to read
  256. * as an argument. The application should then read the actual register values
  257. * (for example the ADC voltage) and should store the result in the supplied
  258. * buffer.<br>
  259. * If the protocol stack wants to update a register value because a write
  260. * register function was received a buffer with the new register values is
  261. * passed to the callback function. The function should then use these values
  262. * to update the application register values.
  263. */
  264. /*! \ingroup modbus_registers
  265. * \brief Callback function used if the value of a <em>Input Register</em>
  266. * is required by the protocol stack. The starting register address is given
  267. * by \c usAddress and the last register is given by <tt>usAddress +
  268. * usNRegs - 1</tt>.
  269. *
  270. * \param pucRegBuffer A buffer where the callback function should write
  271. * the current value of the modbus registers to.
  272. * \param usAddress The starting address of the register. Input registers
  273. * are in the range 1 - 65535.
  274. * \param usNRegs Number of registers the callback function must supply.
  275. *
  276. * \return The function must return one of the following error codes:
  277. * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
  278. * Modbus response is sent.
  279. * - eMBErrorCode::MB_ENOREG If the application can not supply values
  280. * for registers within this range. In this case a
  281. * <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
  282. * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
  283. * currently not available and the application dependent response
  284. * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
  285. * exception is sent as a response.
  286. * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
  287. * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
  288. */
  289. eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
  290. USHORT usNRegs );
  291. /*! \ingroup modbus_registers
  292. * \brief Callback function used if a <em>Holding Register</em> value is
  293. * read or written by the protocol stack. The starting register address
  294. * is given by \c usAddress and the last register is given by
  295. * <tt>usAddress + usNRegs - 1</tt>.
  296. *
  297. * \param pucRegBuffer If the application registers values should be updated the
  298. * buffer points to the new registers values. If the protocol stack needs
  299. * to now the current values the callback function should write them into
  300. * this buffer.
  301. * \param usAddress The starting address of the register.
  302. * \param usNRegs Number of registers to read or write.
  303. * \param eMode If eMBRegisterMode::MB_REG_WRITE the application register
  304. * values should be updated from the values in the buffer. For example
  305. * this would be the case when the Modbus master has issued an
  306. * <b>WRITE SINGLE REGISTER</b> command.
  307. * If the value eMBRegisterMode::MB_REG_READ the application should copy
  308. * the current values into the buffer \c pucRegBuffer.
  309. *
  310. * \return The function must return one of the following error codes:
  311. * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
  312. * Modbus response is sent.
  313. * - eMBErrorCode::MB_ENOREG If the application can not supply values
  314. * for registers within this range. In this case a
  315. * <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
  316. * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
  317. * currently not available and the application dependent response
  318. * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
  319. * exception is sent as a response.
  320. * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
  321. * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
  322. */
  323. eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
  324. USHORT usNRegs, eMBRegisterMode eMode );
  325. /*! \ingroup modbus_registers
  326. * \brief Callback function used if a <em>Coil Register</em> value is
  327. * read or written by the protocol stack. If you are going to use
  328. * this function you might use the functions xMBUtilSetBits( ) and
  329. * xMBUtilGetBits( ) for working with bitfields.
  330. *
  331. * \param pucRegBuffer The bits are packed in bytes where the first coil
  332. * starting at address \c usAddress is stored in the LSB of the
  333. * first byte in the buffer <code>pucRegBuffer</code>.
  334. * If the buffer should be written by the callback function unused
  335. * coil values (I.e. if not a multiple of eight coils is used) should be set
  336. * to zero.
  337. * \param usAddress The first coil number.
  338. * \param usNCoils Number of coil values requested.
  339. * \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
  340. * be updated from the values supplied in the buffer \c pucRegBuffer.
  341. * If eMBRegisterMode::MB_REG_READ the application should store the current
  342. * values in the buffer \c pucRegBuffer.
  343. *
  344. * \return The function must return one of the following error codes:
  345. * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
  346. * Modbus response is sent.
  347. * - eMBErrorCode::MB_ENOREG If the application does not map an coils
  348. * within the requested address range. In this case a
  349. * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
  350. * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
  351. * currently not available and the application dependent response
  352. * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
  353. * exception is sent as a response.
  354. * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
  355. * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
  356. */
  357. eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
  358. USHORT usNCoils, eMBRegisterMode eMode );
  359. /*! \ingroup modbus_registers
  360. * \brief Callback function used if a <em>Input Discrete Register</em> value is
  361. * read by the protocol stack.
  362. *
  363. * If you are going to use his function you might use the functions
  364. * xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.
  365. *
  366. * \param pucRegBuffer The buffer should be updated with the current
  367. * coil values. The first discrete input starting at \c usAddress must be
  368. * stored at the LSB of the first byte in the buffer. If the requested number
  369. * is not a multiple of eight the remaining bits should be set to zero.
  370. * \param usAddress The starting address of the first discrete input.
  371. * \param usNDiscrete Number of discrete input values.
  372. * \return The function must return one of the following error codes:
  373. * - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
  374. * Modbus response is sent.
  375. * - eMBErrorCode::MB_ENOREG If no such discrete inputs exists.
  376. * In this case a <b>ILLEGAL DATA ADDRESS</b> exception frame is sent
  377. * as a response.
  378. * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
  379. * currently not available and the application dependent response
  380. * timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
  381. * exception is sent as a response.
  382. * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
  383. * a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
  384. */
  385. eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
  386. USHORT usNDiscrete );
  387. #ifdef __cplusplus
  388. PR_END_EXTERN_C
  389. #endif
  390. #endif