mbmaster.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * File : mbmaster.c
  3. * This file is part of freemodbus in RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-04-04 yi.qiu first version
  13. */
  14. /* ----------------------- Platform includes --------------------------------*/
  15. #include "rtthread.h"
  16. #include "port.h"
  17. /* ----------------------- Modbus includes ----------------------------------*/
  18. #include "mb.h"
  19. #include "mbconfig.h"
  20. #include "mbframe.h"
  21. #include "mbproto.h"
  22. #include "mbfunc.h"
  23. eMBErrorCode eMBMReadHoldingRegisters (UCHAR ucSlaveAddress, USHORT usRegStartAddress,
  24. UBYTE ubNRegs, UBYTE arusBufferOut[])
  25. {
  26. static UCHAR ucMBFrame[5];
  27. eMBErrorCode eStatus = MB_ENOERR;
  28. eMBEventType eEvent;
  29. static UCHAR ucRcvAddress;
  30. static USHORT usLength;
  31. UCHAR *ucRcvFrame;
  32. /* make up request frame */
  33. ucMBFrame[0] = MB_FUNC_READ_HOLDING_REGISTER;
  34. ucMBFrame[1] = (UCHAR)(usRegStartAddress >> 8);
  35. ucMBFrame[2] = (UCHAR)(usRegStartAddress);
  36. ucMBFrame[3] = (UCHAR)(ubNRegs >> 8);
  37. ucMBFrame[4] = (UCHAR)(ubNRegs);
  38. /* send request frame to slave device */
  39. eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
  40. /* wait on receive event */
  41. if( xMBPortEventGet( &eEvent ) == TRUE )
  42. {
  43. eStatus = eMBRTUReceive( &ucRcvAddress, &ucRcvFrame, &usLength );
  44. if( eStatus == MB_ENOERR )
  45. {
  46. /* Check if the frame is for us. If not ignore the frame. */
  47. if( ( ucRcvAddress == ucSlaveAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
  48. {
  49. RT_ASSERT(ucRcvFrame[0] == MB_FUNC_READ_HOLDING_REGISTER);
  50. RT_ASSERT(ucRcvFrame[1] == 2*ubNRegs)
  51. rt_memcpy((UCHAR *)arusBufferOut, &ucRcvFrame[2], 2*ubNRegs);
  52. }
  53. }
  54. }
  55. else eStatus = MB_ETIMEDOUT;
  56. return eStatus;
  57. }
  58. /*! @fn eMBErrorCode eMBMReadCoils (UCHAR ucSlaveAddress, USHORT usCoilStartAddress,
  59. UBYTE ubNCoils, USHORT arusBufferOut[])
  60. ** @brief request coils
  61. ** @details
  62. ** @param ucSlaveAddress slave station address :from 1 to 247(max)
  63. ** @param usCoilStartAddress coils address
  64. ** @param ubNCoils request coils number
  65. ** @param arusBufferOut response packet buf
  66. ** @return eMBErrorCode
  67. ** @author LiJin
  68. ** @date 2010-04-07
  69. ** @note
  70. */
  71. eMBErrorCode eMBMReadCoils (UCHAR ucSlaveAddress, USHORT usCoilStartAddress,
  72. UBYTE ubNCoils, UBYTE arusBufferOut[])
  73. {
  74. static UCHAR ucMBFrame[5];
  75. eMBErrorCode eStatus = MB_ENOERR;
  76. eMBEventType eEvent;
  77. static UCHAR ucRcvAddress;
  78. static USHORT usLength;
  79. UCHAR *ucRcvFrame;
  80. /* make up request frame */
  81. ucMBFrame[0] = MB_FUNC_READ_COILS;
  82. ucMBFrame[1] = (UCHAR)(usCoilStartAddress >> 8);
  83. ucMBFrame[2] = (UCHAR)(usCoilStartAddress);
  84. ucMBFrame[3] = (UCHAR)(ubNCoils >> 8);
  85. ucMBFrame[4] = (UCHAR)(ubNCoils);
  86. /* send request frame to slave device */
  87. eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
  88. /* wait on receive event */
  89. if( xMBPortEventGet( &eEvent ) == TRUE )
  90. {
  91. eStatus = eMBRTUReceive( &ucRcvAddress, &ucRcvFrame, &usLength );
  92. if( eStatus == MB_ENOERR )
  93. {
  94. /* Check if the frame is for us. If not ignore the frame. */
  95. if( ucRcvAddress == ucSlaveAddress )
  96. {
  97. RT_ASSERT(ucRcvFrame[0] == MB_FUNC_READ_COILS);
  98. rt_memcpy((UCHAR *)arusBufferOut, &ucRcvFrame[2], ucRcvFrame[1]);
  99. }
  100. }
  101. }
  102. else eStatus = MB_ETIMEDOUT;
  103. return eStatus;
  104. }