ffx_master.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, TEMP_ON_FFX_ADDRESS, 1, &data);
  68. if (err == NMBS_ERROR_NONE) {
  69. ffxStatus->humidity_on = data;
  70. }
  71. err = nmbs_read_holding_registers(&nmbs, TEMP_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(TEMP_ON_FFX_ADDRESS, ffxStatus.humidity_on);
  88. set_reg_value(TEMP_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 ++;
  94. } else{
  95. error_count++;
  96. if(error_count >= 10){
  97. set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
  98. }
  99. }
  100. osDelay(2000);
  101. }
  102. }
  103. osMutexRelease(ffx_mutex);
  104. }
  105. osDelay(1000);
  106. }
  107. }
  108. // 检测从机是否存在
  109. bool check_slave_exist(uint8_t slave_addr) {
  110. nmbs_set_destination_rtu_address(&nmbs, slave_addr);
  111. uint16_t result;
  112. osDelay(100);
  113. nmbs_error err = nmbs_read_holding_registers(&nmbs, 0x01, 1, &result);
  114. return err == NMBS_ERROR_NONE;
  115. }
  116. // 轮询从站
  117. void search_ffx_slave() {
  118. for (int i = SLAVE_ADDRESS_START; i <= SLAVE_ADDRESS_END; i++) {
  119. bool exist = check_slave_exist(i);
  120. if (exist) {
  121. slaves[i] = i;
  122. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  123. }
  124. osDelay(200);
  125. if (exist == NMBS_ERROR_NONE) {
  126. exist = check_slave_exist(i);
  127. if (exist != NMBS_ERROR_NONE) {
  128. slaves[i] = i;
  129. set_reg_value(INNER_ERROR2_REG_ADDRESS, 0);
  130. }else{
  131. set_reg_value(INNER_ERROR2_REG_ADDRESS, 1);
  132. }
  133. }
  134. osDelay(200);
  135. }
  136. }
  137. void ffx_master_init() {
  138. ffx_mutex = osMutexNew(NULL);
  139. nmbs_platform_conf platformConf;// 配置uart
  140. nmbs_platform_conf_create(&platformConf);
  141. platformConf.transport = NMBS_TRANSPORT_RTU;// RTU
  142. platformConf.read = &uart_read; // 读写函数
  143. platformConf.write = &uart_write; // 读写函数
  144. nmbs_client_create(&nmbs, &platformConf);// 创建客户端
  145. nmbs_set_read_timeout(&nmbs, 400);
  146. nmbs_set_byte_timeout(&nmbs, 400);
  147. // 开启轮询任务
  148. osThreadNew(ffx_poll_task, NULL, NULL);
  149. osThreadNew(ffx_mater_task, NULL, NULL);
  150. }