uart_rcv.c 28 KB

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