ffx_master.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. set_reg_value(HUMP_ON_FFX_ADDRESS, ffxStatus.humidity_on);
  90. set_reg_value(HUMP_OFF_FFX_ADDRESS, ffxStatus.humidity_off);
  91. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  92. osDelay(300);
  93. is_error_ffx[i] = 0;
  94. error_count = 0;
  95. DEBUG_PRINTF("Read that FFX exists: %d.\r\n", slaves[i]);
  96. if(ffxStatus.fresh_air_system == 2)
  97. fresh_air_flag = true;
  98. } else {
  99. error_count++;
  100. if (error_count >= 10) {
  101. set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
  102. }
  103. }
  104. sync_ac_status(&ffxStatus);
  105. ffxStatus.power = 0;
  106. ffxStatus.inner_num = 0xAA;
  107. ffxStatus.mode = 0;
  108. ffxStatus.fan_speed = 0;
  109. osDelay(2000);
  110. }
  111. }
  112. if(fresh_air_flag == true)
  113. set_reg_value(FRESH_AIR_REG_ADDRESS, 2);
  114. else
  115. set_reg_value(FRESH_AIR_REG_ADDRESS, 1);
  116. osMutexRelease(ffx_mutex);
  117. }
  118. osDelay(1000);
  119. }
  120. }
  121. // 检测从机是否存在
  122. bool check_slave_exist(uint8_t slave_addr) {
  123. nmbs_set_destination_rtu_address(&nmbs, slave_addr);
  124. uint16_t result;
  125. osDelay(100);
  126. nmbs_error err = nmbs_read_holding_registers(&nmbs, 0x01, 1, &result);
  127. return err == NMBS_ERROR_NONE;
  128. }
  129. // 轮询从站
  130. void search_ffx_slave() {
  131. for (int i = SLAVE_ADDRESS_START; i <= SLAVE_ADDRESS_END; i++) {
  132. ffx_status_t ffxStatusquery;
  133. bool exist = check_slave_exist(i);
  134. if (exist) {
  135. slaves[i] = i;
  136. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  137. }
  138. osDelay(200);
  139. if (exist == NMBS_ERROR_NONE) {
  140. exist = check_slave_exist(i);
  141. if (exist != NMBS_ERROR_NONE) {
  142. slaves[i] = i;
  143. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  144. } else {
  145. set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
  146. // ffxStatusquery.inner_num = i;
  147. // ffxStatusquery.power = 0;
  148. // sync_ac_status(&ffxStatusquery);
  149. // osDelay(100);
  150. }
  151. }
  152. osDelay(200);
  153. }
  154. }
  155. void ffx_master_init() {
  156. ffx_mutex = osMutexNew(NULL);
  157. nmbs_platform_conf platformConf;// 配置uart
  158. nmbs_platform_conf_create(&platformConf);
  159. platformConf.transport = NMBS_TRANSPORT_RTU;// RTU
  160. platformConf.read = &uart_read; // 读写函数
  161. platformConf.write = &uart_write; // 读写函数
  162. nmbs_client_create(&nmbs, &platformConf);// 创建客户端
  163. nmbs_set_read_timeout(&nmbs, 400);
  164. nmbs_set_byte_timeout(&nmbs, 400);
  165. // 开启轮询任务
  166. osThreadNew(ffx_poll_task, NULL, NULL);
  167. osThreadNew(ffx_mater_task, NULL, NULL);
  168. }