van_BSP_Scanner.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 QR_RESEND_3s 300
  14. #define DEBUG_BUF_SIZE (512) // 调试串口缓冲区长度
  15. #define BUF_SIZE (512) // 调试串口缓冲区长度
  16. enum SLAVE_MCU_Ctr_cmd_type
  17. {
  18. SLAVE_MCU_CMD_PARAM = 0x01,
  19. SLAVE_MCU_CMD_HEAT = 0x02,
  20. SLAVE_MCU_CMD_LIGHT_CTR= 0X03,
  21. SLAVE_MCU_CMD_QRSCANNER_CTR= 0X04,
  22. SLAVE_MCU_CMD_FAN= 0X05,
  23. SLAVE_MCU_CMD_CARD_ST= 0X06,
  24. SLAVE_MCU_CMD_PID = 0x20,
  25. };
  26. enum SLAVE_MCU_Ctr_UP_type
  27. {
  28. SLAVE_MCU_UP_HEAT = 0x10,
  29. SLAVE_MCU_UP_QR_PARAM= 0X11,
  30. SLAVE_MCU_UP_QR = 0x12,
  31. SLAVE_MCU_INFO, //异常信息
  32. };
  33. uint8_t g_uart2_rcv_start = 0;
  34. uint8_t g_uart2_rcv_complete_flg = 0;
  35. uint8_t g_uart2_rx_buf[DEBUG_BUF_SIZE] = {0};
  36. // uint8_t g_uart2_tx_buf[BUF_SIZE] = {0};
  37. uint32_t g_uart2_rx_len = 0;
  38. str_hole_info_t hole1 = {0};
  39. str_hole_info_t hole2 = {0};
  40. str_hole_info_t hole3 = {0};
  41. str_hole_info_t hole4 = {0};
  42. str_hole_info_t hole5 = {0};
  43. str_hole_info_t hole6 = {0};
  44. str_hole_info_t hole7 = {0};
  45. str_hole_info_t hole8 = {0};
  46. str_hole_info_t hole9 = {0};
  47. str_hole_info_t hole10 = {0};
  48. str_card_param_info_t m_str_card_param_info = {0};
  49. unsigned int haveheaderflag = 0;
  50. uint32_t qr_check_delay = QR_CHECH_STATE_FIRST; /*1s定时器里面,大于0,每s减1*/
  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. void svanner_receive_Rec_Start(UART_HandleTypeDef *huart){
  55. __HAL_UART_CLEAR_IDLEFLAG(huart);
  56. memset((char *)g_uart2_rx_buf,0,DEBUG_BUF_SIZE);
  57. received_len = 0;
  58. HAL_UART_Receive_DMA(huart, g_uart2_rx_buf, DEBUG_BUF_SIZE);
  59. __HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);
  60. sysinf.updateQR = false;
  61. }
  62. void svanner_receive_Rec_Stop(UART_HandleTypeDef *huart){
  63. HAL_UART_DMAStop(huart);
  64. }
  65. /********************************************************************
  66. * bcd_to_hex()
  67. *
  68. *描述:BCD转HEX
  69. *参数:无
  70. *返回:无
  71. *其他:无
  72. *--------------------------------------------------------------------
  73. *记录:
  74. ********************************************************************/
  75. uint8_t bcd_to_hex(uint8_t val)
  76. {
  77. return ((val / 10) << 4) + val % 10;
  78. }
  79. void svanner_receive_Rec_IDLE(UART_HandleTypeDef *huart)
  80. {
  81. if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
  82. {
  83. __HAL_UART_CLEAR_IDLEFLAG(huart);
  84. received_len = DEBUG_BUF_SIZE - __HAL_DMA_GET_COUNTER(huart->hdmarx);
  85. HAL_UART_DMAStop(huart);
  86. HAL_UART_Receive_DMA(huart, g_uart2_rx_buf, DEBUG_BUF_SIZE);
  87. }
  88. }
  89. void svanner_receive_analysis(void)
  90. {
  91. if(received_len){
  92. // Van_Device_Printf(VAN_LOG_INFO, "rec QR inif %d \n\r",received_len);
  93. if (('{' == g_uart2_rx_buf[0]) && (sysinf.updateQR == false)){
  94. if(0 == parse_qrcode_info(&m_str_card_param_info, (char *)&g_uart2_rx_buf[0])){
  95. haveheaderflag = QR_REC_STATE_SENDPRAM;
  96. memcpy(&g_uart2_rx_buf[received_len], sysinf.SN, 7);
  97. received_len += 7;
  98. qr_check_delay = 0;
  99. if((sysinf.psmMaster.state == SW_Scan_Qrcode || sysinf.psmMaster.state == SW_Chip_Id )){
  100. svanner_receive_Rec_Stop(sysinf.scannerUart);
  101. sysinf.updateQR = true;
  102. data_send_to_pc(PC_CMD_QRCODE,(char *)g_uart2_rx_buf, received_len);
  103. memcpy(&sysinf.psmMaster.card_param_info, &m_str_card_param_info, sizeof(str_card_param_info_t));
  104. }
  105. else{
  106. #ifdef ONECHIP_MINI_PLUSE
  107. if((sysinf.psmSlave.state == SW_Scan_Qrcode || sysinf.psmSlave.state == SW_Chip_Id )){
  108. }
  109. #endif
  110. }
  111. sysinf.resendeQRtimer = 1;
  112. beep_set(BEEP_SCANOK);
  113. }
  114. else{
  115. Van_Device_Printf(VAN_LOG_WARN, "unpack json error \n\r %s",g_uart2_rx_buf);
  116. }
  117. }
  118. else{
  119. if(sysinf.updateQR == false){
  120. Van_Device_Printf(VAN_LOG_WARN, "header error \n\r %s",g_uart2_rx_buf);
  121. svanner_receive_Rec_Start(sysinf.scannerUart);
  122. }
  123. }
  124. }
  125. return;
  126. }
  127. /*****************************************************************************
  128. *
  129. *二维码未收到应答,重新上报
  130. *
  131. ************************************************************************************/
  132. void reSend_QR(void)
  133. {
  134. if(sysinf.updateQR ){
  135. sysinf.resendeQRtimer++;
  136. if((sysinf.resendeQRtimer % QR_RESEND_3s)==0){
  137. data_send_to_pc(PC_CMD_QRCODE,(char *)g_uart2_rx_buf, received_len);
  138. if(sysinf.resendeQRtimer > 5*QR_RESEND_3s){
  139. /*最多重发5次*/
  140. svanner_receive_Rec_Start(sysinf.scannerUart);
  141. }
  142. }
  143. }
  144. }
  145. int parse_qrcode_info(str_card_param_info_t *info, char *qrcode_str)
  146. {
  147. int id = 0;
  148. double Tt = 0;
  149. double Tt1 = 0;
  150. if (info == 0)
  151. {
  152. return -1;
  153. }
  154. cJSON *root = cJSON_Parse(qrcode_str);
  155. if (root == NULL)
  156. { /*消息不合格*/
  157. return -1;
  158. }
  159. cJSON *item = cJSON_GetObjectItem(root, "p");
  160. if (item == NULL)
  161. { /*消息不合格*/
  162. cJSON_Delete(root);
  163. return -1;
  164. }
  165. char *pid = cJSON_GetStringValue(item);
  166. // printf("pid=%s \r\n", pid);
  167. memcpy(info->pid, pid, 10);
  168. item = cJSON_GetObjectItem(root, "t");
  169. if (item == NULL)
  170. { /*消息不合格*/
  171. cJSON_Delete(root);
  172. return -1;
  173. }
  174. double t = item->valuedouble;
  175. // printf("t=%f \r\n", t);
  176. info->temp[0] = (uint8_t)t;
  177. info->temp[1] = (uint8_t)((t -(uint8_t)t)* 100);
  178. // 获取数组item
  179. item = cJSON_GetObjectItem(root, "e");
  180. if (item == NULL)
  181. { /*消息不合格*/
  182. cJSON_Delete(root);
  183. return -1;
  184. }
  185. int test_arry_size = cJSON_GetArraySize(item);
  186. for (int i = 0; i < test_arry_size; i++)
  187. {
  188. // 打印数组里的所有item
  189. cJSON *sub_array = cJSON_GetArrayItem(item, i);
  190. cJSON *sub = cJSON_GetObjectItem(sub_array, "i");
  191. id = sub->valueint;
  192. // printf("id=%d \r\n", id);
  193. if(id > 0)
  194. {
  195. sub = cJSON_GetObjectItemCaseSensitive(sub_array, "T");
  196. Tt = sub->valuedouble;
  197. // printf("Tt=%f \r\n", Tt);
  198. sub = cJSON_GetObjectItemCaseSensitive(sub_array, "t");
  199. Tt1 = sub->valuedouble;
  200. // printf("Tt1=%f \r\n", Tt1);
  201. }
  202. else
  203. {
  204. Tt = 0;
  205. Tt1 = 0;
  206. }
  207. info->hole_info[i].item = id;
  208. info->hole_info[i].TtMax[0] = (uint8_t)Tt;
  209. info->hole_info[i].TtMax[1] = (uint8_t)((Tt -(uint8_t)Tt)* 100);
  210. info->hole_info[i].TtMin[0] = (uint8_t)Tt1;
  211. info->hole_info[i].TtMin[1] = (uint8_t)((Tt1 -(uint8_t)Tt1)* 100);
  212. }
  213. // 记得删除json
  214. cJSON_Delete(root);
  215. return 0;
  216. }