/* * uart_rcv.c * * Created on: 2022年4月20日 * Author: dell */ #include #include #include #include #include "NuMicro.h" #include "cfg_param_record.h" #include "uart_rcv.h" #include "usr_pid.h" #include "cJSON.h" #include "ws2812.h" #define ENV_ADJ_THRESHOLD 27.0 enum SLAVE_MCU_Ctr_cmd_type { SLAVE_MCU_CMD_PARAM = 0x01, SLAVE_MCU_CMD_HEAT = 0x02, SLAVE_MCU_CMD_LIGHT_CTR= 0X03, SLAVE_MCU_CMD_QRSCANNER_CTR= 0X04, SLAVE_MCU_CMD_FAN= 0X05, SLAVE_MCU_CMD_CARD_ST= 0X06, SLAVE_MCU_CMD_PID = 0x20, }; enum SLAVE_MCU_Ctr_UP_type { SLAVE_MCU_UP_HEAT = 0x10, SLAVE_MCU_UP_QR_PARAM= 0X11, SLAVE_MCU_UP_QR = 0x12, SLAVE_MCU_INFO, //异常信息 }; #define combine_cmdMessage(msg,header,seqer,cmder,tailer) { \ msg.head = header; \ msg.seq = seqer; \ msg.cmd = cmder; \ msg.tail = tailer; \ } uint8_t g_uart2_rcv_start = 0; uint8_t g_uart2_rcv_complete_flg = 0; uint16_t g_uart2_rx_len = 0; uint8_t g_uart2_frame_overtime_cnt = 0; uint16_t g_uart2_rcv_cmd = 0; uint16_t g_uart2_cmd_overtime_secs = 0; uint32_t g_uart2_cmd_overtime_cnt = 0; uint8_t g_param_set_mode = 0; // 参数设置模式 uint8_t g_uart1_rcv_start = 0; uint8_t g_uart1_rcv_complete_flg = 0; uint8_t g_uart1_tx_buf[BUF_SIZE] = {0}; uint8_t g_uart1_rx_buf[BUF_SIZE] = {0}; uint8_t g_uart1_rx_len = 0; uint8_t g_uart1_frame_overtime_cnt = 0; uint8_t g_uart0_rcv_start = 0; uint8_t g_uart0_rcv_complete_flg = 0; uint8_t g_uart0_tx_buf[DEBUG_BUF_SIZE] = {0}; uint8_t g_uart0_rx_buf[BUF_SIZE] = {0}; uint8_t g_uart0_rx_len = 0; uint8_t g_uart0_frame_overtime_cnt = 0; uint32_t uart0_header = 0; uint32_t uart0_last = 0; unsigned int haveheaderflag = 0; heat_ctrl_struct g_heat_ctrl = {0}; // card_param_struct card = {0}; str_hole_info_t hole1 = {0}; str_hole_info_t hole2 = {0}; str_hole_info_t hole3 = {0}; str_hole_info_t hole4 = {0}; str_hole_info_t hole5 = {0}; str_hole_info_t hole6 = {0}; str_hole_info_t hole7 = {0}; str_hole_info_t hole8 = {0}; str_hole_info_t hole9 = {0}; str_hole_info_t hole10 = {0}; str_card_param_info_t m_str_card_param_info = {0}; uint32_t qr_check_delay = QR_CHECH_STATE_FIRST; /*1s定时器里面,大于0,每s减1*/ extern void QRparam_upload(str_card_param_info_t *p_str_card_param_info); extern uint8_t sys_info[40]; /******************************************************************** * bcd_to_hex() * *描述:BCD转HEX *参数:无 *返回:无 *其他:无 *-------------------------------------------------------------------- *记录: ********************************************************************/ uint8_t bcd_to_hex(uint8_t val) { return ((val / 10) << 4) + val % 10; } /******************************************************************** * debug_receive_int_handle() * *描述:调试串口接收中断数据处理 *参数:无 *返回:无 *其他:无 *-------------------------------------------------------------------- *记录: ********************************************************************/ void debug_receive_int_handle0(void) { if (UART_GET_INT_FLAG(UART0, UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk)) { /* UART receive data available flag */ /* Move the data from Rx FIFO to sw buffer (RAM). */ /* Every time leave 1 byte data in FIFO for Rx timeout */ while (UART_GET_RX_EMPTY(UART0) == 0) { if (uart0_last == BUF_SIZE)uart0_last = 0; g_uart0_rx_buf[uart0_last] = UART_READ(UART0); uart0_last++; } } } void debug_receive_int_handle1(void) { while (UART_GET_RX_EMPTY(UART1) == 0) { if (g_uart1_rx_len >= BUF_SIZE) g_uart1_rx_len = 0; g_uart1_rx_buf[g_uart1_rx_len] = UART_READ(UART1); g_uart1_rx_len++; } } /******************************************************************** * UART02_IRQHandler() * *描述:串口接收中断 *参数:无 *返回:无 *其他:无 *-------------------------------------------------------------------- *记录: ********************************************************************/ void UART02_IRQHandler(void) { if (UART_GET_INT_FLAG(UART0, UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk | UART_INTSTS_RXTOIF_Msk) ) { debug_receive_int_handle0(); } else if (UART_GET_INT_FLAG(UART0,UART_INTSTS_BUFERRIF_Msk) ) { debug_receive_int_handle0(); UART_ClearIntFlag(UART0,UART_INTSTS_BUFERRINT_Msk); } else if (UART_GET_INT_FLAG(UART0,UART_INTSTS_RLSINT_Msk) ) { UART_ClearIntFlag(UART0,UART_INTSTS_RLSINT_Msk); } if (UART_GET_INT_FLAG(UART2, UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk | UART_INTSTS_RXTOIF_Msk) ) { debug_receive_int_handle2(); } else if (UART_GET_INT_FLAG(UART2,UART_INTSTS_BUFERRIF_Msk) ) { debug_receive_int_handle2(); UART_ClearIntFlag(UART2,UART_INTSTS_BUFERRINT_Msk); } else if (UART_GET_INT_FLAG(UART2,UART_INTSTS_RLSINT_Msk) ) { UART_ClearIntFlag(UART2,UART_INTSTS_RLSINT_Msk); } } /******************************************************************** * UART13_IRQHandler() * *描述:串口接收中断 *参数:无 *返回:无 *其他:无 *-------------------------------------------------------------------- *记录: ********************************************************************/ void UART13_IRQHandler(void) { if (UART_GET_INT_FLAG(UART1, UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk | UART_INTSTS_RXTOIF_Msk) ) { debug_receive_int_handle1(); } else if (UART_GET_INT_FLAG(UART1,UART_INTSTS_BUFERRIF_Msk) ) { debug_receive_int_handle1(); UART_ClearIntFlag(UART1,UART_INTSTS_BUFERRINT_Msk); } else if (UART_GET_INT_FLAG(UART1,UART_INTSTS_RLSINT_Msk) ) { UART_ClearIntFlag(UART1,UART_INTSTS_RLSINT_Msk); } } void sendMsgtoMasterMcu(struct CmdMessage *pmsg) { static uint8_t sedbuf[DEBUG_BUF_SIZE]; uint16_t checksum = 0; *(unsigned short *)sedbuf = pmsg->head; sedbuf[2] = pmsg->seq; sedbuf[3] = pmsg->cmd; *(unsigned short *)(sedbuf+4) = pmsg->len; for(int i=0; i < pmsg->len; i++){ sedbuf[6+i] = pmsg->data[i]; } for(int i=2; i < (pmsg->len + 6); i++){ checksum += sedbuf[i]; } sedbuf[6+pmsg->len] = checksum&0xff;/*结尾标记高位*/ sedbuf[7+pmsg->len ] = checksum>>8 ;/*结尾标记低位*/ UART_Write(UART0, sedbuf, (8+pmsg->len)); } uint16_t checkSum(const uint8_t *pInBuffer, int uLen) { uint32_t sum = 0; for(int i = 0; i < uLen; i++) { sum += pInBuffer[i]; } return (sum); } /******************************************************************** * int Decode(const char *pInBuffer, int uLen) * *描述:对串口数据进行解码成结构体 *参数:pInBuffer 接收缓冲区 *uheader 当前头位置 ulast 当前头尾位置 maxlen 缓冲区长度 pMessage 命令结构体 pdata 命令数据 *返回:1解码成功,0解码失败 *其他:无 *-------------------------------------------------------------------- *记录: ********************************************************************/ int DecodeNew(struct CmdMessage *pMessage, uint8_t *pdata, const uint8_t *pInBuffer, uint32_t* pheader,uint32_t ulast,uint32_t maxlen) { uint32_t bufRealLen; uint32_t sizeofMessage = MINI7_MASSAGE_SIZE; uint32_t header = *pheader; uint32_t i = 0; if (NULL == pInBuffer || NULL == pMessage || NULL == pdata || NULL == pheader) { /*空指针判断*/ return 0; } /*缓冲区有效数据长度,缓冲区循环处理,会出现头大于未情况*/ bufRealLen = (ulast > header)?(ulast - header):(ulast + maxlen - header); if(bufRealLen < sizeofMessage){ /*长度过段,跳过*/ return 0; } bufRealLen -= sizeofMessage; /*最大有效数据长度*/ for(; i<=bufRealLen;){ uint32_t headerflag = pInBuffer[(header + i)%maxlen]; headerflag += pInBuffer[(header + i + 1)%maxlen] <<8; if(headerflag != MINI7_HEADER_FALG){ i += 1; // header = (header + 1)%maxlen; continue; } uint32_t messagelen = pInBuffer[(header + i + 4)%maxlen]; messagelen += pInBuffer[(header + i + 5)%maxlen] <<8; if(messagelen > (maxlen- sizeofMessage)){ /*长度超长 跳过2个字节*/ i += 1; // header = (header + 1)%maxlen; continue; } if(messagelen >bufRealLen){ /*当前起始点可能存在数据,跳出循环,*/ i++; /*for采样小于等于判断,循环次数多1,此处加1便于后续处理*/ break; } uint16_t checkesum = 0; uint16_t recsum = 0; if((header + i + 6 + messagelen) < maxlen){ checkesum = checkSum(pInBuffer + header + i + 2, messagelen+4); }else{ checkesum = checkSum(pInBuffer + header + i + 2, (maxlen - (header + i + 2))); checkesum += checkSum(pInBuffer, (header + i + 6 + messagelen)%maxlen); } recsum = *(pInBuffer + (header + i + 6 + messagelen)%maxlen ) ; recsum += *(pInBuffer + (header + i + 7 + messagelen)%maxlen) << 8; /*固定结尾预留做调试用*/ if(checkesum != recsum && recsum>>8 != MINI7_TAIL_FALG){ i += 1; // ? ? ? ? ?header = (header + 1)%maxlen; }else{ /*解析成功,拷贝消息结构体*/ uint8_t * pCharMessage = (uint8_t *)pMessage; for(unsigned copyi=0;copyi<(sizeofMessage-2);copyi++){ *pCharMessage++ = pInBuffer[(header + i+ copyi)%maxlen]; } (*pMessage).tail = MINI7_TAIL_FALG; /*解析成功,拷贝数据结构体*/ for(unsigned copyi=0;copyipid, pid, 10); item = cJSON_GetObjectItem(root, "t"); if (item == NULL) { /*消息不合格*/ cJSON_Delete(root); return -1; } double t = item->valuedouble; // printf("t=%f \r\n", t); info->temp[0] = (uint8_t)t; info->temp[1] = (uint8_t)((t -(uint8_t)t)* 100); // 获取数组item item = cJSON_GetObjectItem(root, "e"); if (item == NULL) { /*消息不合格*/ cJSON_Delete(root); return -1; } int test_arry_size = cJSON_GetArraySize(item); for (int i = 0; i < test_arry_size; i++) { // 打印数组里的所有item cJSON *sub_array = cJSON_GetArrayItem(item, i); cJSON *sub = cJSON_GetObjectItem(sub_array, "i"); id = sub->valueint; // printf("id=%d \r\n", id); if(id > 0) { sub = cJSON_GetObjectItemCaseSensitive(sub_array, "T"); Tt = sub->valuedouble; // printf("Tt=%f \r\n", Tt); sub = cJSON_GetObjectItemCaseSensitive(sub_array, "t"); Tt1 = sub->valuedouble; // printf("Tt1=%f \r\n", Tt1); } else { Tt = 0; Tt1 = 0; } info->hole_info[i].item = id; info->hole_info[i].TtMax[0] = (uint8_t)Tt; info->hole_info[i].TtMax[1] = (uint8_t)((Tt -(uint8_t)Tt)* 100); info->hole_info[i].TtMin[0] = (uint8_t)Tt1; info->hole_info[i].TtMin[1] = (uint8_t)((Tt1 -(uint8_t)Tt1)* 100); } // 记得删除json cJSON_Delete(root); return 0; } /******************************************************************** * QRparam_upload() * *描述:上传二维码扫码参数 *参数:无 *返回:无 *其他:无 *-------------------------------------------------------------------- *记录:包含上报和应答处理 ********************************************************************/ void QRparam_upload(str_card_param_info_t *p_str_card_param_info) { memset(g_uart0_tx_buf, 0, sizeof(g_uart0_tx_buf)); // int QR_param_pid = 0; // QR_param_pid = atoi(&m_str_card_param_info.pid[0]); g_uart0_tx_buf[0] = 0xAA; // 帧头 g_uart0_tx_buf[1] = 0x55; // 帧头 g_uart0_tx_buf[2] = 0x0A; // 命令标识 g_uart0_tx_buf[3] = 0x11; // 命令标识 #if 0 /*卡盒编号*/ g_uart0_tx_buf[4] = (uint8_t)m_str_card_param_info.pid[0] - 48; g_uart0_tx_buf[5] = (uint8_t)m_str_card_param_info.pid[1] - 48; g_uart0_tx_buf[6] = (uint8_t)m_str_card_param_info.pid[2] - 48; g_uart0_tx_buf[7] = (uint8_t)m_str_card_param_info.pid[3] - 48; g_uart0_tx_buf[8] = (uint8_t)m_str_card_param_info.pid[4] - 48; g_uart0_tx_buf[9] = (uint8_t)m_str_card_param_info.pid[5] - 48; /*卡槽工作温度(整数+小数)*/ g_uart0_tx_buf[10] = (uint8_t)(m_str_card_param_info.temp); g_uart0_tx_buf[11] = (uint8_t)((m_str_card_param_info.temp-(uint8_t)(m_str_card_param_info.temp))*100); /*孔1检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/ g_uart0_tx_buf[12] = (uint8_t)(m_str_card_param_info.hole_info[0].item); g_uart0_tx_buf[13] = (uint8_t)(m_str_card_param_info.hole_info[0].TtMax); 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); g_uart0_tx_buf[15] = (uint8_t)(m_str_card_param_info.hole_info[0].TtMin); 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); /*孔2检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/ g_uart0_tx_buf[17] = (uint8_t)(m_str_card_param_info.hole_info[1].item); g_uart0_tx_buf[18] = (uint8_t)(m_str_card_param_info.hole_info[1].TtMax); 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); g_uart0_tx_buf[20] = (uint8_t)(m_str_card_param_info.hole_info[1].TtMin); 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); /*孔3检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/ g_uart0_tx_buf[22] = (uint8_t)(m_str_card_param_info.hole_info[2].item); g_uart0_tx_buf[23] = (uint8_t)(m_str_card_param_info.hole_info[2].TtMax); 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); g_uart0_tx_buf[25] = (uint8_t)(m_str_card_param_info.hole_info[2].TtMin); 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); /*孔4检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/ g_uart0_tx_buf[27] = (uint8_t)(m_str_card_param_info.hole_info[3].item); g_uart0_tx_buf[28] = (uint8_t)(m_str_card_param_info.hole_info[3].TtMax); 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); g_uart0_tx_buf[30] = (uint8_t)(m_str_card_param_info.hole_info[3].TtMin); 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); /*孔5检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/ g_uart0_tx_buf[32] = (uint8_t)(m_str_card_param_info.hole_info[4].item); g_uart0_tx_buf[33] = (uint8_t)(m_str_card_param_info.hole_info[4].TtMax); 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); g_uart0_tx_buf[35] = (uint8_t)(m_str_card_param_info.hole_info[4].TtMin); 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); /*孔6检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/ g_uart0_tx_buf[37] = (uint8_t)(m_str_card_param_info.hole_info[5].item); g_uart0_tx_buf[38] = (uint8_t)(m_str_card_param_info.hole_info[5].TtMax); 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); g_uart0_tx_buf[40] = (uint8_t)(m_str_card_param_info.hole_info[5].TtMin); 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); /*孔7检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/ g_uart0_tx_buf[42] = (uint8_t)(m_str_card_param_info.hole_info[6].item); g_uart0_tx_buf[43] = (uint8_t)(m_str_card_param_info.hole_info[6].TtMax); 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); g_uart0_tx_buf[45] = (uint8_t)(m_str_card_param_info.hole_info[6].TtMin); 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); /*孔8检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/ g_uart0_tx_buf[47] = (uint8_t)(m_str_card_param_info.hole_info[7].item); g_uart0_tx_buf[48] = (uint8_t)(m_str_card_param_info.hole_info[7].TtMax); 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); g_uart0_tx_buf[50] = (uint8_t)(m_str_card_param_info.hole_info[7].TtMin); 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); /*孔9检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/ g_uart0_tx_buf[52] = (uint8_t)(m_str_card_param_info.hole_info[8].item); g_uart0_tx_buf[53] = (uint8_t)(m_str_card_param_info.hole_info[8].TtMax); 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); g_uart0_tx_buf[55] = (uint8_t)(m_str_card_param_info.hole_info[8].TtMin); 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); /*孔10检测项目ID、TtMax(整数+小数)、TtMin(整数+小数)*/ g_uart0_tx_buf[57] = (uint8_t)(m_str_card_param_info.hole_info[9].item); g_uart0_tx_buf[58] = (uint8_t)(m_str_card_param_info.hole_info[9].TtMax); 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); g_uart0_tx_buf[60] = (uint8_t)(m_str_card_param_info.hole_info[9].TtMin); 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); #endif memcpy(g_uart0_tx_buf + 4, (uint8_t*)p_str_card_param_info, sizeof(str_card_param_info_t)); g_uart0_tx_buf[62] = 0x55; // 帧尾 g_uart0_tx_buf[63] = 0xBB; // 帧尾 UART_Write(UART0, g_uart0_tx_buf, 64); }