mbfuncinput_m.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
  3. * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  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: mbfuncinput_m.c,v 1.60 2013/10/12 14:23:40 Armink Add Master Functions Exp $
  29. */
  30. /* ----------------------- System includes ----------------------------------*/
  31. #include "stdlib.h"
  32. #include "string.h"
  33. /* ----------------------- Platform includes --------------------------------*/
  34. #include "port.h"
  35. /* ----------------------- Modbus includes ----------------------------------*/
  36. #include "mb_m.h"
  37. #include "mbframe.h"
  38. #include "mbproto.h"
  39. #include "mbconfig.h"
  40. /* ----------------------- Defines ------------------------------------------*/
  41. #define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
  42. #define MB_PDU_REQ_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  43. #define MB_PDU_REQ_READ_SIZE ( 4 )
  44. #define MB_PDU_FUNC_READ_BYTECNT_OFF ( MB_PDU_DATA_OFF + 0 )
  45. #define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
  46. #define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
  47. #define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF ( MB_PDU_DATA_OFF )
  48. /* ----------------------- Static functions ---------------------------------*/
  49. eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
  50. /* ----------------------- Start implementation -----------------------------*/
  51. #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED
  52. #if MB_FUNC_READ_INPUT_ENABLED
  53. /**
  54. * This function will request read input register.
  55. *
  56. * @param ucSndAddr salve address
  57. * @param usRegAddr register start address
  58. * @param usNRegs register total number
  59. * @param lTimeOut timeout (-1 will waiting forever)
  60. *
  61. * @return error code
  62. */
  63. eMBMasterReqErrCode
  64. eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut )
  65. {
  66. UCHAR *ucMBFrame;
  67. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  68. if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
  69. else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
  70. else
  71. {
  72. vMBMasterGetPDUSndBuf(&ucMBFrame);
  73. vMBMasterSetDestAddress(ucSndAddr);
  74. ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_INPUT_REGISTER;
  75. ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
  76. ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
  77. ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
  78. ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
  79. vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
  80. ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT );
  81. eErrStatus = eMBMasterWaitRequestFinish( );
  82. }
  83. return eErrStatus;
  84. }
  85. eMBException
  86. eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
  87. {
  88. UCHAR *ucMBFrame;
  89. USHORT usRegAddress;
  90. USHORT usRegCount;
  91. eMBException eStatus = MB_EX_NONE;
  92. eMBErrorCode eRegStatus;
  93. /* If this request is broadcast, and it's read mode. This request don't need execute. */
  94. if ( xMBMasterRequestIsBroadcast() )
  95. {
  96. eStatus = MB_EX_NONE;
  97. }
  98. else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
  99. {
  100. vMBMasterGetPDUSndBuf(&ucMBFrame);
  101. usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
  102. usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
  103. usRegAddress++;
  104. usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] << 8 );
  105. usRegCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] );
  106. /* Check if the number of registers to read is valid. If not
  107. * return Modbus illegal data value exception.
  108. */
  109. if( ( usRegCount >= 1 ) && ( 2 * usRegCount == pucFrame[MB_PDU_FUNC_READ_BYTECNT_OFF] ) )
  110. {
  111. /* Make callback to fill the buffer. */
  112. eRegStatus = eMBMasterRegInputCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usRegCount );
  113. /* If an error occured convert it into a Modbus exception. */
  114. if( eRegStatus != MB_ENOERR )
  115. {
  116. eStatus = prveMBError2Exception( eRegStatus );
  117. }
  118. }
  119. else
  120. {
  121. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  122. }
  123. }
  124. else
  125. {
  126. /* Can't be a valid request because the length is incorrect. */
  127. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  128. }
  129. return eStatus;
  130. }
  131. #endif
  132. #endif // #if MB_MASTER_RTU_ENABLED || MB_MASTER_ASCII_ENABLED || MB_MASTER_TCP_ENABLED