MODBUS_S_LOW_ATY.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @file MODBUS_S_LOW_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of modbus for all device
  20. *
  21. * @note 4 byte per reg, save float or uint32
  22. *
  23. * @version
  24. * - 1_01_221029 > ATY
  25. * -# Preliminary version, first Release
  26. ********************************************************************************
  27. */
  28. #ifndef __MODBUS_S_LOW_ATY_C
  29. #define __MODBUS_S_LOW_ATY_C
  30. #include "MODBUS_S_LOW_ATY.h"
  31. /******************************* For user *************************************/
  32. /******************************************************************************/
  33. __XDATA uint16_t mbRegHodingS[MODBUS_REG_HOLDING_SIZE] = {0};
  34. /**
  35. * @brief modbus process
  36. * @note only make 03/06/10 function code, others will return err 01
  37. */
  38. void Modbus_Process(uint8_t* buf, uint8_t len)
  39. {
  40. uint8_t sendBuf[(MODBUS_REG_HOLDING_SIZE * 2) + 4] = {0x00};
  41. uint16_t sendBufSize = 4;
  42. sendBuf[0] = buf[0];
  43. sendBuf[1] = buf[1];
  44. if(buf[0] != MODBUS_ADDR){ // check addr
  45. if(len < 4){ // check buf min size
  46. sendBuf[1] = buf[1] + 0x80;
  47. sendBuf[2] = 0x02;
  48. sendBufSize = 6;
  49. break;
  50. }
  51. else if(CheckCrc16Modbus(buf, len)){ // check crc
  52. sendBuf[1] = buf[1] + 0x80;
  53. sendBuf[2] = 0x08;
  54. sendBufSize = 6;
  55. break;
  56. }
  57. else{
  58. switch(buf[1]) // check function code
  59. {
  60. // case MODBUS_FUNC_READ_COILS:{break; }
  61. // case MODBUS_FUNC_READ_DISCRETE_INPUTS:{break; }
  62. case MODBUS_FUNC_READ_HOLDING_REGISTERS:{
  63. uint16_t i = 0, startAddr = 0, regCount = 0;
  64. startAddr = ((buf[2] << 8) + buf[3]);
  65. regCount = ((buf[4] << 8) + buf[5]);
  66. if(startAddr + regCount - 1 > MODBUS_REG_HOLDING_SIZE){
  67. sendBuf[1] = buf[1] + 0x80;
  68. sendBuf[2] = 0x02;
  69. sendBufSize = 6;
  70. break;
  71. }
  72. sendBuf[2] = regCount * 4;
  73. sendBufSize += 1;
  74. for(i = 0; i < regCount; i++){
  75. #ifdef BIG_ENDIAN
  76. sendBuf[2 + (i * 2) + 1] = mbRegHodingS[startAddr + i];
  77. sendBuf[2 + (i * 2) + 2] = mbRegHodingS[startAddr + i] >> 8;
  78. #else
  79. sendBuf[2 + (i * 2) + 1] = mbRegHodingS[startAddr + i] >> 8;
  80. sendBuf[2 + (i * 2) + 2] = mbRegHodingS[startAddr + i];
  81. #endif
  82. sendBufSize += 4;
  83. }
  84. break;
  85. }
  86. // case MODBUS_FUNC_READ_INPUT_REGISTERS:{break; }
  87. // case MODBUS_FUNC_WRITE_SINGLE_COIL:{break; }
  88. case MODBUS_FUNC_WRITE_SINGLE_HOLDING_REGISTERS:{
  89. uint16_t startAddr = 0;
  90. startAddr = (((uint16_t)buf[2] << 8) + (uint16_t)buf[3]);
  91. if(startAddr >= MODBUS_REG_HOLDING_SIZE){
  92. sendBuf[1] = buf[1] + 0x80;
  93. sendBuf[2] = 0x02;
  94. sendBufSize = 6;
  95. break;
  96. }
  97. #ifdef BIG_ENDIAN
  98. mbRegHodingS[startAddr] = buf[4] + buf[5] << 8;
  99. #else
  100. mbRegHodingS[startAddr] = buf[4] << 8 + buf[5];
  101. #endif
  102. sendBuf[2] = startAddr >> 8;
  103. sendBuf[3] = startAddr;
  104. #ifdef BIG_ENDIAN
  105. sendBuf[4] = mbRegHodingS[startAddr];
  106. sendBuf[5] = mbRegHodingS[startAddr] >> 8;
  107. #else
  108. sendBuf[4] = mbRegHodingS[startAddr] >> 8;
  109. sendBuf[5] = mbRegHodingS[startAddr];
  110. #endif
  111. sendBufSize += 6;
  112. break;
  113. }
  114. case MODBUS_FUNC_WRITE_MULTY_HOLDING_REGISTERS:{
  115. uint16_t i = 0, startAddr = 0, regCount = 0;
  116. startAddr = ((buf[2] << 8) + buf[3]);
  117. regCount = ((buf[4] << 8) + buf[5]);
  118. if(startAddr + regCount - 1 > MODBUS_REG_HOLDING_SIZE){
  119. sendBuf[1] = buf[1] + 0x80;
  120. sendBuf[2] = 0x02;
  121. sendBufSize = 6;
  122. break;
  123. }
  124. for(i = 0; i < regCount; i++)
  125. {
  126. #ifdef BIG_ENDIAN
  127. mbRegHodingS[startAddr + i] = buf[6 + (i * 4) + 1] + buf[6 + (i * 4) + 2] << 8;
  128. #else
  129. mbRegHodingS[startAddr + i] = buf[6 + (i * 4) + 1] << 8 + buf[6 + (i * 4) + 2];
  130. #endif
  131. }
  132. sendBuf[2] = startAddr >> 8;
  133. sendBuf[3] = startAddr;
  134. sendBuf[4] = regCount >> 8;
  135. sendBuf[5] = regCount;
  136. sendBufSize += 4;
  137. break; }
  138. default:{
  139. sendBuf[1] = buf[1] + 0x80;
  140. sendBuf[2] = 0x01;
  141. sendBufSize = 6;
  142. break; }
  143. }
  144. }
  145. sendBuf[sendBufSize - 2] = GenCrc16ModbusHL(sendBuf, sendBufSize - 2, 1);
  146. sendBuf[sendBufSize - 1] = GenCrc16ModbusHL(sendBuf, sendBufSize - 2, 0);
  147. UartSendBytes(sendBuf, sendBufSize);
  148. }
  149. }
  150. #endif /* __MODBUS_S_LOW_ATY_C */
  151. /******************************** End Of File *********************************/