mbport.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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: mbport.h,v 1.17 2006/12/07 22:10:34 wolti Exp $
  29. * mbport.h,v 1.60 2013/08/17 11:42:56 Armink Add Master Functions $
  30. */
  31. #ifndef _MB_PORT_H
  32. #define _MB_PORT_H
  33. #ifdef __cplusplus
  34. PR_BEGIN_EXTERN_C
  35. #endif
  36. /* ----------------------- Defines ------------------------------------------*/
  37. /* ----------------------- Type definitions ---------------------------------*/
  38. typedef enum
  39. {
  40. EV_READY = 1<<0, /*!< Startup finished. */
  41. EV_FRAME_RECEIVED = 1<<1, /*!< Frame received. */
  42. EV_EXECUTE = 1<<2, /*!< Execute function. */
  43. EV_FRAME_SENT = 1<<3 /*!< Frame sent. */
  44. } eMBEventType;
  45. typedef enum
  46. {
  47. EV_MASTER_READY = 1<<0, /*!< Startup finished. */
  48. EV_MASTER_FRAME_RECEIVED = 1<<1, /*!< Frame received. */
  49. EV_MASTER_EXECUTE = 1<<2, /*!< Execute function. */
  50. EV_MASTER_FRAME_SENT = 1<<3, /*!< Frame sent. */
  51. EV_MASTER_ERROR_PROCESS = 1<<4, /*!< Frame error process. */
  52. EV_MASTER_PROCESS_SUCESS = 1<<5, /*!< Request process success. */
  53. EV_MASTER_ERROR_RESPOND_TIMEOUT = 1<<6, /*!< Request respond timeout. */
  54. EV_MASTER_ERROR_RECEIVE_DATA = 1<<7, /*!< Request receive data error. */
  55. EV_MASTER_ERROR_EXECUTE_FUNCTION = 1<<8, /*!< Request execute function error. */
  56. } eMBMasterEventType;
  57. typedef enum
  58. {
  59. EV_ERROR_RESPOND_TIMEOUT, /*!< Slave respond timeout. */
  60. EV_ERROR_RECEIVE_DATA, /*!< Receive frame data erroe. */
  61. EV_ERROR_EXECUTE_FUNCTION, /*!< Execute function error. */
  62. } eMBMasterErrorEventType;
  63. /*! \ingroup modbus
  64. * \brief Parity used for characters in serial mode.
  65. *
  66. * The parity which should be applied to the characters sent over the serial
  67. * link. Please note that this values are actually passed to the porting
  68. * layer and therefore not all parity modes might be available.
  69. */
  70. typedef enum
  71. {
  72. MB_PAR_NONE, /*!< No parity. */
  73. MB_PAR_ODD, /*!< Odd parity. */
  74. MB_PAR_EVEN /*!< Even parity. */
  75. } eMBParity;
  76. /* ----------------------- Supporting functions -----------------------------*/
  77. BOOL xMBPortEventInit( void );
  78. BOOL xMBPortEventPost( eMBEventType eEvent );
  79. BOOL xMBPortEventGet( /*@out@ */ eMBEventType * eEvent );
  80. BOOL xMBMasterPortEventInit( void );
  81. BOOL xMBMasterPortEventPost( eMBMasterEventType eEvent );
  82. BOOL xMBMasterPortEventGet( /*@out@ */ eMBMasterEventType * eEvent );
  83. void vMBMasterOsResInit( void );
  84. BOOL xMBMasterRunResTake( int32_t time );
  85. void vMBMasterRunResRelease( void );
  86. /* ----------------------- Serial port functions ----------------------------*/
  87. BOOL xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
  88. UCHAR ucDataBits, eMBParity eParity );
  89. void vMBPortClose( void );
  90. void xMBPortSerialClose( void );
  91. void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
  92. INLINE BOOL xMBPortSerialGetByte( CHAR * pucByte );
  93. INLINE BOOL xMBPortSerialPutByte( CHAR ucByte );
  94. BOOL xMBMasterPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
  95. UCHAR ucDataBits, eMBParity eParity );
  96. void vMBMasterPortClose( void );
  97. void xMBMasterPortSerialClose( void );
  98. void vMBMasterPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable );
  99. INLINE BOOL xMBMasterPortSerialGetByte( CHAR * pucByte );
  100. INLINE BOOL xMBMasterPortSerialPutByte( CHAR ucByte );
  101. /* ----------------------- Timers functions ---------------------------------*/
  102. BOOL xMBPortTimersInit( USHORT usTimeOut50us );
  103. void xMBPortTimersClose( void );
  104. INLINE void vMBPortTimersEnable( void );
  105. INLINE void vMBPortTimersDisable( void );
  106. BOOL xMBMasterPortTimersInit( USHORT usTimeOut50us );
  107. void xMBMasterPortTimersClose( void );
  108. INLINE void vMBMasterPortTimersT35Enable( void );
  109. INLINE void vMBMasterPortTimersConvertDelayEnable( void );
  110. INLINE void vMBMasterPortTimersRespondTimeoutEnable( void );
  111. INLINE void vMBMasterPortTimersDisable( void );
  112. /* ----------------- Callback for the master error process ------------------*/
  113. void vMBMasterErrorCBRespondTimeout( UCHAR ucDestAddress, const UCHAR* pucPDUData,
  114. USHORT ucPDULength );
  115. void vMBMasterErrorCBReceiveData( UCHAR ucDestAddress, const UCHAR* pucPDUData,
  116. USHORT ucPDULength );
  117. void vMBMasterErrorCBExecuteFunction( UCHAR ucDestAddress, const UCHAR* pucPDUData,
  118. USHORT ucPDULength );
  119. void vMBMasterCBRequestScuuess( void );
  120. /* ----------------------- Callback for the protocol stack ------------------*/
  121. /*!
  122. * \brief Callback function for the porting layer when a new byte is
  123. * available.
  124. *
  125. * Depending upon the mode this callback function is used by the RTU or
  126. * ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
  127. * must immediately return a new character.
  128. *
  129. * \return <code>TRUE</code> if a event was posted to the queue because
  130. * a new byte was received. The port implementation should wake up the
  131. * tasks which are currently blocked on the eventqueue.
  132. */
  133. extern BOOL( *pxMBFrameCBByteReceived ) ( void );
  134. extern BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
  135. extern BOOL( *pxMBPortCBTimerExpired ) ( void );
  136. extern BOOL( *pxMBMasterFrameCBByteReceived ) ( void );
  137. extern BOOL( *pxMBMasterFrameCBTransmitterEmpty ) ( void );
  138. extern BOOL( *pxMBMasterPortCBTimerExpired ) ( void );
  139. /* ----------------------- TCP port functions -------------------------------*/
  140. BOOL xMBTCPPortInit( USHORT usTCPPort );
  141. void vMBTCPPortClose( void );
  142. void vMBTCPPortDisable( void );
  143. BOOL xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
  144. BOOL xMBTCPPortSendResponse( const UCHAR *pucMBTCPFrame, USHORT usTCPLength );
  145. #ifdef __cplusplus
  146. PR_END_EXTERN_C
  147. #endif
  148. #endif