mb.h 20 KB

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