mbfuncdisc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  42. #define MB_PDU_FUNC_READ_SIZE ( 4 )
  43. #define MB_PDU_FUNC_READ_DISCCNT_MAX ( 0x07D0 )
  44. /* ----------------------- Static functions ---------------------------------*/
  45. eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
  46. /* ----------------------- Start implementation -----------------------------*/
  47. #if MB_FUNC_READ_COILS_ENABLED > 0
  48. eMBException
  49. eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
  50. {
  51. USHORT usRegAddress;
  52. USHORT usDiscreteCnt;
  53. UCHAR ucNBytes;
  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. usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
  63. usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_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( ( usDiscreteCnt >= 1 ) &&
  68. ( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_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_DISCRETE_INPUTS;
  75. *usLen += 1;
  76. /* Test if the quantity of coils is a multiple of 8. If not last
  77. * byte is only partially field with unused coils set to zero. */
  78. if( ( usDiscreteCnt & 0x0007 ) != 0 )
  79. {
  80. ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
  81. }
  82. else
  83. {
  84. ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
  85. }
  86. *pucFrameCur++ = ucNBytes;
  87. *usLen += 1;
  88. eRegStatus =
  89. eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
  90. /* If an error occured convert it into a Modbus exception. */
  91. if( eRegStatus != MB_ENOERR )
  92. {
  93. eStatus = prveMBError2Exception( eRegStatus );
  94. }
  95. else
  96. {
  97. /* The response contains the function code, the starting address
  98. * and the quantity of registers. We reuse the old values in the
  99. * buffer because they are still valid. */
  100. *usLen += ucNBytes;;
  101. }
  102. }
  103. else
  104. {
  105. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  106. }
  107. }
  108. else
  109. {
  110. /* Can't be a valid read coil register request because the length
  111. * is incorrect. */
  112. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  113. }
  114. return eStatus;
  115. }
  116. #endif