mbfuncinput_m.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: mbfuncinput_m.c,v 1.60 2013/10/12 14:23:40 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_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
  44. #define MB_PDU_REQ_READ_SIZE ( 4 )
  45. #define MB_PDU_FUNC_READ_BYTECNT_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. #define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF ( MB_PDU_DATA_OFF )
  49. /* ----------------------- Static functions ---------------------------------*/
  50. eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
  51. /* ----------------------- Start implementation -----------------------------*/
  52. #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
  53. #if MB_FUNC_READ_INPUT_ENABLED > 0
  54. eMBMasterReqErrCode
  55. eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs )
  56. {
  57. UCHAR *ucMBFrame;
  58. eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
  59. if ( xMBMasterGetIsBusy() ) eErrStatus = MB_MRE_MASTER_BUSY;
  60. else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
  61. else
  62. {
  63. vMBMasterGetPDUSndBuf(&ucMBFrame);
  64. vMBMasterSetDestAddress(ucSndAddr);
  65. ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_INPUT_REGISTER;
  66. ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
  67. ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
  68. ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
  69. ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
  70. vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
  71. ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
  72. }
  73. return eErrStatus;
  74. }
  75. eMBException
  76. eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
  77. {
  78. UCHAR *ucMBFrame;
  79. USHORT usRegAddress;
  80. USHORT usRegCount;
  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. usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] << 8 );
  90. usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] );
  91. /* Check if the number of registers to read is valid. If not
  92. * return Modbus illegal data value exception.
  93. */
  94. if( ( usRegCount >= 1 ) && ( 2 * usRegCount == pucFrame[MB_PDU_FUNC_READ_BYTECNT_OFF] ) )
  95. {
  96. /* Make callback to fill the buffer. */
  97. eRegStatus = eMBRegInputCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usRegCount );
  98. /* If an error occured convert it into a Modbus exception. */
  99. if( eRegStatus != MB_ENOERR )
  100. {
  101. eStatus = prveMBError2Exception( eRegStatus );
  102. }
  103. }
  104. else
  105. {
  106. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  107. }
  108. }
  109. else
  110. {
  111. /* Can't be a valid request because the length is incorrect. */
  112. eStatus = MB_EX_ILLEGAL_DATA_VALUE;
  113. }
  114. return eStatus;
  115. }
  116. #endif
  117. #endif