van_BSP_Scanner.c 6.7 KB

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