van_BSP_Scanner.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. Refresh_IWDG();
  95. if(0 == parse_qrcode_info(&m_str_card_param_info, (char *)&g_uart2_rx_buf[0])){
  96. haveheaderflag = QR_REC_STATE_SENDPRAM;
  97. memcpy(&g_uart2_rx_buf[received_len], sysinf.SN, 7);
  98. received_len += 7;
  99. qr_check_delay = 0;
  100. if((sysinf.psmMaster.state == SW_Scan_Qrcode || sysinf.psmMaster.state == SW_Chip_Id )){
  101. svanner_receive_Rec_Stop(sysinf.scannerUart);
  102. sysinf.updateQR = true;
  103. data_send_to_pc(PC_CMD_QRCODE,(char *)g_uart2_rx_buf, received_len);
  104. memcpy(&sysinf.psmMaster.card_param_info, &m_str_card_param_info, sizeof(str_card_param_info_t));
  105. sysinf.resendeQRtimer = 1;
  106. // beep_set(BEEP_SCANOK);
  107. }
  108. else{
  109. svanner_receive_Rec_Start(sysinf.scannerUart);
  110. #ifdef ONECHIP_MINI_PLUSE
  111. if((sysinf.psmSlave.state == SW_Scan_Qrcode || sysinf.psmSlave.state == SW_Chip_Id )){
  112. }
  113. #endif
  114. }
  115. // sysinf.resendeQRtimer = 1;
  116. beep_set(BEEP_SCANOK);
  117. }
  118. else{
  119. Van_Device_Printf(VAN_LOG_WARN, "unpack json error \n\r %s",g_uart2_rx_buf);
  120. }
  121. }
  122. else{
  123. if(sysinf.updateQR == false){
  124. Van_Device_Printf(VAN_LOG_WARN, "header error \n\r %s",g_uart2_rx_buf);
  125. svanner_receive_Rec_Start(sysinf.scannerUart);
  126. }
  127. }
  128. }
  129. return;
  130. }
  131. /*****************************************************************************
  132. *
  133. *二维码未收到应答,重新上报
  134. *
  135. ************************************************************************************/
  136. void reSend_QR(void)
  137. {
  138. if(sysinf.updateQR ){
  139. sysinf.resendeQRtimer++;
  140. if((sysinf.resendeQRtimer % QR_RESEND_3s)==0){
  141. data_send_to_pc(PC_CMD_QRCODE,(char *)g_uart2_rx_buf, received_len);
  142. if(sysinf.resendeQRtimer > 5*QR_RESEND_3s){
  143. /*最多重发5次*/
  144. svanner_receive_Rec_Start(sysinf.scannerUart);
  145. }
  146. }
  147. }
  148. }
  149. int parse_qrcode_info(str_card_param_info_t *info, char *qrcode_str)
  150. {
  151. int id = 0;
  152. double Tt = 0;
  153. double Tt1 = 0;
  154. if (info == 0)
  155. {
  156. return -1;
  157. }
  158. cJSON *root = cJSON_Parse(qrcode_str);
  159. if (root == NULL)
  160. { /*消息不合格*/
  161. return -1;
  162. }
  163. cJSON *item = cJSON_GetObjectItem(root, "p");
  164. if (item == NULL)
  165. { /*消息不合格*/
  166. cJSON_Delete(root);
  167. return -1;
  168. }
  169. char *pid = cJSON_GetStringValue(item);
  170. // printf("pid=%s \r\n", pid);
  171. memcpy(info->pid, pid, 10);
  172. item = cJSON_GetObjectItem(root, "t");
  173. if (item == NULL)
  174. { /*消息不合格*/
  175. cJSON_Delete(root);
  176. return -1;
  177. }
  178. double t = item->valuedouble;
  179. // printf("t=%f \r\n", t);
  180. info->temp[0] = (uint8_t)t;
  181. info->temp[1] = (uint8_t)((t -(uint8_t)t)* 100);
  182. // 获取数组item
  183. item = cJSON_GetObjectItem(root, "e");
  184. if (item == NULL)
  185. { /*消息不合格*/
  186. cJSON_Delete(root);
  187. return -1;
  188. }
  189. int test_arry_size = cJSON_GetArraySize(item);
  190. for (int i = 0; i < test_arry_size; i++)
  191. {
  192. // 打印数组里的所有item
  193. cJSON *sub_array = cJSON_GetArrayItem(item, i);
  194. cJSON *sub = cJSON_GetObjectItem(sub_array, "i");
  195. id = sub->valueint;
  196. // printf("id=%d \r\n", id);
  197. if(id > 0)
  198. {
  199. sub = cJSON_GetObjectItemCaseSensitive(sub_array, "T");
  200. Tt = sub->valuedouble;
  201. // printf("Tt=%f \r\n", Tt);
  202. sub = cJSON_GetObjectItemCaseSensitive(sub_array, "t");
  203. Tt1 = sub->valuedouble;
  204. // printf("Tt1=%f \r\n", Tt1);
  205. }
  206. else
  207. {
  208. Tt = 0;
  209. Tt1 = 0;
  210. }
  211. info->hole_info[i].item = id;
  212. info->hole_info[i].TtMax[0] = (uint8_t)Tt;
  213. info->hole_info[i].TtMax[1] = (uint8_t)((Tt -(uint8_t)Tt)* 100);
  214. info->hole_info[i].TtMin[0] = (uint8_t)Tt1;
  215. info->hole_info[i].TtMin[1] = (uint8_t)((Tt1 -(uint8_t)Tt1)* 100);
  216. }
  217. // 记得删除json
  218. cJSON_Delete(root);
  219. return 0;
  220. }