mbfuncdisc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * FreeRTOS Modbus Libary: A Modbus serial implementation for FreeRTOS
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /* ----------------------- System includes ----------------------------------*/
  20. #include "stdlib.h"
  21. #include "string.h"
  22. /* ----------------------- Platform includes --------------------------------*/
  23. #include "port.h"
  24. /* ----------------------- Modbus includes ----------------------------------*/
  25. #include "mb.h"
  26. #include "mbframe.h"
  27. #include "mbproto.h"
  28. #include "mbconfig.h"
  29. /* ----------------------- Defines ------------------------------------------*/
  30. #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
  31. #define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  32. #define MB_PDU_FUNC_READ_SIZE ( 4 )
  33. #define MB_PDU_FUNC_READ_DISCCNT_MAX ( 0x07D0 )
  34. /* ----------------------- Static functions ---------------------------------*/
  35. eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
  36. /* ----------------------- Start implementation -----------------------------*/
  37. #if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED || MB_TCP_ENABLED
  38. #if MB_FUNC_READ_COILS_ENABLED
  39. eMBException
  40. eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
  41. {
  42. USHORT usRegAddress;
  43. USHORT usDiscreteCnt;
  44. UCHAR ucNBytes;
  45. UCHAR *pucFrameCur;
  46. eMBException eStatus = MB_EX_NONE;
  47. eMBErrorCode eRegStatus;
  48. if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
  49. {
  50. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
  51. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
  52. usRegAddress++;
  53. usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
  54. usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
  55. /* Check if the number of registers to read is valid. If not
  56. * return Modbus illegal data value exception.
  57. */
  58. if( ( usDiscreteCnt >= 1 ) &&
  59. ( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) )
  60. {
  61. /* Set the current PDU data pointer to the beginning. */
  62. pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
  63. *usLen = MB_PDU_FUNC_OFF;
  64. /* First byte contains the function code. */
  65. *pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
  66. *usLen += 1;
  67. /* Test if the quantity of coils is a multiple of 8. If not last
  68. * byte is only partially field with unused coils set to zero. */
  69. if( ( usDiscreteCnt & 0x0007 ) != 0 )
  70. {
  71. ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
  72. }
  73. else
  74. {
  75. ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
  76. }
  77. *pucFrameCur++ = ucNBytes;
  78. *usLen += 1;
  79. eRegStatus =
  80. eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
  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. /* The response contains the function code, the starting address
  89. * and the quantity of registers. We reuse the old values in the
  90. * buffer because they are still valid. */
  91. *usLen += ucNBytes;;
  92. }
  93. }
  94. else
  95. {
  96. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  97. }
  98. }
  99. else
  100. {
  101. /* Can't be a valid read coil register request because the length
  102. * is incorrect. */
  103. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  104. }
  105. return eStatus;
  106. }
  107. #endif
  108. #endif