mbfuncinput.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. */
  29. /* ----------------------- System includes ----------------------------------*/
  30. #include "stdlib.h"
  31. #include "string.h"
  32. /* ----------------------- Platform includes --------------------------------*/
  33. #include "port.h"
  34. /* ----------------------- Modbus includes ----------------------------------*/
  35. #include "mb.h"
  36. #include "mbframe.h"
  37. #include "mbproto.h"
  38. #include "mbconfig.h"
  39. /* ----------------------- Defines ------------------------------------------*/
  40. #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
  41. #define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  42. #define MB_PDU_FUNC_READ_SIZE ( 4 )
  43. #define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
  44. #define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF ( MB_PDU_DATA_OFF )
  45. /* ----------------------- Static functions ---------------------------------*/
  46. eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
  47. /* ----------------------- Start implementation -----------------------------*/
  48. #if MB_FUNC_READ_INPUT_ENABLED > 0
  49. eMBException
  50. eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
  51. {
  52. USHORT usRegAddress;
  53. USHORT usRegCount;
  54. UCHAR *pucFrameCur;
  55. eMBException eStatus = MB_EX_NONE;
  56. eMBErrorCode eRegStatus;
  57. if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
  58. {
  59. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
  60. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
  61. usRegAddress++;
  62. usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
  63. usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
  64. /* Check if the number of registers to read is valid. If not
  65. * return Modbus illegal data value exception.
  66. */
  67. if( ( usRegCount >= 1 )
  68. && ( usRegCount < MB_PDU_FUNC_READ_REGCNT_MAX ) )
  69. {
  70. /* Set the current PDU data pointer to the beginning. */
  71. pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
  72. *usLen = MB_PDU_FUNC_OFF;
  73. /* First byte contains the function code. */
  74. *pucFrameCur++ = MB_FUNC_READ_INPUT_REGISTER;
  75. *usLen += 1;
  76. /* Second byte in the response contain the number of bytes. */
  77. *pucFrameCur++ = ( UCHAR )( usRegCount * 2 );
  78. *usLen += 1;
  79. eRegStatus =
  80. eMBRegInputCB( pucFrameCur, usRegAddress, usRegCount );
  81. /* If an error occured convert it into a Modbus exception. */
  82. if( eRegStatus != MB_ENOERR )
  83. {
  84. eStatus = prveMBError2Exception( eRegStatus );
  85. }
  86. else
  87. {
  88. *usLen += usRegCount * 2;
  89. }
  90. }
  91. else
  92. {
  93. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  94. }
  95. }
  96. else
  97. {
  98. /* Can't be a valid read input register request because the length
  99. * is incorrect. */
  100. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  101. }
  102. return eStatus;
  103. }
  104. #endif