ffx_master.c 7.1 KB

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