ffx_master.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // Created by DELL on 2024/10/16.
  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 * 10);
  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, 2, ffx_ststus);
  48. if (err == NMBS_ERROR_NONE) {
  49. ffxStatus->power = ffx_ststus[0];
  50. ffxStatus->mode = ffx_ststus[1];
  51. } else {
  52. return false;
  53. }
  54. uint16_t data;
  55. err = nmbs_read_holding_registers(&nmbs, INNER_NUM_FFX_ADDRESS, 1, &data);
  56. if (err == NMBS_ERROR_NONE) {
  57. ffxStatus->inner_num = data;
  58. }
  59. err = nmbs_read_holding_registers(&nmbs, SET_TEMP_FFX_ADDRESS, 1, &data);
  60. if (err == NMBS_ERROR_NONE) {
  61. ffxStatus->set_temp = data;
  62. }
  63. err = nmbs_read_holding_registers(&nmbs, FAN_SPEED_FFX_ADDRESS, 1, &data);
  64. if (err == NMBS_ERROR_NONE) {
  65. ffxStatus->fan_speed = data;
  66. }
  67. err = nmbs_read_holding_registers(&nmbs, HUMP_ON_FFX_ADDRESS, 1, &data);
  68. if (err == NMBS_ERROR_NONE) {
  69. ffxStatus->humidity_on = data;
  70. }
  71. err = nmbs_read_holding_registers(&nmbs, HUMP_OFF_FFX_ADDRESS, 1, &data);
  72. if (err == NMBS_ERROR_NONE) {
  73. ffxStatus->humidity_off = data;
  74. }
  75. return true;
  76. }
  77. _Noreturn void ffx_mater_task(void *pv) {
  78. uint8_t error_count = 0;
  79. uint8_t is_error_ffx[8] = {0};
  80. for (;;) {
  81. if (osMutexAcquire(ffx_mutex, osWaitForever) == osOK) {
  82. ffx_status_t ffxStatus;
  83. for (int i = 0; i < sizeof(slaves) + 1; i++) {
  84. if (slaves[i] != 0) {
  85. nmbs_set_destination_rtu_address(&nmbs, slaves[i]);
  86. if (get_ffx_status(&ffxStatus)) {
  87. set_reg_value(HUMP_ON_FFX_ADDRESS, ffxStatus.humidity_on);
  88. set_reg_value(HUMP_OFF_FFX_ADDRESS, ffxStatus.humidity_off);
  89. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  90. osDelay(300);
  91. sync_ac_status(&ffxStatus);
  92. is_error_ffx[i] = 0;
  93. error_count = 0;
  94. DEBUG_PRINTF("Read that FFX exists.\n");
  95. } else{
  96. error_count++;
  97. if(error_count >= 10){
  98. set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
  99. }
  100. }
  101. osDelay(2000);
  102. }
  103. }
  104. osMutexRelease(ffx_mutex);
  105. }
  106. osDelay(1000);
  107. }
  108. }
  109. // 检测从机是否存在
  110. bool check_slave_exist(uint8_t slave_addr) {
  111. nmbs_set_destination_rtu_address(&nmbs, slave_addr);
  112. uint16_t result;
  113. osDelay(100);
  114. nmbs_error err = nmbs_read_holding_registers(&nmbs, 0x01, 1, &result);
  115. return err == NMBS_ERROR_NONE;
  116. }
  117. // 轮询从站
  118. void search_ffx_slave() {
  119. for (int i = SLAVE_ADDRESS_START; i <= SLAVE_ADDRESS_END; i++) {
  120. bool exist = check_slave_exist(i);
  121. if (exist) {
  122. slaves[i] = i;
  123. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  124. }
  125. osDelay(200);
  126. if (exist == NMBS_ERROR_NONE) {
  127. exist = check_slave_exist(i);
  128. if (exist != NMBS_ERROR_NONE) {
  129. slaves[i] = i;
  130. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  131. }else{
  132. set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
  133. }
  134. }
  135. osDelay(200);
  136. }
  137. }
  138. void ffx_master_init() {
  139. ffx_mutex = osMutexNew(NULL);
  140. nmbs_platform_conf platformConf;// 配置uart
  141. nmbs_platform_conf_create(&platformConf);
  142. platformConf.transport = NMBS_TRANSPORT_RTU;// RTU
  143. platformConf.read = &uart_read; // 读写函数
  144. platformConf.write = &uart_write; // 读写函数
  145. nmbs_client_create(&nmbs, &platformConf);// 创建客户端
  146. nmbs_set_read_timeout(&nmbs, 400);
  147. nmbs_set_byte_timeout(&nmbs, 400);
  148. // 开启轮询任务
  149. osThreadNew(ffx_poll_task, NULL, NULL);
  150. osThreadNew(ffx_mater_task, NULL, NULL);
  151. }