mbfuncinput_m.c 5.8 KB

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