ffx_master.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Created by DELL on 2024/10/16.1
  3. //
  4. #include "main.h"
  5. #include "ffx_master.h"
  6. #include "modbus.h"
  7. #include "usart.h"
  8. #include "cmsis_os2.h"
  9. #include "mini_gateway_master.h"
  10. #include "modbus_slave.h"
  11. static nmbs_t nmbs;
  12. uint8_t slaves[SLAVE_ADDRESS_END] = {0}; // 保存已经存在的从站号
  13. static uint8_t index_2 = 0;
  14. static osMutexId_t ffx_mutex;
  15. extern uint8_t error_count;
  16. uint8_t buffer_reg[10]; // 保存读取到的寄存器值
  17. void search_ffx_slave();
  18. uint8_t is_slave_exist = 0;
  19. static int32_t uart_read(uint8_t *buf, uint16_t count, int32_t byte_timeout_ms,
  20. void *arg) {
  21. HAL_StatusTypeDef status = HAL_UART_Receive(&huart2, buf, count, byte_timeout_ms);
  22. if (status == HAL_OK) {
  23. return count;
  24. } else {
  25. return 0;
  26. }
  27. }
  28. static int32_t uart_write(const uint8_t *buf, uint16_t count, int32_t byte_timeout_ms,
  29. void *arg) {
  30. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  31. HAL_UART_Transmit(&huart2, buf, count, byte_timeout_ms);
  32. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  33. return count;
  34. }
  35. // task
  36. _Noreturn void ffx_poll_task(void *pv) {
  37. for (;;) {
  38. if (osMutexAcquire(ffx_mutex, osWaitForever) == osOK) {
  39. search_ffx_slave();
  40. osMutexRelease(ffx_mutex);
  41. }
  42. osDelay(1000 * 60 * 1);
  43. }
  44. }
  45. bool get_ffx_status(ffx_status_t *ffxStatus) {
  46. uint16_t ffx_ststus[3] = {0};
  47. nmbs_error err = nmbs_read_holding_registers(&nmbs, POWER_FFX_ADDRESS, 3, ffx_ststus);
  48. if (err == NMBS_ERROR_NONE) {
  49. ffxStatus->power = ffx_ststus[0];
  50. ffxStatus->mode = ffx_ststus[1];
  51. ffxStatus->fresh_air_system = ffx_ststus[2];
  52. }
  53. uint16_t data;
  54. err = nmbs_read_holding_registers(&nmbs, INNER_NUM_FFX_ADDRESS, 1, &data);
  55. if (err == NMBS_ERROR_NONE) {
  56. ffxStatus->inner_num = data;
  57. }
  58. err = nmbs_read_holding_registers(&nmbs, SET_TEMP_FFX_ADDRESS, 1, &data);
  59. if (err == NMBS_ERROR_NONE) {
  60. ffxStatus->set_temp = data;
  61. }
  62. err = nmbs_read_holding_registers(&nmbs, FAN_SPEED_FFX_ADDRESS, 1, &data);
  63. if (err == NMBS_ERROR_NONE) {
  64. ffxStatus->fan_speed = data;
  65. }
  66. err = nmbs_read_holding_registers(&nmbs, HUMP_ON_FFX_ADDRESS, 1, &data);
  67. if (err == NMBS_ERROR_NONE) {
  68. ffxStatus->humidity_on = data;
  69. }
  70. err = nmbs_read_holding_registers(&nmbs, HUMP_OFF_FFX_ADDRESS, 1, &data);
  71. if (err == NMBS_ERROR_NONE) {
  72. ffxStatus->humidity_off = data;
  73. }else {
  74. return false;
  75. }
  76. return true;
  77. }
  78. _Noreturn void ffx_mater_task(void *pv) {
  79. uint8_t error_count = 0;
  80. uint8_t is_error_ffx[8] = {0};
  81. for (;;) {
  82. if (osMutexAcquire(ffx_mutex, osWaitForever) == osOK) {
  83. bool fresh_air_flag = false;
  84. for (int i = 0; i < sizeof(slaves) + 1; i++) {
  85. if (slaves[i] != 0) {
  86. ffx_status_t ffxStatus;
  87. nmbs_set_destination_rtu_address(&nmbs, slaves[i]);
  88. if (get_ffx_status(&ffxStatus) && ffxStatus.power == 1) {
  89. ffxStatus.power = get_reg_value(POWER_SYNC_REG_ADDRESS);
  90. ffxStatus.mode = get_reg_value(MODE_SYNC_REG_ADDRESS);
  91. ffxStatus.set_temp = get_reg_value(SET_TEMP_SYNC_REG_ADDRESS);
  92. ffxStatus.fan_speed = get_reg_value(FAN_SPEED_SYNC_REG_ADDRESS);
  93. set_reg_value(HUMP_ON_FFX_ADDRESS, ffxStatus.humidity_on);
  94. set_reg_value(HUMP_OFF_FFX_ADDRESS, ffxStatus.humidity_off);
  95. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  96. osDelay(300);
  97. is_error_ffx[i] = 0;
  98. error_count = 0;
  99. DEBUG_PRINTF("Read that FFX exists: %d.\r\n", slaves[i]);
  100. if(ffxStatus.fresh_air_system == 2)
  101. fresh_air_flag = true;
  102. } else {
  103. error_count++;
  104. if (error_count >= 10) {
  105. set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
  106. }
  107. }
  108. // get_reg_value(INNER_ERROR2_REG_ADDRESS);
  109. sync_ac_status(&ffxStatus);
  110. ffxStatus.power = 0;
  111. ffxStatus.inner_num = 0xAA;
  112. ffxStatus.mode = 0;
  113. ffxStatus.fan_speed = 0;
  114. osDelay(2000);
  115. }
  116. }
  117. if(fresh_air_flag == true)
  118. set_reg_value(FRESH_AIR_REG_ADDRESS, 2);
  119. else
  120. set_reg_value(FRESH_AIR_REG_ADDRESS, 1);
  121. osMutexRelease(ffx_mutex);
  122. }
  123. osDelay(1000);
  124. }
  125. }
  126. // 检测从机是否存在
  127. bool check_slave_exist(uint8_t slave_addr) {
  128. nmbs_set_destination_rtu_address(&nmbs, slave_addr);
  129. uint16_t result;
  130. osDelay(100);
  131. nmbs_error err = nmbs_read_holding_registers(&nmbs, 0x01, 1, &result);
  132. return err == NMBS_ERROR_NONE;
  133. }
  134. // 轮询从站
  135. void search_ffx_slave() {
  136. for (int i = SLAVE_ADDRESS_START; i <= SLAVE_ADDRESS_END; i++) {
  137. ffx_status_t ffxStatusquery;
  138. bool exist = check_slave_exist(i);
  139. if (exist) {
  140. slaves[i] = i;
  141. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  142. }
  143. osDelay(200);
  144. if (exist == NMBS_ERROR_NONE) {
  145. exist = check_slave_exist(i);
  146. if (exist != NMBS_ERROR_NONE) {
  147. slaves[i] = i;
  148. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  149. } else {
  150. set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
  151. // ffxStatusquery.inner_num = i;
  152. // ffxStatusquery.power = 0;
  153. // sync_ac_status(&ffxStatusquery);
  154. // osDelay(100);
  155. }
  156. }
  157. osDelay(200);
  158. }
  159. }
  160. void ffx_master_init() {
  161. ffx_mutex = osMutexNew(NULL);
  162. nmbs_platform_conf platformConf;// 配置uart
  163. nmbs_platform_conf_create(&platformConf);
  164. platformConf.transport = NMBS_TRANSPORT_RTU;// RTU
  165. platformConf.read = &uart_read; // 读写函数
  166. platformConf.write = &uart_write; // 读写函数
  167. nmbs_client_create(&nmbs, &platformConf);// 创建客户端
  168. nmbs_set_read_timeout(&nmbs, 400);
  169. nmbs_set_byte_timeout(&nmbs, 400);
  170. // 开启轮询任务
  171. osThreadNew(ffx_poll_task, NULL, NULL);
  172. osThreadNew(ffx_mater_task, NULL, NULL);
  173. }