/*
 * uart_rcv.c
 *
 *  Created on: 2022年4月20日
 *      Author: dell
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#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;
uint8_t g_uart2_rx_buf[DEBUG_BUF_SIZE] = {0};
uint8_t g_uart2_tx_buf[BUF_SIZE] = {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;
struct CmdMessage  uart0_massage;
uint8_t  uart0_data[DEBUG_BUF_SIZE];
uint32_t  uart0_header = 0;
uint32_t  uart0_last = 0;
uint32_t  uart0_recOK = 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++;
	}
}

void debug_receive_int_handle2(void)
{
	while (UART_GET_RX_EMPTY(UART2) == 0)
	{
		unsigned char recChar = UART_READ(UART2);
		if(haveheaderflag <= QR_REC_STATE_HEADER){
			
			if (g_uart2_rx_len >= DEBUG_BUF_SIZE)
				g_uart2_rx_len = 0;
			g_uart2_rx_buf[g_uart2_rx_len++] = recChar;
		}
	}
}

/********************************************************************
 *                         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;copyi<messagelen;copyi++){
			*pdata++ = pInBuffer[(header + i + 6 + copyi)%maxlen];
		}
		*pheader = (header + messagelen + i + sizeofMessage - 2)%maxlen;
			return 1;
		}
	}
   *pheader = (header + i - 1)%maxlen;
   return 0;
}


/********************************************************************
 *                         uart0_rcv_frame_analysis()
 *
 *描述：接收帧解析
 *参数：无
 *返回：无
 *其他：无
 *--------------------------------------------------------------------
 *记录：
 ********************************************************************/
void uart0_rcv_data_analysis(void)
{
	float kp, ki, kd;
	uint8_t false = 0;
	struct CmdMessage msg;
	uint16_t result =0;
	struct light_band_para_t light;
	msg.len = 2;
	msg.data = (char*)&result;

	if(uart0_recOK == false){
		return;
	} 
	uart0_recOK = false;
	if(uart0_massage.seq == MINI7_MASSAGE_TYPE_MCUTOMCU){
		switch (uart0_massage.cmd){
			case SLAVE_MCU_CMD_PARAM:{
				g_heat_ctrl.PresetTempVal = *(float *)&uart0_data[0];
				g_heat_ctrl.PresetTempTime = *(int *)&uart0_data[4];
				g_heat_ctrl.SetTempVal = *(float *)&uart0_data[8];
				g_heat_ctrl.HoldTemp = *(float *)&uart0_data[12];
				g_heat_ctrl.TempOffset = *(float *)&uart0_data[16];		
				g_heat_ctrl.PreHeatTime = *(uint32_t *)&uart0_data[20];
				g_heat_ctrl.ReadyTempVal = *(float *)&uart0_data[24];	
				if(g_heat_ctrl.PresetTempVal < 90 && g_heat_ctrl.SetTempVal < 90)	
				{
					combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_PARAM,MINI7_TAIL_FALG)
					msg.len = strlen(sys_info);
					msg.data = sys_info;
					sendMsgtoMasterMcu(&msg);
				}	
			}
				break;
			case SLAVE_MCU_CMD_HEAT:{
				g_heat_ctrl.heat_state = uart0_data[0];			
				combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_HEAT,MINI7_TAIL_FALG)
				sendMsgtoMasterMcu(&msg);
			}
				break;
			case SLAVE_MCU_CMD_LIGHT_CTR:{
				memcpy(&light, uart0_data, sizeof(struct light_band_para_t));
    			light_set(light.mode, light.speed, light.color, 0);
				memcpy(&light, uart0_data+sizeof(struct light_band_para_t), sizeof(struct light_band_para_t));
    			light_set(light.mode, light.speed, light.color, 1);

				combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_LIGHT_CTR,MINI7_TAIL_FALG)
				sendMsgtoMasterMcu(&msg);
			}
				break;
		    case SLAVE_MCU_CMD_QRSCANNER_CTR:{
				}
			break;
			case SLAVE_MCU_CMD_FAN:{				
				g_heat_ctrl.fan_state = uart0_data[0];
				if (g_heat_ctrl.fan_state == 1)
				{
					PB4 = 1;
				}
				else{
					PB4 = 0;
				}			
				combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_FAN,MINI7_TAIL_FALG)
				sendMsgtoMasterMcu(&msg);	
			}
				break;
		    case SLAVE_MCU_CMD_CARD_ST:{
				g_heat_ctrl.card_state = uart0_data[0];	
				combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_CARD_ST,MINI7_TAIL_FALG)				
