ffx_master.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 * 10 * 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. } else {
  53. return false;
  54. }
  55. uint16_t data;
  56. err = nmbs_read_holding_registers(&nmbs, INNER_NUM_FFX_ADDRESS, 1, &data);
  57. if (err == NMBS_ERROR_NONE) {
  58. ffxStatus->inner_num = data;
  59. }
  60. err = nmbs_read_holding_registers(&nmbs, SET_TEMP_FFX_ADDRESS, 1, &data);
  61. if (err == NMBS_ERROR_NONE) {
  62. ffxStatus->set_temp = data;
  63. }
  64. err = nmbs_read_holding_registers(&nmbs, FAN_SPEED_FFX_ADDRESS, 1, &data);
  65. if (err == NMBS_ERROR_NONE) {
  66. ffxStatus->fan_speed = data;
  67. }
  68. err = nmbs_read_holding_registers(&nmbs, HUMP_ON_FFX_ADDRESS, 1, &data);
  69. if (err == NMBS_ERROR_NONE) {
  70. ffxStatus->humidity_on = data;
  71. }
  72. err = nmbs_read_holding_registers(&nmbs, HUMP_OFF_FFX_ADDRESS, 1, &data);
  73. if (err == NMBS_ERROR_NONE) {
  74. ffxStatus->humidity_off = data;
  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. ffx_status_t ffxStatus;
  84. for (int i = 0; i < sizeof(slaves) + 1; i++) {
  85. if (slaves[i] != 0) {
  86. nmbs_set_destination_rtu_address(&nmbs, slaves[i]);
  87. if (get_ffx_status(&ffxStatus) && ffxStatus.power == 1) {
  88. set_reg_value(FRESH_AIR_REG_ADDRESS, ffxStatus.fresh_air_system);
  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.\n");
  96. } else {
  97. error_count++;
  98. if (error_count >= 10) {
  99. set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
  100. }
  101. }
  102. sync_ac_status(&ffxStatus);
  103. osDelay(2000);
  104. }
  105. }
  106. osMutexRelease(ffx_mutex);
  107. }
  108. osDelay(1000);
  109. }
  110. }
  111. // 检测从机是否存在
  112. bool check_slave_exist(uint8_t slave_addr) {
  113. nmbs_set_destination_rtu_address(&nmbs, slave_addr);
  114. uint16_t result;
  115. osDelay(100);
  116. nmbs_error err = nmbs_read_holding_registers(&nmbs, 0x01, 1, &result);
  117. return err == NMBS_ERROR_NONE;
  118. }
  119. // 轮询从站
  120. void search_ffx_slave() {
  121. for (int i = SLAVE_ADDRESS_START; i <= SLAVE_ADDRESS_END; i++) {
  122. ffx_status_t ffxStatusquery;
  123. bool exist = check_slave_exist(i);
  124. if (exist) {
  125. slaves[i] = i;
  126. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  127. }
  128. osDelay(200);
  129. if (exist == NMBS_ERROR_NONE) {
  130. exist = check_slave_exist(i);
  131. if (exist != NMBS_ERROR_NONE) {
  132. slaves[i] = i;
  133. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  134. } else {
  135. set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
  136. ffxStatusquery.inner_num = i;
  137. ffxStatusquery.power = 0;
  138. sync_ac_status(&ffxStatusquery);
  139. osDelay(100);
  140. }
  141. }
  142. osDelay(200);
  143. }
  144. }
  145. void ffx_master_init() {
  146. ffx_mutex = osMutexNew(NULL);
  147. nmbs_platform_conf platformConf;// 配置uart
  148. nmbs_platform_conf_create(&platformConf);
  149. platformConf.transport = NMBS_TRANSPORT_RTU;// RTU
  150. platformConf.read = &uart_read; // 读写函数
  151. platformConf.write = &uart_write; // 读写函数
  152. nmbs_client_create(&nmbs, &platformConf);// 创建客户端
  153. nmbs_set_read_timeout(&nmbs, 400);
  154. nmbs_set_byte_timeout(&nmbs, 400);
  155. // 开启轮询任务
  156. osThreadNew(ffx_poll_task, NULL, NULL);
  157. osThreadNew(ffx_mater_task, NULL, NULL);
  158. }