mbport.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.19 2010/06/06 13:54:40 wolti Exp $
  29. */
  30. #ifndef _MB_PORT_H
  31. #define _MB_PORT_H
  32. #ifdef __cplusplus
  33. PR_BEGIN_EXTERN_C
  34. #endif
  35. /* ----------------------- Type definitions ---------------------------------*/
  36. typedef enum
  37. {
  38. EV_READY, /*!< Startup finished. */
  39. EV_FRAME_RECEIVED, /*!< Frame received. */
  40. EV_EXECUTE, /*!< Execute function. */
  41. EV_FRAME_SENT /*!< Frame sent. */
  42. } eMBEventType;
  43. /*! \ingroup modbus
  44. * \brief Parity used for characters in serial mode.
  45. *
  46. * The parity which should be applied to the characters sent over the serial
  47. * link. Please note that this values are actually passed to the porting
  48. * layer and therefore not all parity modes might be available.
  49. */
  50. typedef enum
  51. {
  52. MB_PAR_NONE, /*!< No parity. */
  53. MB_PAR_ODD, /*!< Odd parity. */
  54. MB_PAR_EVEN /*!< Even parity. */
  55. } eMBParity;
  56. typedef enum
  57. {
  58. STATE_RX_INIT, /*!< Receiver is in initial state. */
  59. STATE_RX_IDLE, /*!< Receiver is in idle state. */
  60. STATE_RX_RCV, /*!< Frame is beeing received. */
  61. STATE_RX_ERROR /*!< If the frame is invalid. */
  62. } eMBRcvState;
  63. typedef enum
  64. {
  65. STATE_TX_IDLE, /*!< Transmitter is in idle state. */
  66. STATE_TX_XMIT /*!< Transmitter is in transfer state. */
  67. } eMBSndState;
  68. typedef struct _fmodbus_t fmodbus_t;
  69. /* ----------------------- Supporting functions -----------------------------*/
  70. BOOL xMBPortEventInit(fmodbus_t* ctx );
  71. BOOL xMBPortEventPost(fmodbus_t* ctx, eMBEventType eEvent );
  72. BOOL xMBPortEventGet( fmodbus_t* ctx, /*@out@ */ eMBEventType * eEvent );
  73. /* ----------------------- Serial port functions ----------------------------*/
  74. BOOL xMBPortSerialInit(fmodbus_t* ctx );
  75. void vMBPortClose(fmodbus_t* ctx );
  76. void xMBPortSerialClose(fmodbus_t* ctx );
  77. void vMBPortSerialEnable(fmodbus_t* ctx, BOOL xRxEnable, BOOL xTxEnable );
  78. BOOL xMBPortSerialGetByte( fmodbus_t* ctx, CHAR * pucByte );
  79. BOOL xMBPortSerialPutByte(fmodbus_t* ctx, CHAR ucByte );
  80. /* ----------------------- Timers functions ---------------------------------*/
  81. BOOL xMBPortTimersInit(fmodbus_t* ctx, USHORT usTimeOut50us );
  82. void xMBPortTimersClose( fmodbus_t* ctx );
  83. void vMBPortTimersEnable( fmodbus_t* ctx );
  84. void vMBPortTimersDisable( fmodbus_t* ctx );
  85. void vMBPortTimersDelay(fmodbus_t* ctx, USHORT usTimeOutMS );
  86. /* ----------------------- Callback for the protocol stack ------------------*/
  87. /*!
  88. * \brief Callback function for the porting layer when a new byte is
  89. * available.
  90. *
  91. * Depending upon the mode this callback function is used by the RTU or
  92. * ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
  93. * must immediately return a new character.
  94. *
  95. * \return <code>TRUE</code> if a event was posted to the queue because
  96. * a new byte was received. The port implementation should wake up the
  97. * tasks which are currently blocked on the eventqueue.
  98. */
  99. extern BOOL( *pxMBFrameCBByteReceived ) ( void );
  100. extern BOOL( *pxMBFrameCBTransmitterEmpty ) ( void );
  101. extern BOOL( *pxMBPortCBTimerExpired ) ( void );
  102. /* ----------------------- TCP port functions -------------------------------*/
  103. BOOL xMBTCPPortInit(fmodbus_t* ctx, USHORT usTCPPort );
  104. void vMBTCPPortClose(fmodbus_t* ctx );
  105. void vMBTCPPortDisable( fmodbus_t* ctx );
  106. BOOL xMBPortTCPPool(fmodbus_t *ctx);
  107. BOOL xMBTCPPortGetRequest(fmodbus_t *ctx, UCHAR **ppucMBTCPFrame, USHORT *usTCPLength);
  108. BOOL xMBTCPPortSendResponse(fmodbus_t* ctx, const UCHAR *pucMBTCPFrame, USHORT usTCPLength );
  109. #ifdef __cplusplus
  110. PR_END_EXTERN_C
  111. #endif
  112. #endif