//				/*进入稳定状态，调整目标温度*/
			    if(g_heat_ctrl.card_state &&  uart0_massage.len >= 8){
					float cabinTem =  *(float *)&uart0_data[4];
					float adj =  *(float *)&uart0_data[8];
					if(cabinTem < ENV_ADJ_THRESHOLD){
						g_heat_ctrl.adJTarget = adj;
					}else{
						g_heat_ctrl.adJTarget = 0;
					}
				}
				sendMsgtoMasterMcu(&msg);
			}
			break;
		    case SLAVE_MCU_CMD_PID:{
				kp = *(float *)&uart0_data[0];
				ki = *(float *)&uart0_data[4];
				kd = *(float *)&uart0_data[8];	
    			PID_set(&pid1, kp, ki, kd);	
				combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_CMD_PID,MINI7_TAIL_FALG)
				sendMsgtoMasterMcu(&msg);
			}
			break;
		    case SLAVE_MCU_UP_QR:{
		    	/*扫描结果上报成功，清除缓存*/
				memset(g_uart2_rx_buf, 0 ,sizeof(g_uart2_rx_buf));
				g_uart2_rx_len = 0;
				haveheaderflag = QR_REC_STATE_EMPTY;
						
//		    light_set(MODE_NORMAL, 250, COLOR_YELLOW, 0);
//		    light_set(MODE_NORMAL, 250, COLOR_YELLOW, 1);
			}
			break;
		    case SLAVE_MCU_UP_QR_PARAM:{
				qr_check_delay = 0;
				haveheaderflag = QR_REC_STATE_SENDQR;
//			    light_set(MODE_NORMAL, 250, COLOR_GREEN, 0);
//			    light_set(MODE_NORMAL, 250, COLOR_GREEN, 1);
			}
			break;
		}
	}
}


/********************************************************************
 *                         heater_state_upload()
 *
 *描述：上传加热、风扇状态及加热体温度
 *参数：无
 *返回：0/1：设置失败/成功
 *其他：无
 *--------------------------------------------------------------------
 *记录：包含上报和应答处理
 ********************************************************************/
void heater_state_upload(uint32_t upenv)
{
	struct CmdMessage msg;
	uint8_t databuf[7]={0};
	combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_UP_HEAT,MINI7_TAIL_FALG)
	if(upenv){		
		msg.len = sizeof(databuf);
	}else{
		msg.len = sizeof(databuf)-2;
	}
	msg.data = databuf;	
	databuf[0] = g_heat_ctrl.heat_state;
	databuf[1] = g_heat_ctrl.temp_stable_sta;
	databuf[2] = (uint8_t)pid1.ActualTemp;
	databuf[3] = (uint8_t)((pid1.ActualTemp - (uint8_t)pid1.ActualTemp) * 100);
	databuf[4] = g_heat_ctrl.fan_state;
	databuf[5] = (uint8_t)pid1.cabinTem;
	databuf[6] = (uint8_t)((pid1.cabinTem - (uint8_t)pid1.cabinTem) * 100);
	sendMsgtoMasterMcu(&msg);
	
}

void sendInfoMasterMcu(uint8_t *data, uint16_t len)
{
	struct CmdMessage msg;
	combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_INFO,MINI7_TAIL_FALG)
	msg.len = len;
	msg.data = data;

	sendMsgtoMasterMcu(&msg);
}

/********************************************************************
 *                         uart2_rcv_frame_analysis()
 *
 *描述：接收帧解析
 *参数：无
 *返回：无
 *其他：无
 *--------------------------------------------------------------------
 *记录：
 ********************************************************************/

void send_param(void){	
	struct CmdMessage msg;
	combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_UP_QR_PARAM,MINI7_TAIL_FALG)
	msg.len = sizeof(m_str_card_param_info);
	msg.data = (char*)&m_str_card_param_info;
	sendMsgtoMasterMcu(&msg);
}
void send_QR_inf(void){	
	struct CmdMessage msg;	
	combine_cmdMessage(msg,MINI7_HEADER_FALG,MINI7_MASSAGE_TYPE_MCUTOMCU,SLAVE_MCU_UP_QR,MINI7_TAIL_FALG)
	msg.len = g_uart2_rx_len;
	msg.data = g_uart2_rx_buf;
	sendMsgtoMasterMcu(&msg);
}

