ffx_master.c 6.8 KB

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