van_BSP_Scanner.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * uart_rcv.c
  3. *
  4. * Created on: 2022年4月20日
  5. * Author: dell
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include "onechip_include_header.h"
  12. #define ENV_ADJ_THRESHOLD 27.0
  13. #define DEBUG_BUF_SIZE (512) // 调试串口缓冲区长度
  14. #define BUF_SIZE (512) // 调试串口缓冲区长度
  15. enum SLAVE_MCU_Ctr_cmd_type
  16. {
  17. SLAVE_MCU_CMD_PARAM = 0x01,
  18. SLAVE_MCU_CMD_HEAT = 0x02,
  19. SLAVE_MCU_CMD_LIGHT_CTR= 0X03,
  20. SLAVE_MCU_CMD_QRSCANNER_CTR= 0X04,
  21. SLAVE_MCU_CMD_FAN= 0X05,
  22. SLAVE_MCU_CMD_CARD_ST= 0X06,
  23. SLAVE_MCU_CMD_PID = 0x20,
  24. };
  25. enum SLAVE_MCU_Ctr_UP_type
  26. {
  27. SLAVE_MCU_UP_HEAT = 0x10,
  28. SLAVE_MCU_UP_QR_PARAM= 0X11,
  29. SLAVE_MCU_UP_QR = 0x12,
  30. SLAVE_MCU_INFO, //异常信息
  31. };
  32. uint8_t g_uart2_rcv_start = 0;
  33. uint8_t g_uart2_rcv_complete_flg = 0;
  34. uint8_t g_uart2_rx_buf[DEBUG_BUF_SIZE] = {0};
  35. uint8_t g_uart2_tx_buf[BUF_SIZE] = {0};
  36. uint16_t g_uart2_rx_len = 0;
  37. str_hole_info_t hole1 = {0};
  38. str_hole_info_t hole2 = {0};
  39. str_hole_info_t hole3 = {0};
  40. str_hole_info_t hole4 = {0};
  41. str_hole_info_t hole5 = {0};
  42. str_hole_info_t hole6 = {0};
  43. str_hole_info_t hole7 = {0};
  44. str_hole_info_t hole8 = {0};
  45. str_hole_info_t hole9 = {0};
  46. str_hole_info_t hole10 = {0};
  47. str_card_param_info_t m_str_card_param_info = {0};
  48. unsigned int haveheaderflag = 0;
  49. uint32_t qr_check_delay = QR_CHECH_STATE_FIRST; /*1s定时器里面,大于0,每s减1*/
  50. extern void DelayMS(uint32_t ms);
  51. extern void QRparam_upload(str_card_param_info_t *p_str_card_param_info);
  52. extern uint8_t sys_info[40];
  53. uint32_t received_len;
  54. /********************************************************************
  55. * bcd_to_hex()
  56. *
  57. *描述:BCD转HEX
  58. *参数:无
  59. *返回:无
  60. *其他:无
  61. *--------------------------------------------------------------------
  62. *记录:
  63. ********************************************************************/
  64. uint8_t bcd_to_hex(uint8_t val)
  65. {
  66. return ((val / 10) << 4) + val % 10;
  67. }
  68. void svanner_receive_Rec_IDLE(UART_HandleTypeDef *huart)
  69. {
  70. if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
  71. {
  72. __HAL_UART_CLEAR_IDLEFLAG(huart);
  73. received_len = DEBUG_BUF_SIZE - __HAL_DMA_GET_COUNTER(huart->hdmarx);
  74. HAL_UART_DMAStop(huart);
  75. HAL_UART_Receive_DMA(huart, g_uart2_rx_buf, DEBUG_BUF_SIZE);
  76. }
  77. }
  78. void svanner_receive_analysis(void)
  79. {
  80. if(received_len){
  81. Van_Device_Printf(VAN_LOG_CRITICAL, "rec QR inif %d \n\r",received_len);
  82. unsigned filter = 0;
  83. /*while('{' != g_uart2_rx_buf[filter] && filter<received_len){
  84. filter++;
  85. }*/
  86. if ('{' == g_uart2_rx_buf[filter]){
  87. if(0 == parse_qrcode_info(&m_str_card_param_info, (char *)&g_uart2_rx_buf[filter])){
  88. haveheaderflag = QR_REC_STATE_SENDPRAM;
  89. qr_check_delay = 0;
  90. Van_Device_Printf(VAN_LOG_CRITICAL, "unpack json ok \n\r ");
  91. }
  92. else{
  93. Van_Device_Printf(VAN_LOG_CRITICAL, "unpack json error \n\r %s",g_uart2_rx_buf);
  94. }
  95. }
  96. else{
  97. Van_Device_Printf(VAN_LOG_CRITICAL, "header error \n\r %s",g_uart2_rx_buf);
  98. }
  99. received_len = 0;
  100. memset((char *)g_uart2_rx_buf,0,DEBUG_BUF_SIZE);
  101. }
  102. return;
  103. }
  104. int parse_qrcode_info(str_card_param_info_t *info, char *qrcode_str)
  105. {
  106. int id = 0;
  107. double Tt = 0;
  108. double Tt1 = 0;
  109. if (info == 0)
  110. {
  111. return -1;
  112. }
  113. cJSON *root = cJSON_Parse(qrcode_str);
  114. if (root == NULL)
  115. { /*消息不合格*/
  116. return -1;
  117. }
  118. cJSON *item = cJSON_GetObjectItem(root, "p");
  119. if (item == NULL)
  120. { /*消息不合格*/
  121. cJSON_Delete(root);
  122. return -1;
  123. }
  124. char *pid = cJSON_GetStringValue(item);
  125. // printf("pid=%s \r\n", pid);
  126. memcpy(info->pid, pid, 10);
  127. item = cJSON_GetObjectItem(root, "t");
  128. if (item == NULL)
  129. { /*消息不合格*/
  130. cJSON_Delete(root);
  131. return -1;
  132. }
  133. double t = item->valuedouble;
  134. // printf("t=%f \r\n", t);
  135. info->temp[0] = (uint8_t)t;
  136. info->temp[1] = (uint8_t)((t -(uint8_t)t)* 100);
  137. // 获取数组item
  138. item = cJSON_GetObjectItem(root, "e");
  139. if (item == NULL)
  140. { /*消息不合格*/
  141. cJSON_Delete(root);
  142. return -1;
  143. }
  144. int test_arry_size = cJSON_GetArraySize(item);
  145. for (int i = 0; i < test_arry_size; i++)
  146. {
  147. // 打印数组里的所有item
  148. cJSON *sub_array = cJSON_GetArrayItem(item, i);
  149. cJSON *sub = cJSON_GetObjectItem(sub_array, "i");
  150. id = sub->valueint;
  151. // printf("id=%d \r\n", id);
  152. if(id > 0)
  153. {
  154. sub = cJSON_GetObjectItemCaseSensitive(sub_array, "T");
  155. Tt = sub->valuedouble;
  156. // printf("Tt=%f \r\n", Tt);
  157. sub = cJSON_GetObjectItemCaseSensitive(sub_array, "t");
  158. Tt1 = sub->valuedouble;
  159. // printf("Tt1=%f \r\n", Tt1);
  160. }
  161. else
  162. {
  163. Tt = 0;
  164. Tt1 = 0;
  165. }
  166. info->hole_info[i].item = id;
  167. info->hole_info[i].TtMax[0] = (uint8_t)Tt;
  168. info->hole_info[i].TtMax[1] = (uint8_t)((Tt -(uint8_t)Tt)* 100);
  169. info->hole_info[i].TtMin[0] = (uint8_t)Tt1;
  170. info->hole_info[i].TtMin[1] = (uint8_t)((Tt1 -(uint8_t)Tt1)* 100);
  171. }
  172. // 记得删除json
  173. cJSON_Delete(root);
  174. return 0;
  175. }
  176. void svanner_receive_Rec_Start(UART_HandleTypeDef *huart){
  177. __HAL_UART_CLEAR_IDLEFLAG(huart);
  178. memset((char *)g_uart2_rx_buf,0,DEBUG_BUF_SIZE);
  179. HAL_UART_Receive_DMA(huart, g_uart2_rx_buf, DEBUG_BUF_SIZE);
  180. __HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);
  181. }