mb.h 19 KB

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