van_BSP_Scanner.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 QRparam_upload(str_card_param_info_t *p_str_card_param_info);
  51. extern uint8_t sys_info[40];
  52. uint32_t received_len;
  53. void svanner_receive_Rec_Start(UART_HandleTypeDef *huart){
  54. __HAL_UART_CLEAR_IDLEFLAG(huart);
  55. memset((char *)g_uart2_rx_buf,0,DEBUG_BUF_SIZE);
  56. HAL_UART_Receive_DMA(huart, g_uart2_rx_buf, DEBUG_BUF_SIZE);
  57. __HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);
  58. }
  59. void svanner_receive_Rec_Stop(UART_HandleTypeDef *huart){
  60. HAL_UART_DMAStop(huart);
  61. }
  62. /********************************************************************
  63. * bcd_to_hex()
  64. *
  65. *描述:BCD转HEX
  66. *参数:无
  67. *返回:无
  68. *其他:无
  69. *--------------------------------------------------------------------
  70. *记录:
  71. ********************************************************************/
  72. uint8_t bcd_to_hex(uint8_t val)
  73. {
  74. return ((val / 10) << 4) + val % 10;
  75. }
  76. void svanner_receive_Rec_IDLE(UART_HandleTypeDef *huart)
  77. {
  78. if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
  79. {
  80. __HAL_UART_CLEAR_IDLEFLAG(huart);
  81. received_len = DEBUG_BUF_SIZE - __HAL_DMA_GET_COUNTER(huart->hdmarx);
  82. HAL_UART_DMAStop(huart);
  83. HAL_UART_Receive_DMA(huart, g_uart2_rx_buf, DEBUG_BUF_SIZE);
  84. sysinf.updateQR = false;
  85. }
  86. }
  87. void svanner_receive_analysis(void)
  88. {
  89. if(received_len){
  90. Van_Device_Printf(VAN_LOG_CRITICAL, "rec QR inif %d \n\r",received_len);
  91. unsigned filter = 0;
  92. /*while('{' != g_uart2_rx_buf[filter] && filter<received_len){
  93. filter++;
  94. }*/
  95. if ('{' == g_uart2_rx_buf[0]){
  96. if(0 == parse_qrcode_info(&m_str_card_param_info, (char *)&g_uart2_rx_buf[0])){
  97. haveheaderflag = QR_REC_STATE_SENDPRAM;
  98. memcpy(&g_uart2_rx_buf[g_uart2_rx_len], sysinf.SN, 7);
  99. g_uart2_rx_len += 7;
  100. qr_check_delay = 0;
  101. if((sysinf.psmMaster.state == SW_Scan_Qrcode || sysinf.psmMaster.state == SW_Chip_Id )){
  102. svanner_receive_Rec_Stop(sysinf.scannerUart);
  103. sysinf.updateQR = true;
  104. data_send_to_pc(PC_CMD_QRCODE,(char *)g_uart2_rx_buf, g_uart2_rx_len);
  105. }else{
  106. #ifdef ONECHIP_MINI_PLUSE
  107. if((sysinf.psmSlave.state == SW_Scan_Qrcode || sysinf.psmSlave.state == SW_Chip_Id )){
  108. {
  109. }
  110. #endif
  111. }
  112. Van_Device_Printf(VAN_LOG_CRITICAL, "unpack json ok \n\r ");
  113. }
  114. else{
  115. Van_Device_Printf(VAN_LOG_CRITICAL, "unpack json error \n\r %s",g_uart2_rx_buf);
  116. }
  117. }
  118. else{
  119. Van_Device_Printf(VAN_LOG_CRITICAL, "header error \n\r %s",g_uart2_rx_buf);
  120. }
  121. received_len = 0;
  122. memset((char *)g_uart2_rx_buf,0,DEBUG_BUF_SIZE);
  123. }
  124. return;
  125. }
  126. int parse_qrcode_info(str_card_param_info_t *info, char *qrcode_str)
  127. {
  128. int id = 0;
  129. double Tt = 0;
  130. double Tt1 = 0;
  131. if (info == 0)
  132. {
  133. return -1;
  134. }
  135. cJSON *root = cJSON_Parse(qrcode_str);
  136. if (root == NULL)
  137. { /*消息不合格*/
  138. return -1;
  139. }
  140. cJSON *item = cJSON_GetObjectItem(root, "p");
  141. if (item == NULL)
  142. { /*消息不合格*/
  143. cJSON_Delete(root);
  144. return -1;
  145. }
  146. char *pid = cJSON_GetStringValue(item);
  147. // printf("pid=%s \r\n", pid);
  148. memcpy(info->pid, pid, 10);
  149. item = cJSON_GetObjectItem(root, "t");
  150. if (item == NULL)
  151. { /*消息不合格*/
  152. cJSON_Delete(root);
  153. return -1;
  154. }
  155. double t = item->valuedouble;
  156. // printf("t=%f \r\n", t);
  157. info->temp[0] = (uint8_t)t;
  158. info->temp[1] = (uint8_t)((t -(uint8_t)t)* 100);
  159. // 获取数组item
  160. item = cJSON_GetObjectItem(root, "e");
  161. if (item == NULL)
  162. { /*消息不合格*/
  163. cJSON_Delete(root);
  164. return -1;
  165. }
  166. int test_arry_size = cJSON_GetArraySize(item);
  167. for (int i = 0; i < test_arry_size; i++)
  168. {
  169. // 打印数组里的所有item
  170. cJSON *sub_array = cJSON_GetArrayItem(item, i);
  171. cJSON *sub = cJSON_GetObjectItem(sub_array, "i");
  172. id = sub->valueint;
  173. // printf("id=%d \r\n", id);
  174. if(id > 0)
  175. {
  176. sub = cJSON_GetObjectItemCaseSensitive(sub_array, "T");
  177. Tt = sub->valuedouble;
  178. // printf("Tt=%f \r\n", Tt);
  179. sub = cJSON_GetObjectItemCaseSensitive(sub_array, "t");
  180. Tt1 = sub->valuedouble;
  181. // printf("Tt1=%f \r\n", Tt1);
  182. }
  183. else
  184. {
  185. Tt = 0;
  186. Tt1 = 0;
  187. }
  188. info->hole_info[i].item = id;
  189. info->hole_info[i].TtMax[0] = (uint8_t)Tt;
  190. info->hole_info[i].TtMax[1] = (uint8_t)((Tt -(uint8_t)Tt)* 100);
  191. info->hole_info[i].TtMin[0] = (uint8_t)Tt1;
  192. info->hole_info[i].TtMin[1] = (uint8_t)((Tt1 -(uint8_t)Tt1)* 100);
  193. }
  194. // 记得删除json
  195. cJSON_Delete(root);
  196. return 0;
  197. }