mb.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * 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. #include "cmsis_os2.h"
  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 {
  78. MB_RTU, /*!< RTU transmission mode. */
  79. MB_ASCII, /*!< ASCII transmission mode. */
  80. MB_TCP /*!< TCP mode. */
  81. } eMBMode;
  82. /* ----------------------- Function prototypes ------------------------------*/
  83. /*! \ingroup modbus
  84. * \brief Initialize the Modbus protocol stack.
  85. *
  86. * This functions initializes the ASCII or RTU module and calls the
  87. * init functions of the porting layer to prepare the hardware. Please
  88. * note that the receiver is still disabled and no Modbus frames are
  89. * processed until eMBEnable( ) has been called.
  90. *
  91. * \param eMode If ASCII or RTU mode should be used.
  92. * \param ucSlaveAddress The slave address. Only frames sent to this
  93. * address or to the broadcast address are processed.
  94. * \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
  95. * is platform dependent and some ports simply choose to ignore it.
  96. * \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
  97. * on the porting layer.
  98. * \param eParity Parity used for serial transmission.
  99. *
  100. * \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
  101. * The protocol is then in the disabled state and ready for activation
  102. * by calling eMBEnable( ). Otherwise one of the following error codes
  103. * is returned:
  104. * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
  105. * slave addresses are in the range 1 - 247.
  106. * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
  107. */
  108. eMBErrorCode eMBInit(eMBRTUSlaveObj *obj);
  109. /*! \ingroup modbus
  110. * \brief Initialize the Modbus protocol stack for Modbus TCP.
  111. *
  112. * This function initializes the Modbus TCP Module. Please note that
  113. * frame processing is still disabled until eMBEnable( ) is called.
  114. *
  115. * \param usTCPPort The TCP port to listen on.
  116. * \return If the protocol stack has been initialized correctly the function
  117. * returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
  118. * codes is returned:
  119. * - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
  120. * slave addresses are in the range 1 - 247.
  121. * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
  122. */
  123. eMBErrorCode eMBTCPInit(USHORT usTCPPort);
  124. /*! \ingroup modbus
  125. * \brief Release resources used by the protocol stack.
  126. *
  127. * This function disables the Modbus protocol stack and release all
  128. * hardware resources. It must only be called when the protocol stack
  129. * is disabled.
  130. *
  131. * \note Note all ports implement this function. A port which wants to
  132. * get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
  133. *
  134. * \return If the resources where released it return eMBErrorCode::MB_ENOERR.
  135. * If the protocol stack is not in the disabled state it returns
  136. * eMBErrorCode::MB_EILLSTATE.
  137. */
  138. eMBErrorCode eMBClose(eMBRTUSlaveObj *obj);
  139. /*! \ingroup modbus
  140. * \brief Enable the Modbus protocol stack.
  141. *
  142. * This function enables processing of Modbus frames. Enabling the protocol
  143. * stack is only possible if it is in the disabled state.
  144. *
  145. * \return If the protocol stack is now in the state enabled it returns
  146. * eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
  147. * return eMBErrorCode::MB_EILLSTATE.
  148. */
  149. eMBErrorCode eMBEnable(eMBRTUSlaveObj *obj);
  150. /*! \ingroup modbus
  151. * \brief Disable the Modbus protocol stack.
  152. *
  153. * This function disables processing of Modbus frames.
  154. *
  155. * \return If the protocol stack has been disabled it returns
  156. * eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
  157. * eMBErrorCode::MB_EILLSTATE.
  158. */
  159. eMBErrorCode eMBDisable(eMBRTUSlaveObj *obj);
  160. /*! \ingroup modbus
  161. * \brief The main pooling loop of the Modbus protocol stack.
  162. *
  163. * This function must be called periodically. The timer interval required
  164. * is given by the application dependent Modbus slave timeout. Internally the
  165. * function calls xMBPortEventGet() and waits for an event from the receiver or
  166. * transmitter state machines.
  167. *
  168. * \return If the protocol stack is not in the enabled state the function
  169. * returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
  170. * eMBErrorCode::MB_ENOERR.
  171. */
  172. eMBErrorCode eMBPoll(eMBRTUSlaveObj *obj);
  173. /*! \ingroup modbus
  174. * \brief Configure the slave id of the device.
  175. *
  176. * This function should be called when the Modbus function <em>Report Slave ID</em>
  177. * is enabled ( By defining MB_FUNC_OTHER_REP_SLAVEID_ENABLED in mbconfig.h ).
  178. *
  179. * \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the
  180. * <em>Report Slave ID</em> response.
  181. * \param xIsRunning If TRUE the <em>Run Indicator Status</em> byte is set to 0xFF.
  182. * otherwise the <em>Run Indicator Status</em> is 0x00.
  183. * \param pucAdditional Values which should be returned in the <em>Additional</em>
  184. * bytes of the <em> Report Slave ID</em> response.
  185. * \param usAdditionalLen Length of the buffer <code>pucAdditonal</code>.
  186. *
  187. * \return If the static buffer defined by MB_FUNC_OTHER_REP_SLAVEID_BUF in
  188. * mbconfig.h is to small it returns eMBErrorCode::MB_ENORES. Otherwise
  189. * it returns eMBErrorCode::MB_ENOERR.
  190. */
  191. eMBErrorCode eMBSetSlaveID(UCHAR ucSlaveID, BOOL xIsRunning,
  192. UCHAR const *pucAdditional,
  193. USHORT usAdditionalLen);
  194. /*! \ingroup modbus
  195. * \brief Registers a callback handler for a given function code.
  196. *
  197. * This function registers a new callback handler for a given function code.
  198. * The callback handler supplied is responsible for interpreting the Modbus PDU and
  199. * the creation of an appropriate response. In case of an error it should return
  200. * one of the possible Modbus exceptions which results in a Modbus exception frame
  201. * sent by the protocol stack.
  202. *
  203. * \param ucFunctionCode The Modbus function code for which this handler should
  204. * be registers. Valid function codes are in the range 1 to 127.
  205. * \param pxHandler The function handler which should be called in case
  206. * such a frame is received. If \c NULL a previously registered function handler
  207. * for this function code is removed.
  208. *
  209. * \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
  210. * more resources are available it returns eMBErrorCode::MB_ENORES. In this
  211. * case the values in mbconfig.h should be adjusted. If the argument was not
  212. * valid it returns eMBErrorCode::MB_EINVAL.
  213. */
  214. eMBErrorCode eMBRegisterCB(UCHAR ucFunctionCode,
  215. pxMBFunctionHandler pxHandler);
  216. /* ----------------------- Callback -----------------------------------------*/
  217. #ifdef __cplusplus
  218. PR_END_EXTERN_C
  219. #endif
  220. #endif