mbfuncdisc_m.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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: mbfuncdisc_m.c,v 1.60 2013/10/15 8:48:20 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_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  44. #define MB_PDU_REQ_READ_SIZE ( 4 )
  45. #define MB_PDU_FUNC_READ_DISCCNT_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. /* ----------------------- Static functions ---------------------------------*/
  49. eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
  50. /* ----------------------- Start implementation -----------------------------*/
  51. #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
  52. #if MB_FUNC_READ_COILS_ENABLED > 0
  53. eMBMasterReqErrCode
  54. eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn )
  55. {
  56. UCHAR *ucMBFrame;
  57. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  58. if ( xMBMasterGetIsBusy() ) eErrStatus = MB_MRE_MASTER_BUSY;
  59. else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
  60. else
  61. {
  62. vMBMasterGetPDUSndBuf(&ucMBFrame);
  63. vMBMasterSetDestAddress(ucSndAddr);
  64. ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_DISCRETE_INPUTS;
  65. ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8;
  66. ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr;
  67. ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8;
  68. ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn;
  69. vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
  70. ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
  71. }
  72. return eErrStatus;
  73. }
  74. eMBException
  75. eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
  76. {
  77. USHORT usRegAddress;
  78. USHORT usDiscreteCnt;
  79. UCHAR ucNBytes;
  80. UCHAR *ucMBFrame;
  81. eMBException eStatus = MB_EX_NONE;
  82. eMBErrorCode eRegStatus;
  83. if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
  84. {
  85. vMBMasterGetPDUSndBuf(&ucMBFrame);
  86. usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
  87. usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
  88. usRegAddress++;
  89. usDiscreteCnt = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF] << 8 );
  90. usDiscreteCnt |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] );
  91. /* Test if the quantity of coils is a multiple of 8. If not last
  92. * byte is only partially field with unused coils set to zero. */
  93. if( ( usDiscreteCnt & 0x0007 ) != 0 )
  94. {
  95. ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 );
  96. }
  97. else
  98. {
  99. ucNBytes = ( UCHAR )( usDiscreteCnt / 8 );
  100. }
  101. /* Check if the number of registers to read is valid. If not
  102. * return Modbus illegal data value exception.
  103. */
  104. if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF])
  105. {
  106. /* Make callback to fill the buffer. */
  107. eRegStatus = eMBRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt );
  108. /* If an error occured convert it into a Modbus exception. */
  109. if( eRegStatus != MB_ENOERR )
  110. {
  111. eStatus = prveMBError2Exception( eRegStatus );
  112. }
  113. }
  114. else
  115. {
  116. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  117. }
  118. }
  119. else
  120. {
  121. /* Can't be a valid read coil register request because the length
  122. * is incorrect. */
  123. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  124. }
  125. return eStatus;
  126. }
  127. #endif
  128. #endif