mbmaster.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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, USHORT arusBufferOut[])
  25. {
  26. static UCHAR ucMBFrame[5];
  27. eMBErrorCode eStatus = MB_ENOERR;
  28. eMBEventType eEvent;
  29. static UCHAR ucRcvAddress;
  30. static USHORT usLength;
  31. /* make up request frame */
  32. ucMBFrame[0] = MB_FUNC_READ_HOLDING_REGISTER;
  33. ucMBFrame[1] = (UCHAR)(usRegStartAddress >> 8);
  34. ucMBFrame[2] = (UCHAR)(usRegStartAddress);
  35. ucMBFrame[3] = (UCHAR)(ubNRegs >> 8);
  36. ucMBFrame[4] = (UCHAR)(ubNRegs);
  37. rt_kprintf("send frame [%x%x%x%x%x]\n",
  38. ucMBFrame[0], ucMBFrame[1], ucMBFrame[2], ucMBFrame[3], ucMBFrame[4]);
  39. /* send request frame to slave device */
  40. eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
  41. /* wait on receive event */
  42. if( xMBPortEventGet( &eEvent ) == TRUE )
  43. {
  44. eStatus = eMBRTUReceive( &ucRcvAddress, &ucMBFrame, &usLength );
  45. if( eStatus == MB_ENOERR )
  46. {
  47. /* Check if the frame is for us. If not ignore the frame. */
  48. if( ( ucRcvAddress == ucSlaveAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
  49. {
  50. /* parse and restore data */
  51. rt_kprintf("parse and restore date here\n");
  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, USHORT arusBufferOut[])
  73. {
  74. static UCHAR ucMBFrame[5];
  75. eMBErrorCode eStatus = MB_ENOERR;
  76. eMBEventType eEvent;
  77. static UCHAR ucRcvAddress;
  78. static USHORT usLength;
  79. /* make up request frame */
  80. ucMBFrame[0] = MB_FUNC_READ_COILS;
  81. ucMBFrame[1] = (UCHAR)(usCoilStartAddress >> 8);
  82. ucMBFrame[2] = (UCHAR)(usCoilStartAddress);
  83. ucMBFrame[3] = (UCHAR)(ubNCoils >> 8);
  84. ucMBFrame[4] = (UCHAR)(ubNCoils);
  85. rt_kprintf("send frame [%x%x%x%x%x]\n",
  86. ucMBFrame[0], ucMBFrame[1], ucMBFrame[2], ucMBFrame[3], ucMBFrame[4]);
  87. /* send request frame to slave device */
  88. eStatus = eMBRTUSend( ucSlaveAddress, ucMBFrame, 5 );
  89. /* wait on receive event */
  90. if( xMBPortEventGet( &eEvent ) == TRUE )
  91. {
  92. eStatus = eMBRTUReceive( &ucRcvAddress, &ucMBFrame, &usLength );
  93. if( eStatus == MB_ENOERR )
  94. {
  95. /* Check if the frame is for us. If not ignore the frame. */
  96. if( ( ucRcvAddress == ucSlaveAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
  97. {
  98. /* parse and restore data */
  99. rt_kprintf("parse and restore date here\n");
  100. }
  101. }
  102. }
  103. else eStatus = MB_ETIMEDOUT;
  104. return eStatus;
  105. }