uart_rcv.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 "NuMicro.h"
  12. #include "cfg_param_record.h"
  13. #include "uart_rcv.h"
  14. #include "usr_pid.h"
  15. #include "cJSON.h"
  16. #include "ws2812.h"
  17. #define ENV_ADJ_THRESHOLD 27.0
  18. enum SLAVE_MCU_Ctr_cmd_type
  19. {
  20. SLAVE_MCU_CMD_PARAM = 0x01,
  21. SLAVE_MCU_CMD_HEAT = 0x02,
  22. SLAVE_MCU_CMD_LIGHT_CTR= 0X03,
  23. SLAVE_MCU_CMD_QRSCANNER_CTR= 0X04,
  24. SLAVE_MCU_CMD_FAN= 0X05,
  25. SLAVE_MCU_CMD_CARD_ST= 0X06,
  26. SLAVE_MCU_CMD_PID = 0x20,
  27. };
  28. enum SLAVE_MCU_Ctr_UP_type
  29. {
  30. SLAVE_MCU_UP_HEAT = 0x10,
  31. SLAVE_MCU_UP_QR_PARAM= 0X11,
  32. SLAVE_MCU_UP_QR = 0x12,
  33. SLAVE_MCU_INFO, //异常信息
  34. };
  35. #define combine_cmdMessage(msg,header,seqer,cmder,tailer) { \
  36. msg.head = header; \
  37. msg.seq = seqer; \
  38. msg.cmd = cmder; \
  39. msg.tail = tailer; \
  40. }
  41. uint8_t g_uart2_rcv_start = 0;
  42. uint8_t g_uart2_rcv_complete_flg = 0;
  43. uint8_t g_uart2_rx_buf[DEBUG_BUF_SIZE] = {0};
  44. uint8_t g_uart2_tx_buf[BUF_SIZE] = {0};
  45. uint16_t g_uart2_rx_len = 0;
  46. uint8_t g_uart2_frame_overtime_cnt = 0;
  47. uint16_t g_uart2_rcv_cmd = 0;
  48. uint16_t g_uart2_cmd_overtime_secs = 0;
  49. uint32_t g_uart2_cmd_overtime_cnt = 0;
  50. uint8_t g_param_set_mode = 0; // 参数设置模式
  51. uint8_t g_uart1_rcv_start = 0;
  52. uint8_t g_uart1_rcv_complete_flg = 0;
  53. uint8_t g_uart1_tx_buf[BUF_SIZE] = {0};
  54. uint8_t g_uart1_rx_buf[BUF_SIZE] = {0};
  55. uint8_t g_uart1_rx_len = 0;
  56. uint8_t g_uart1_frame_overtime_cnt = 0;
  57. uint8_t g_uart0_rcv_start = 0;
  58. uint8_t g_uart0_rcv_complete_flg = 0;
  59. uint8_t g_uart0_tx_buf[DEBUG_BUF_SIZE] = {0};
  60. uint8_t g_uart0_rx_buf[BUF_SIZE] = {0};
  61. uint8_t g_uart0_rx_len = 0;
  62. uint8_t g_uart0_frame_overtime_cnt = 0;
  63. struct CmdMessage uart0_massage;
  64. uint8_t uart0_data[DEBUG_BUF_SIZE];
  65. uint32_t uart0_header = 0;
  66. uint32_t uart0_last = 0;
  67. uint32_t uart0_recOK = 0;
  68. unsigned int haveheaderflag = 0;
  69. heat_ctrl_struct g_heat_ctrl = {0};
  70. // card_param_struct card = {0};
  71. str_hole_info_t hole1 = {0};
  72. str_hole_info_t hole2 = {0};
  73. str_hole_info_t hole3 = {0};
  74. str_hole_info_t hole4 = {0};
  75. str_hole_info_t hole5 = {0};
  76. str_hole_info_t hole6 = {0};
  77. str_hole_info_t hole7 = {0};
  78. str_hole_info_t hole8 = {0};
  79. str_hole_info_t hole9 = {0};
  80. str_hole_info_t hole10 = {0};
  81. str_card_param_info_t m_str_card_param_info = {0};
  82. uint32_t qr_check_delay = QR_CHECH_STATE_FIRST; /*1s定时器里面,大于0,每s减1*/
  83. extern void QRparam_upload(str_card_param_info_t *p_str_card_param_info);
  84. extern uint8_t sys_info[40];
  85. /********************************************************************
  86. * bcd_to_hex()
  87. *
  88. *描述:BCD转HEX
  89. *参数:无
  90. *返回:无
  91. *其他:无
  92. *--------------------------------------------------------------------
  93. *记录:
  94. ********************************************************************/
  95. uint8_t bcd_to_hex(uint8_t val)
  96. {
  97. return ((val / 10) << 4) + val % 10;
  98. }
  99. /********************************************************************
  100. * debug_receive_int_handle()
  101. *
  102. *描述:调试串口接收中断数据处理
  103. *参数:无
  104. *返回:无
  105. *其他:无
  106. *--------------------------------------------------------------------
  107. *记录:
  108. ********************************************************************/
  109. void debug_receive_int_handle0(void)
  110. {
  111. if (UART_GET_INT_FLAG(UART0, UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk))
  112. {
  113. /* UART receive data available flag */
  114. /* Move the data from Rx FIFO to sw buffer (RAM). */
  115. /* Every time leave 1 byte data in FIFO for Rx timeout */
  116. while (UART_GET_RX_EMPTY(UART0) == 0)
  117. {
  118. if (uart0_last == BUF_SIZE)uart0_last = 0;
  119. g_uart0_rx_buf[uart0_last] = UART_READ(UART0);
  120. uart0_last++;
  121. }
  122. }
  123. }
  124. void debug_receive_int_handle1(void)
  125. {
  126. while (UART_GET_RX_EMPTY(UART1) == 0)
  127. {
  128. if (g_uart1_rx_len >= BUF_SIZE)
  129. g_uart1_rx_len = 0;
  130. g_uart1_rx_buf[g_uart1_rx_len] = UART_READ(UART1);
  131. g_uart1_rx_len++;
  132. }
  133. }
  134. void debug_receive_int_handle2(void)
  135. {
  136. while (UART_GET_RX_EMPTY(UART2) == 0)
  137. {
  138. unsigned char recChar = UART_READ(UART2);
  139. if(haveheaderflag <= QR_REC_STATE_HEADER){
  140. if (g_uart2_rx_len >= DEBUG_BUF_SIZE)
  141. g_uart2_rx_len = 0;
  142. g_uart2_rx_buf[g_uart2_rx_len++] = recChar;
  143. }
  144. }
  145. }
  146. /********************************************************************
  147. * UART02_IRQHandler()
  148. *
  149. *描述:串口接收中断
  150. *参数:无
  151. *返回:无
  152. *其他:无
  153. *--------------------------------------------------------------------
  154. *记录:
  155. ********************************************************************/
  156. void UART02_IRQHandler(void)
  157. {
  158. if (UART_GET_INT_FLAG(UART0, UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk | UART_INTSTS_RXTOIF_Msk) )
  159. {
  160. debug_receive_int_handle0();
  161. }
  162. else if (UART_GET_INT_FLAG(UART0,UART_INTSTS_BUFERRIF_Msk) )
  163. {
  164. debug_receive_int_handle0();
  165. UART_ClearIntFlag(UART0,UART_INTSTS_BUFERRINT_Msk);
  166. }
  167. else if (UART_GET_INT_FLAG(UART0,UART_INTSTS_RLSINT_Msk) )
  168. {
  169. UART_ClearIntFlag(UART0,UART_INTSTS_RLSINT_Msk);
  170. }
  171. if (UART_GET_INT_FLAG(UART2, UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk | UART_INTSTS_RXTOIF_Msk) )
  172. {
  173. debug_receive_int_handle2();
  174. }
  175. else if (UART_GET_INT_FLAG(UART2,UART_INTSTS_BUFERRIF_Msk) )
  176. {
  177. debug_receive_int_handle2();
  178. UART_ClearIntFlag(UART2,UART_INTSTS_BUFERRINT_Msk);
  179. }
  180. else if (UART_GET_INT_FLAG(UART2,UART_INTSTS_RLSINT_Msk) )
  181. {
  182. UART_ClearIntFlag(UART2,UART_INTSTS_RLSINT_Msk);
  183. }
  184. }
  185. /********************************************************************
  186. * UART13_IRQHandler()
  187. *
  188. *描述:串口接收中断
  189. *参数:无
  190. *返回:无
  191. *其他:无
  192. *--------------------------------------------------------------------
  193. *记录:
  194. ********************************************************************/
  195. void UART13_IRQHandler(void)
  196. {
  197. if (UART_GET_INT_FLAG(UART1, UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk | UART_INTSTS_RXTOIF_Msk) )
  198. {
  199. debug_receive_int_handle1();
  200. }
  201. else if (UART_GET_INT_FLAG(UART1,UART_INTSTS_BUFERRIF_Msk) )
  202. {
  203. debug_receive_int_handle1();
  204. UART_ClearIntFlag(UART1,UART_INTSTS_BUFERRINT_Msk);
  205. }
  206. else if (UART_GET_INT_FLAG(UART1,UART_INTSTS_RLSINT_Msk) )
  207. {
  208. UART_ClearIntFlag(UART1,UART_INTSTS_RLSINT_Msk);
  209. }
  210. }
  211. void sendMsgtoMasterMcu(struct CmdMessage *pmsg)
  212. {
  213. static uint8_t sedbuf[DEBUG_BUF_SIZE];
  214. uint16_t checksum = 0;
  215. *(unsigned short *)sedbuf = pmsg->head;
  216. sedbuf[2] = pmsg->seq;
  217. sedbuf[3] = pmsg->cmd;
  218. *(unsigned short *)(sedbuf+4) = pmsg->len;
  219. for(int i=0; i < pmsg->len; i++){
  220. sedbuf[6+i] = pmsg->data[i];
  221. }
  222. for(int i=2; i < (pmsg->len + 6); i++){
  223. checksum += sedbuf[i];
  224. }
  225. sedbuf[6+pmsg->len] = checksum&0xff;/*结尾标记高位*/
  226. sedbuf[7+pmsg->len ] = checksum>>8 ;/*结尾标记低位*/
  227. UART_Write(UART0, sedbuf, (8+pmsg->len));
  228. }
  229. uint16_t checkSum(const uint8_t *pInBuffer, int uLen)
  230. {
  231. uint32_t sum = 0;
  232. for(int i = 0; i < uLen; i++)
  233. {
  234. sum += pInBuffer[i];
  235. }
  236. return (sum);
  237. }
  238. /********************************************************************
  239. * int Decode(const char *pInBuffer, int uLen)
  240. *
  241. *描述:对串口数据进行解码成结构体
  242. *参数:pInBuffer 接收缓冲区
  243. *uheader 当前头位置
  244. ulast 当前头尾位置
  245. maxlen 缓冲区长度
  246. pMessage 命令结构体
  247. pdata 命令数据
  248. *返回:1解码成功,0解码失败
  249. *其他:无
  250. *--------------------------------------------------------------------
  251. *记录:
  252. ********************************************************************/
  253. int DecodeNew(struct CmdMessage *pMessage, uint8_t *pdata, const uint8_t *pInBuffer, uint32_t* pheader,uint32_t ulast,uint32_t maxlen)
  254. {
  255. uint32_t bufRealLen;
  256. uint32_t sizeofMessage = MINI7_MASSAGE_SIZE;
  257. uint32_t header = *pheader;
  258. uint32_t i = 0;
  259. if (NULL == pInBuffer || NULL == pMessage || NULL == pdata || NULL == pheader)
  260. {
  261. /*空指针判断*/
  262. return 0;
  263. }
  264. /*缓冲区有效数据长度,缓冲区循环处理,会出现头大于未情况*/
  265. bufRealLen = (ulast > header)?(ulast - header):(ulast + maxlen - header);
  266. if(bufRealLen < sizeofMessage){
  267. /*长度过段,跳过*/
  268. return 0;
  269. }
  270. bufRealLen -= sizeofMessage; /*最大有效数据长度*/
  271. for(; i<=bufRealLen;){
  272. uint32_t headerflag = pInBuffer[(header + i)%maxlen];
  273. headerflag += pInBuffer[(header + i + 1)%maxlen] <<8;
  274. if(headerflag != MINI7_HEADER_FALG){
  275. i += 1;
  276. // header = (header + 1)%maxlen;
  277. continue;
  278. }
  279. uint32_t messagelen = pInBuffer[(header + i + 4)%maxlen];
  280. messagelen += pInBuffer[(header + i + 5)%maxlen] <<8;
  281. if(messagelen > (maxlen- sizeofMessage)){
  282. /*长度超长 跳过2个字节*/
  283. i += 1;
  284. // header = (header + 1)%maxlen;
  285. continue;
  286. }
  287. if(messagelen >bufRealLen){
  288. /*当前起始点可能存在数据,跳出循环,*/
  289. i++; /*for采样小于等于判断,循环次数多1,此处加1便于后续处理*/
  290. break;
  291. }
  292. uint16_t checkesum = 0;
  293. uint16_t recsum = 0;
  294. if((header + i + 6 + messagelen) < maxlen){
  295. checkesum = checkSum(pInBuffer + header + i + 2, messagelen+4);
  296. }else{
  297. checkesum = checkSum(pInBuffer + header + i + 2, (maxlen - (header + i + 2)));
  298. checkesum += checkSum(pInBuffer, (header + i + 6 + messagelen)%maxlen);
  299. }
  300. recsum = *(pInBuffer + (header + i + 6 + messagelen)%maxlen ) ;
  301. recsum += *(pInBuffer + (header + i + 7 + messagelen)%maxlen) << 8;
  302. /*固定结尾预留做调试用*/
  303. if(checkesum != recsum && recsum>>8 != MINI7_TAIL_FALG){
  304. i += 1;
  305. // ? ? ? ? ?header = (header + 1)%maxlen;
  306. }else{
  307. /*解析成功,拷贝消息结构体*/
  308. uint8_t * pCharMessage = (uint8_t *)pMessage;
  309. for(unsigned copyi=0;copyi<(sizeofMessage-2);copyi++){
  310. *pCharMessage++ = pInBuffer[(header + i+ copyi)%maxlen];
  311. }
  312. (*pMessage).tail = MINI7_TAIL_FALG;
  313. /*解析成功,拷贝数据结构体*/
  314. for(unsigned copyi=0;copyi<messagelen;copyi++){
  315. *pdata++ = pInBuffer[(header + i + 6 + copyi)%maxlen];
  316. }
  317. *pheader = (header + messagelen + i + sizeofMessage - 2)%maxlen;
  318. return 1;
  319. }
  320. }
  321. *pheader = (header + i - 1)%maxlen;
  322. return 0;
  323. }
  324. /********************************************************************
  325. * uart0_rcv_frame_analysis()
  326. *
  327. *描述:接收帧解析
  328. *参数:无
  329. *返回:无
  330. *其他:无
  331. *--------------------------------------------------------------------
  332. *记录:
  333. ********************************************************************/
  334. void uart0_rcv_data_analysis(void)
  335. {
  336. float kp, ki, kd;
  337. uint8_t false = 0;
  338. struct CmdMessage msg;
  339. uint16_t result =0;
  340. struct light_band_para_t light;
  341. msg.len = 2;
  342. msg.data = (char*)&result;
  343. if(uart0_recOK == false){
  344. return;
  345. }
  346. uart0_recOK = false;
  347. if(uart0_massage.seq == MINI7_MASSAGE_TYPE_MCUTOMCU){
  348. switch (uart0_massage.cmd){
  349. case SLAVE_MCU_CMD_PARAM:{
  350. g_heat_ctrl.PresetTempVal = *(float *)&uart0_data[0];
  351. g_heat_ctrl.PresetTempTime = *(int *)&uart0_data[4];
  352. g_heat_ctrl.SetTempVal = *(float *)&uart0_data[8];
  353. g_heat_ctrl.HoldTemp = *(float *)&uart0_data[12];
  354. g_heat_ctrl.TempOffset = *(float *)&uart0_data[16];
  355. g_heat_ctrl.PreHeatTime = *(uint32_t *)&uart0_data[20];
  356. g_heat_ctrl.ReadyTempVal = *(float *)&uart0_data[24];
  357. if(g_heat_ctrl.PresetTempVal < 90 && g_heat_ctrl.SetTempVal < 90)
  358. {
  359. combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_PARAM,MINI7_TAIL_FALG)
  360. msg.len = strlen(sys_info);
  361. msg.data = sys_info;
  362. sendMsgtoMasterMcu(&msg);
  363. }
  364. }
  365. break;
  366. case SLAVE_MCU_CMD_HEAT:{
  367. g_heat_ctrl.heat_state = uart0_data[0];
  368. combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_HEAT,MINI7_TAIL_FALG)
  369. sendMsgtoMasterMcu(&msg);
  370. }
  371. break;
  372. case SLAVE_MCU_CMD_LIGHT_CTR:{
  373. memcpy(&light, uart0_data, sizeof(struct light_band_para_t));
  374. light_set(light.mode, light.speed, light.color, 0);
  375. memcpy(&light, uart0_data+sizeof(struct light_band_para_t), sizeof(struct light_band_para_t));
  376. light_set(light.mode, light.speed, light.color, 1);
  377. combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_LIGHT_CTR,MINI7_TAIL_FALG)
  378. sendMsgtoMasterMcu(&msg);
  379. }
  380. break;
  381. case SLAVE_MCU_CMD_QRSCANNER_CTR:{
  382. }
  383. break;
  384. case SLAVE_MCU_CMD_FAN:{
  385. g_heat_ctrl.fan_state = uart0_data[0];
  386. if (g_heat_ctrl.fan_state == 1)
  387. {
  388. PB4 = 1;
  389. }
  390. else{
  391. PB4 = 0;
  392. }
  393. combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_FAN,MINI7_TAIL_FALG)
  394. sendMsgtoMasterMcu(&msg);
  395. }
  396. break;
  397. case SLAVE_MCU_CMD_CARD_ST:{
  398. g_heat_ctrl.card_state = uart0_data[0];
  399. combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_CARD_ST,MINI7_TAIL_FALG)
  400. // /*进入稳定状态,调整目标温度*/
  401. if(g_heat_ctrl.card_state && uart0_massage.len >= 8){
  402. float cabinTem = *(float *)&uart0_data[4];
  403. float adj = *(float *)&uart0_data[8];
  404. if(cabinTem < ENV_ADJ_THRESHOLD){
  405. g_heat_ctrl.adJTarget = adj;
  406. }else{
  407. g_heat_ctrl.adJTarget = 0;
  408. }
  409. }
  410. sendMsgtoMasterMcu(&msg);
  411. }
  412. break;
  413. case SLAVE_MCU_CMD_PID:{
  414. kp = *(float *)&uart0_data[0];
  415. ki = *(float *)&uart0_data[4];
  416. kd = *(float *)&uart0_data[8];
  417. PID_set(&pid1, kp, ki, kd);
  418. combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_PID,MINI7_TAIL_FALG)
  419. sendMsgtoMasterMcu(&msg);
  420. }
  421. break;
  422. case SLAVE_MCU_UP_QR:{
  423. /*扫描结果上报成功,清除缓存*/
  424. memset(g_uart2_rx_buf, 0 ,sizeof(g_uart2_rx_buf));
  425. g_uart2_rx_len = 0;
  426. haveheaderflag = QR_REC_STATE_EMPTY;
  427. // light_set(MODE_NORMAL, 250, COLOR_YELLOW, 0);
  428. // light_set(MODE_NORMAL, 250, COLOR_YELLOW, 1);
  429. }
  430. break;
  431. case SLAVE_MCU_UP_QR_PARAM:{
  432. qr_check_delay = 0;
  433. haveheaderflag = QR_REC_STATE_SENDQR;
  434. // light_set(MODE_NORMAL, 250, COLOR_GREEN, 0);
  435. // light_set(MODE_NORMAL, 250, COLOR_GREEN, 1);
  436. }
  437. break;
  438. }
  439. }
  440. }
  441. /********************************************************************
  442. * heater_state_upload()
  443. *
  444. *描述:上传加热、风扇状态及加热体温度
  445. *参数:无
  446. *返回:0/1:设置失败/成功
  447. *其他:无
  448. *--------------------------------------------------------------------
  449. *记录:包含上报和应答处理
  450. ********************************************************************/
  451. void heater_state_upload(uint32_t upenv)
  452. {
  453. struct CmdMessage msg;
  454. uint8_t databuf[7]={0};
  455. combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_UP_HEAT,MINI7_TAIL_FALG)
  456. if(upenv){
  457. msg.len = sizeof(databuf);
  458. }else{
  459. msg.len = sizeof(databuf)-2;
  460. }
  461. msg.data = databuf;
  462. databuf[0] = g_heat_ctrl.heat_state;
  463. databuf[1] = g_heat_ctrl.temp_stable_sta;
  464. databuf[2] = (uint8_t)pid1.ActualTemp;
  465. databuf[3] = (uint8_t)((pid1.ActualTemp - (uint8_t)pid1.ActualTemp) * 100);
  466. databuf[4] = g_heat_ctrl.fan_state;
  467. databuf[5] = (uint8_t)pid1.cabinTem;
  468. databuf[6] = (uint8_t)((pid1.cabinTem - (uint8_t)pid1.cabinTem) * 100);
  469. sendMsgtoMasterMcu(&msg);
  470. }
  471. void sendInfoMasterMcu(uint8_t *data, uint16_t len)
  472. {
  473. struct CmdMessage msg;
  474. combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_INFO,MINI7_TAIL_FALG)
  475. msg.len = len;
  476. msg.data = data;
  477. sendMsgtoMasterMcu(&msg);
  478. }
  479. /********************************************************************
  480. * uart2_rcv_frame_analysis()
  481. *
  482. *描述:接收帧解析
  483. *参数:无
  484. *返回:无
  485. *其他:无
  486. *--------------------------------------------------------------------
  487. *记录:
  488. ********************************************************************/
  489. void send_param(void){
  490. struct CmdMessage msg;
  491. combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_UP_QR_PARAM,MINI7_TAIL_FALG)
  492. msg.len = sizeof(m_str_card_param_info);
  493. msg.data = (char*)&m_str_card_param_info;
  494. sendMsgtoMasterMcu(&msg);
  495. }
  496. void send_QR_inf(void){
  497. struct CmdMessage msg;
  498. combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_UP_QR,MINI7_TAIL_FALG)
  499. msg.len = g_uart2_rx_len;
  500. msg.data = g_uart2_rx_buf;
  501. sendMsgtoMasterMcu(&msg);
  502. }
  503. /*上报二维码信息*/
  504. void sendQR_inf(void){
  505. if((haveheaderflag == QR_REC_STATE_SENDPRAM) && (qr_check_delay%2==0)){
  506. send_param();
  507. }
  508. if((haveheaderflag == QR_REC_STATE_SENDQR) && (qr_check_delay%2==0)){
  509. send_QR_inf();
  510. }
  511. }
  512. /*发送完成,清除标记,清除缓冲区*/
  513. //void sendQR_send_ok(void)
  514. //{
  515. // memset(g_uart2_rx_buf, 0 ,sizeof(g_uart2_rx_buf));
  516. // g_uart2_rx_len = 0;
  517. // qr_check_delay = QR_CHECH_STATE_FIRST;
  518. //}
  519. void uart2_rcv_frame_analysis(void)
  520. {
  521. if(haveheaderflag <= QR_REC_STATE_HEADER){
  522. if (('{' == g_uart2_rx_buf[0])&&(g_uart2_rx_len>10))
  523. {
  524. if(haveheaderflag != QR_REC_STATE_HEADER){
  525. haveheaderflag = QR_REC_STATE_HEADER;
  526. qr_check_delay = 0;
  527. // light_set(MODE_NORMAL, 250, COLOR_RED, 0);
  528. // light_set(MODE_NORMAL, 250, COLOR_RED, 1);
  529. }
  530. if(0 == parse_qrcode_info(&m_str_card_param_info, g_uart2_rx_buf)){
  531. haveheaderflag = QR_REC_STATE_SENDPRAM;
  532. qr_check_delay = 0;
  533. // light_set(MODE_NORMAL, 250, COLOR_BLUE, 0);
  534. // light_set(MODE_NORMAL, 250, COLOR_BLUE, 1);
  535. }
  536. }
  537. if((qr_check_delay == QR_CHECH_STATE_WAITING) ||('{' != g_uart2_rx_buf[0] && g_uart2_rx_len > 0)){
  538. /*码头为 '{' 延时解码不对,清空 */
  539. /*解码完成 */
  540. /*码头不为 '{' 非法,清空*/
  541. memset(g_uart2_rx_buf, 0 ,sizeof(g_uart2_rx_buf));
  542. g_uart2_rx_len = 0;
  543. qr_check_delay = 0;
  544. haveheaderflag = QR_REC_STATE_EMPTY;
  545. }
  546. }
  547. }
  548. /********************************************************************
  549. * trigmode_select_sensor()
  550. *
  551. *描述:切换为感应触发模式
  552. *参数:无
  553. *返回:0/1:设置失败/成功
  554. *其他:无
  555. *--------------------------------------------------------------------
  556. *记录:包含上报和应答处理
  557. ********************************************************************/
  558. void trigmode_select_sensor(void)
  559. {
  560. memset(g_uart2_tx_buf, 0, sizeof(g_uart2_tx_buf));
  561. // memset(g_uart2_rx_buf, 0, sizeof(g_uart2_rx_buf));
  562. g_uart2_tx_buf[0] = 0x7E; // 帧头
  563. g_uart2_tx_buf[1] = 0x00; // 帧头
  564. g_uart2_tx_buf[2] = 0x08; // 命令标识
  565. g_uart2_tx_buf[3] = 0x01; // 命令标识
  566. g_uart2_tx_buf[4] = 0x00;
  567. g_uart2_tx_buf[5] = 0x00;
  568. g_uart2_tx_buf[6] = 0xD7;
  569. // g_uart2_tx_buf[6] = 0xEF;
  570. g_uart2_tx_buf[7] = 0xAB;
  571. g_uart2_tx_buf[8] = 0xCD;
  572. UART_Write(UART2, g_uart2_tx_buf, 9);
  573. }
  574. /********************************************************************
  575. * data_out_oragin()
  576. *
  577. *描述:切换为原始数据输出
  578. *参数:无
  579. *返回:0/1:设置失败/成功
  580. *其他:无
  581. *--------------------------------------------------------------------
  582. *记录:包含上报和应答处理
  583. ********************************************************************/
  584. void data_out_original(void)
  585. {
  586. memset(g_uart2_tx_buf, 0, sizeof(g_uart2_tx_buf));
  587. g_uart2_tx_buf[0] = 0x7E; // 帧头
  588. g_uart2_tx_buf[1] = 0x00; // 帧头
  589. g_uart2_tx_buf[2] = 0x08; // 命令标识
  590. g_uart2_tx_buf[3] = 0x01; // 命令标识
  591. g_uart2_tx_buf[4] = 0x00;
  592. g_uart2_tx_buf[5] = 0x0D;
  593. // g_uart0_tx_buf[6] = 0xD7;
  594. g_uart2_tx_buf[6] = 0xCA;
  595. g_uart2_tx_buf[7] = 0xAB;
  596. g_uart2_tx_buf[8] = 0xCD;
  597. UART_Write(UART2, g_uart2_tx_buf, 9);
  598. HAL_Delay(100);
  599. memset(g_uart2_rx_buf, 0, sizeof(g_uart2_rx_buf));
  600. g_uart2_rx_len = 0;
  601. }
  602. int parse_qrcode_info(str_card_param_info_t *info, char *qrcode_str)
  603. {
  604. int id = 0;
  605. double Tt = 0;
  606. double Tt1 = 0;
  607. if (info == 0)
  608. {
  609. return -1;
  610. }
  611. cJSON *root = cJSON_Parse(qrcode_str);
  612. if (root == NULL)
  613. { /*消息不合格*/
  614. return -1;
  615. }
  616. cJSON *item = cJSON_GetObjectItem(root, "p");
  617. if (item == NULL)
  618. { /*消息不合格*/
  619. cJSON_Delete(root);
  620. return -1;
  621. }
  622. char *pid = cJSON_GetStringValue(item);
  623. // printf("pid=%s \r\n", pid);
  624. memcpy(info->pid, pid, 10);
  625. item = cJSON_GetObjectItem(root, "t");
  626. if (item == NULL)
  627. { /*消息不合格*/
  628. cJSON_Delete(root);
  629. return -1;
  630. }
  631. double t = item->valuedouble;
  632. // printf("t=%f \r\n", t);
  633. info->temp[0] = (uint8_t)t;
  634. info->temp[1] = (uint8_t)((t -(uint8_t)t)* 100);
  635. // 获取数组item
  636. item = cJSON_GetObjectItem(root, "e");
  637. if (item == NULL)
  638. { /*消息不合格*/
  639. cJSON_Delete(root);
  640. return -1;
  641. }
  642. int test_arry_size = cJSON_GetArraySize(item);
  643. for (int i = 0; i < test_arry_size; i++)
  644. {
  645. // 打印数组里的所有item
  646. cJSON *sub_array = cJSON_GetArrayItem(item, i);
  647. cJSON *sub = cJSON_GetObjectItem(sub_array, "i");
  648. id = sub->valueint;
  649. // printf("id=%d \r\n", id);
  650. if(id > 0)
  651. {
  652. sub = cJSON_GetObjectItemCaseSensitive(sub_array, "T");
  653. Tt = sub->valuedouble;
  654. // printf("Tt=%f \r\n", Tt);
  655. sub = cJSON_GetObjectItemCaseSensitive(sub_array, "t");
  656. Tt1 = sub->valuedouble;
  657. // printf("Tt1=%f \r\n", Tt1);
  658. }
  659. else
  660. {
  661. Tt = 0;
  662. Tt1 = 0;
  663. }
  664. info->hole_info[i].item = id;
  665. info->hole_info[i].TtMax[0] = (uint8_t)Tt;
  666. info->hole_info[i].TtMax[1] = (uint8_t)((Tt -(uint8_t)Tt)* 100);
  667. info->hole_info[i].TtMin[0] = (uint8_t)Tt1;
  668. info->hole_info[i].TtMin[1] = (uint8_t)((Tt1 -(uint8_t)Tt1)* 100);
  669. }
  670. // 记得删除json
  671. cJSON_Delete(root);
  672. return 0;
  673. }
  674. /********************************************************************
  675. * QRparam_upload()
  676. *
  677. *描述:上传二维码扫码参数
  678. *参数:无
  679. *返回:无
  680. *其他:无
  681. *--------------------------------------------------------------------
  682. *记录:包含上报和应答处理
  683. ********************************************************************/
  684. void QRparam_upload(str_card_param_info_t *p_str_card_param_info)
  685. {
  686. memset(g_uart0_tx_buf, 0, sizeof(g_uart0_tx_buf));
  687. // int QR_param_pid = 0;
  688. // QR_param_pid = atoi(&m_str_card_param_info.pid[0]);
  689. g_uart0_tx_buf[0] = 0xAA; // 帧头
  690. g_uart0_tx_buf[1] = 0x55; // 帧头
  691. g_uart0_tx_buf[2] = 0x0A; // 命令标识
  692. g_uart0_tx_buf[3] = 0x11; // 命令标识
  693. #if 0
  694. /*卡盒编号*/
  695. g_uart0_tx_buf[4] = (uint8_t)m_str_card_param_info.pid[0] - 48;
  696. g_uart0_tx_buf[5] = (uint8_t)m_str_card_param_info.pid[1] - 48;
  697. g_uart0_tx_buf[6] = (uint8_t)m_str_card_param_info.pid[2] - 48;
  698. g_uart0_tx_buf[7] = (uint8_t)m_str_card_param_info.pid[3] - 48;
  699. g_uart0_tx_buf[8] = (uint8_t)m_str_card_param_info.pid[4] - 48;
  700. g_uart0_tx_buf[9] = (uint8_t)m_str_card_param_info.pid[5] - 48;
  701. /*卡槽工作温度(整数+小数)*/
  702. g_uart0_tx_buf[10] = (uint8_t)(m_str_card_param_info.temp);
  703. g_uart0_tx_buf[11] = (uint8_t)((m_str_card_param_info.temp-(uint8_t)(m_str_card_param_info.temp))*100);
  704. /*孔1检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/
  705. g_uart0_tx_buf[12] = (uint8_t)(m_str_card_param_info.hole_info[0].item);
  706. g_uart0_tx_buf[13] = (uint8_t)(m_str_card_param_info.hole_info[0].TtMax);
  707. g_uart0_tx_buf[14] = (uint8_t)((m_str_card_param_info.hole_info[0].TtMax-(uint8_t)(m_str_card_param_info.hole_info[0].TtMax))*100);
  708. g_uart0_tx_buf[15] = (uint8_t)(m_str_card_param_info.hole_info[0].TtMin);
  709. g_uart0_tx_buf[16] = (uint8_t)((m_str_card_param_info.hole_info[0].TtMin-(uint8_t)(m_str_card_param_info.hole_info[0].TtMin))*100);
  710. /*孔2检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/
  711. g_uart0_tx_buf[17] = (uint8_t)(m_str_card_param_info.hole_info[1].item);
  712. g_uart0_tx_buf[18] = (uint8_t)(m_str_card_param_info.hole_info[1].TtMax);
  713. g_uart0_tx_buf[19] = (uint8_t)((m_str_card_param_info.hole_info[1].TtMax-(uint8_t)(m_str_card_param_info.hole_info[1].TtMax))*100);
  714. g_uart0_tx_buf[20] = (uint8_t)(m_str_card_param_info.hole_info[1].TtMin);
  715. g_uart0_tx_buf[21] = (uint8_t)((m_str_card_param_info.hole_info[1].TtMin-(uint8_t)(m_str_card_param_info.hole_info[1].TtMin))*100);
  716. /*孔3检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/
  717. g_uart0_tx_buf[22] = (uint8_t)(m_str_card_param_info.hole_info[2].item);
  718. g_uart0_tx_buf[23] = (uint8_t)(m_str_card_param_info.hole_info[2].TtMax);
  719. g_uart0_tx_buf[24] = (uint8_t)((m_str_card_param_info.hole_info[2].TtMax-(uint8_t)(m_str_card_param_info.hole_info[2].TtMax))*100);
  720. g_uart0_tx_buf[25] = (uint8_t)(m_str_card_param_info.hole_info[2].TtMin);
  721. g_uart0_tx_buf[26] = (uint8_t)((m_str_card_param_info.hole_info[2].TtMin-(uint8_t)(m_str_card_param_info.hole_info[2].TtMin))*100);
  722. /*孔4检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/
  723. g_uart0_tx_buf[27] = (uint8_t)(m_str_card_param_info.hole_info[3].item);
  724. g_uart0_tx_buf[28] = (uint8_t)(m_str_card_param_info.hole_info[3].TtMax);
  725. g_uart0_tx_buf[29] = (uint8_t)((m_str_card_param_info.hole_info[3].TtMax-(uint8_t)(m_str_card_param_info.hole_info[3].TtMax))*100);
  726. g_uart0_tx_buf[30] = (uint8_t)(m_str_card_param_info.hole_info[3].TtMin);
  727. g_uart0_tx_buf[31] = (uint8_t)((m_str_card_param_info.hole_info[3].TtMin-(uint8_t)(m_str_card_param_info.hole_info[3].TtMin))*100);
  728. /*孔5检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/
  729. g_uart0_tx_buf[32] = (uint8_t)(m_str_card_param_info.hole_info[4].item);
  730. g_uart0_tx_buf[33] = (uint8_t)(m_str_card_param_info.hole_info[4].TtMax);
  731. g_uart0_tx_buf[34] = (uint8_t)((m_str_card_param_info.hole_info[4].TtMax-(uint8_t)(m_str_card_param_info.hole_info[4].TtMax))*100);
  732. g_uart0_tx_buf[35] = (uint8_t)(m_str_card_param_info.hole_info[4].TtMin);
  733. g_uart0_tx_buf[36] = (uint8_t)((m_str_card_param_info.hole_info[4].TtMin-(uint8_t)(m_str_card_param_info.hole_info[4].TtMin))*100);
  734. /*孔6检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/
  735. g_uart0_tx_buf[37] = (uint8_t)(m_str_card_param_info.hole_info[5].item);
  736. g_uart0_tx_buf[38] = (uint8_t)(m_str_card_param_info.hole_info[5].TtMax);
  737. g_uart0_tx_buf[39] = (uint8_t)((m_str_card_param_info.hole_info[5].TtMax-(uint8_t)(m_str_card_param_info.hole_info[5].TtMax))*100);
  738. g_uart0_tx_buf[40] = (uint8_t)(m_str_card_param_info.hole_info[5].TtMin);
  739. g_uart0_tx_buf[41] = (uint8_t)((m_str_card_param_info.hole_info[5].TtMin-(uint8_t)(m_str_card_param_info.hole_info[5].TtMin))*100);
  740. /*孔7检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/
  741. g_uart0_tx_buf[42] = (uint8_t)(m_str_card_param_info.hole_info[6].item);
  742. g_uart0_tx_buf[43] = (uint8_t)(m_str_card_param_info.hole_info[6].TtMax);
  743. g_uart0_tx_buf[44] = (uint8_t)((m_str_card_param_info.hole_info[6].TtMax-(uint8_t)(m_str_card_param_info.hole_info[6].TtMax))*100);
  744. g_uart0_tx_buf[45] = (uint8_t)(m_str_card_param_info.hole_info[6].TtMin);
  745. g_uart0_tx_buf[46] = (uint8_t)((m_str_card_param_info.hole_info[6].TtMin-(uint8_t)(m_str_card_param_info.hole_info[6].TtMin))*100);
  746. /*孔8检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/
  747. g_uart0_tx_buf[47] = (uint8_t)(m_str_card_param_info.hole_info[7].item);
  748. g_uart0_tx_buf[48] = (uint8_t)(m_str_card_param_info.hole_info[7].TtMax);
  749. g_uart0_tx_buf[49] = (uint8_t)((m_str_card_param_info.hole_info[7].TtMax-(uint8_t)(m_str_card_param_info.hole_info[7].TtMax))*100);
  750. g_uart0_tx_buf[50] = (uint8_t)(m_str_card_param_info.hole_info[7].TtMin);
  751. g_uart0_tx_buf[51] = (uint8_t)((m_str_card_param_info.hole_info[7].TtMin-(uint8_t)(m_str_card_param_info.hole_info[7].TtMin))*100);
  752. /*孔9检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/
  753. g_uart0_tx_buf[52] = (uint8_t)(m_str_card_param_info.hole_info[8].item);
  754. g_uart0_tx_buf[53] = (uint8_t)(m_str_card_param_info.hole_info[8].TtMax);
  755. g_uart0_tx_buf[54] = (uint8_t)((m_str_card_param_info.hole_info[8].TtMax-(uint8_t)(m_str_card_param_info.hole_info[8].TtMax))*100);
  756. g_uart0_tx_buf[55] = (uint8_t)(m_str_card_param_info.hole_info[8].TtMin);
  757. g_uart0_tx_buf[56] = (uint8_t)((m_str_card_param_info.hole_info[8].TtMin-(uint8_t)(m_str_card_param_info.hole_info[8].TtMin))*100);
  758. /*孔10检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/
  759. g_uart0_tx_buf[57] = (uint8_t)(m_str_card_param_info.hole_info[9].item);
  760. g_uart0_tx_buf[58] = (uint8_t)(m_str_card_param_info.hole_info[9].TtMax);
  761. g_uart0_tx_buf[59] = (uint8_t)((m_str_card_param_info.hole_info[9].TtMax-(uint8_t)(m_str_card_param_info.hole_info[9].TtMax))*100);
  762. g_uart0_tx_buf[60] = (uint8_t)(m_str_card_param_info.hole_info[9].TtMin);
  763. g_uart0_tx_buf[61] = (uint8_t)((m_str_card_param_info.hole_info[9].TtMin-(uint8_t)(m_str_card_param_info.hole_info[9].TtMin))*100);
  764. #endif
  765. memcpy(g_uart0_tx_buf + 4, (uint8_t*)p_str_card_param_info, sizeof(str_card_param_info_t));
  766. g_uart0_tx_buf[62] = 0x55; // 帧尾
  767. g_uart0_tx_buf[63] = 0xBB; // 帧尾
  768. UART_Write(UART0, g_uart0_tx_buf, 64);
  769. }