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_FUNC_READ_COILS_ENABLED > 0
  38. eMBException
  39. eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
  40. {
  41. USHORT usRegAddress;
  42. USHORT usDiscreteCnt;
  43. UCHAR ucNBytes;
  44. UCHAR *pucFrameCur;
  45. eMBException eStatus = MB_EX_NONE;
  46. eMBErrorCode eRegStatus;
  47. if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
  48. {
  49. usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
  50. usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
  51. usRegAddress++;
  52. usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
  53. usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
  54. /* Check if the number of registers to read is valid. If not
  55. * return Modbus illegal data value exception.
  56. */
  57. if( ( usDiscreteCnt >= 1 ) &&
  58. ( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) )
  59. {
  60. /* Set the current PDU data pointer to the beginning. */
  61. pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
  62. *usLen = MB_PDU_FUNC_OFF;
  63. /* First byte contains the function code. */
  64. *pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
  65. *usLen += 1;
  66. /* Test if the quantity of coils is a multiple of 8. If not last
  67. * byte is only partially field with unused coils set to zero. */
  68. if( ( usDiscreteCnt & 0x0007 ) != 0 )
  69. {
  70. ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
  71. }
  72. else
  73. {
  74. ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
  75. }
  76. *pucFrameCur++ = ucNBytes;
  77. *usLen += 1;
  78. eRegStatus =
  79. eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
  80. /* If an error occured convert it into a Modbus exception. */
  81. if( eRegStatus != MB_ENOERR )
  82. {
  83. eStatus = prveMBError2Exception( eRegStatus );
  84. }
  85. else
  86. {
  87. /* The response contains the function code, the starting address
  88. * and the quantity of registers. We reuse the old values in the
  89. * buffer because they are still valid. */
  90. *usLen += ucNBytes;;
  91. }
  92. }
  93. else
  94. {
  95. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  96. }
  97. }
  98. else
  99. {
  100. /* Can't be a valid read coil register request because the length
  101. * is incorrect. */
  102. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  103. }
  104. return eStatus;
  105. }
  106. #endif