/*上报二维码信息*/
void sendQR_inf(void){
	if((haveheaderflag == QR_REC_STATE_SENDPRAM) && (qr_check_delay%2==0)){
		send_param();
	}
	if((haveheaderflag == QR_REC_STATE_SENDQR) && (qr_check_delay%2==0)){
		send_QR_inf();
	}
}
/*发送完成，清除标记，清除缓冲区*/
//void sendQR_send_ok(void)
//{
//	memset(g_uart2_rx_buf, 0 ,sizeof(g_uart2_rx_buf));
//	g_uart2_rx_len = 0;
//	qr_check_delay = QR_CHECH_STATE_FIRST;
//}
void uart2_rcv_frame_analysis(void)
{
	if(haveheaderflag <= QR_REC_STATE_HEADER){		
		if (('{' == g_uart2_rx_buf[0])&&(g_uart2_rx_len>10))
		{
			if(haveheaderflag != QR_REC_STATE_HEADER){
				haveheaderflag = QR_REC_STATE_HEADER;
				qr_check_delay = 0;
				
//			    light_set(MODE_NORMAL, 250, COLOR_RED, 0);
//			    light_set(MODE_NORMAL, 250, COLOR_RED, 1);
			}
			if(0 == parse_qrcode_info(&m_str_card_param_info, g_uart2_rx_buf)){
				haveheaderflag = QR_REC_STATE_SENDPRAM; 	
				qr_check_delay = 0;
//			    light_set(MODE_NORMAL, 250, COLOR_BLUE, 0);
//			    light_set(MODE_NORMAL, 250, COLOR_BLUE, 1);
			}
		}
		if((qr_check_delay == QR_CHECH_STATE_WAITING) ||('{' != g_uart2_rx_buf[0] && g_uart2_rx_len > 0)){
			/*码头为 '{' 延时解码不对，清空 */
			/*解码完成 */
			/*码头不为 '{' 非法，清空*/
			memset(g_uart2_rx_buf, 0 ,sizeof(g_uart2_rx_buf));
			g_uart2_rx_len = 0;
			qr_check_delay =  0;
			haveheaderflag = QR_REC_STATE_EMPTY;		
		}
	}
}

/********************************************************************
 *                         trigmode_select_sensor()
 *
 *描述：切换为感应触发模式
 *参数：无
 *返回：0/1：设置失败/成功
 *其他：无
 *--------------------------------------------------------------------
 *记录：包含上报和应答处理
 ********************************************************************/
void trigmode_select_sensor(void)
{
	memset(g_uart2_tx_buf, 0, sizeof(g_uart2_tx_buf));
	//	memset(g_uart2_rx_buf, 0, sizeof(g_uart2_rx_buf));
	g_uart2_tx_buf[0] = 0x7E; // 帧头
	g_uart2_tx_buf[1] = 0x00; // 帧头
	g_uart2_tx_buf[2] = 0x08; // 命令标识
	g_uart2_tx_buf[3] = 0x01; // 命令标识
	g_uart2_tx_buf[4] = 0x00;
	g_uart2_tx_buf[5] = 0x00;
	g_uart2_tx_buf[6] = 0xD7;
	//	g_uart2_tx_buf[6]  = 0xEF;
	g_uart2_tx_buf[7] = 0xAB;
	g_uart2_tx_buf[8] = 0xCD;
	UART_Write(UART2, g_uart2_tx_buf, 9);
}

/********************************************************************
 *                         data_out_oragin()
 *
 *描述：切换为原始数据输出
 *参数：无
 *返回：0/1：设置失败/成功
 *其他：无
 *--------------------------------------------------------------------
 *记录：包含上报和应答处理
 ********************************************************************/
void data_out_original(void)
{
	memset(g_uart2_tx_buf, 0, sizeof(g_uart2_tx_buf));

	g_uart2_tx_buf[0] = 0x7E; // 帧头
	g_uart2_tx_buf[1] = 0x00; // 帧头
	g_uart2_tx_buf[2] = 0x08; // 命令标识
	g_uart2_tx_buf[3] = 0x01; // 命令标识
	g_uart2_tx_buf[4] = 0x00;
	g_uart2_tx_buf[5] = 0x0D;
	//	g_uart0_tx_buf[6]  = 0xD7;
	g_uart2_tx_buf[6] = 0xCA;
	g_uart2_tx_buf[7] = 0xAB;
	g_uart2_tx_buf[8] = 0xCD;
	UART_Write(UART2, g_uart2_tx_buf, 9);
	HAL_Delay(100);
	memset(g_uart2_rx_buf, 0, sizeof(g_uart2_rx_buf));
	g_uart2_rx_len = 0;
}

int parse_qrcode_info(str_card_param_info_t *info, char *qrcode_str)
{
	int id = 0;
	double Tt = 0;
	double Tt1 = 0;
	
	if (info == 0)
	{
		return -1;
	}

	cJSON *root = cJSON_Parse(qrcode_str);
	if (root == NULL)
	{   /*消息不合格*/
		return -1;
	}
	cJSON *item = cJSON_GetObjectItem(root, "p");
	if (item == NULL)
	{   /*消息不合格*/
		cJSON_Delete(root);
		return -1;
	}
	char *pid = cJSON_GetStringValue(item);
	//    printf("pid=%s \r\n", pid);
	memcpy(info->pid, 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);
}
