#include "ring_buffer.h"
#include <rtthread.h>
#include "usart.h"
#include "weight.h"
#include "flash.h"
#include "plan.h"
#include "display.h"
#include "button.h"
#include "xBeeAppEscapingMode.h"

rt_sem_t thread_EmptyBuffer_sem = RT_NULL;

rt_sem_t uart1_lock = RT_NULL;
rt_sem_t uart2_lock = RT_NULL;
rt_sem_t uart3_lock = RT_NULL;
rt_sem_t uart4_lock = RT_NULL;
rt_sem_t uart5_lock = RT_NULL;


#define BUF_SIZE 256
ring_buffer usart1_buf,usart2_buf,usart3_buf,uart4_buf,uart5_buf;
ring_buffer *rb_usart1,*rb_usart2,*rb_usart3,*rb_uart4,*rb_uart5;
unsigned char rb_buffer1[BUF_SIZE],rb_buffer2[BUF_SIZE*4],rb_buffer3[BUF_SIZE],rb_buffer4[BUF_SIZE],rb_buffer5[BUF_SIZE];

ring_buffer display_buf;
ring_buffer *rb_display;
unsigned char rb_display_buffer[BUF_SIZE];

ring_buffer _SendWeightQueue,_XbeeQueue,_XbeeAPIQueue,_RemoteQueue;
ring_buffer *SendWeightQueue,*XbeeQueue,*XbeeAPIQueue,*RemoteQueue;
unsigned char SendWeightQueue_bf[BUF_SIZE*4],XbeeQueue_bf[BUF_SIZE*2],XbeeAPIQueue_bf[BUF_SIZE*4],RemoteQueue_bf[BUF_SIZE*2];

ring_buffer _RecWeightQueue;
ring_buffer *RecWeightQueue;
uint8_t RecWeightQueue_bf[128];

uint8_t usart1_send_data[64];
uint8_t usart2_send_data[256];
uint8_t usart3_send_data[64];
uint8_t uart4_send_data[256];

uint8_t flag_uart1_send = 0;
uint8_t flag_uart2_send = 0;
uint8_t flag_uart3_send = 0;
uint8_t flag_uart4_send = 0;

unsigned char XbeeConst_CenterAddress[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF};  //开始地址 0xFF
unsigned char XbeeConst_CCAddress[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF};      //开始地址 0xFF                                 //开始地址 0x10
unsigned char XbeeConst_RemoteAddress = 0x01; // 远程TMR地址

static unsigned char XbeeConst_FRxBufAPI[100];        //处理无线临时表 
unsigned char XbeeConst_ReadAPINum = 0;
static unsigned char XbeeConst_needEscape=0;
unsigned char XbeeConst_FRemoteTxBuf[]= {0x00,0x7E,0x15,0x00,0xF1,0x7E,0x45,
                                                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                                                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                                                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                                                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
                                               };        //处理铲车临时表

static unsigned char XbeeConst_FRxBuf0[100];        //处理完整无线临时表
static unsigned char XbeeConst_FRxBuf0_Remote[100];        //处理完整铲车无线临时表


static unsigned char XbeeConst_FrameLength0 = 0;
static unsigned char XbeeConst_ReadNum0 = 0;

static unsigned char XbeeConst_FrameLength0_Remote = 0, XbeeConst_ReadNum0_Remote = 0;

unsigned char XbeeConst_FrameNumLast=0;  //上次发送的帧编号
unsigned char XbeeConst_FTxBuf0[50];        //处理发送无线临时表

unsigned char* XbeeConst_PTxBuf0;        //处理发送无线临时表
unsigned char* XbeeConst_PRemoteTxBuf0;        //处理发送远程显示临时表																					 
unsigned char ccsend = 0;

		
unsigned char readyPop = 0;			
static unsigned char XbeeConst_FRxBuf12[100];        //处理完整无线临时表																							 
void RB_init(void)
{
    rb_usart1 = &usart1_buf;
    rb_init(rb_usart1, sizeof(rb_buffer1)/sizeof(rb_buffer1[0]), rb_buffer1);

    rb_usart2 = &usart2_buf;
    rb_init(rb_usart2, sizeof(rb_buffer2)/sizeof(rb_buffer2[0]), rb_buffer2);

    rb_usart3 = &usart3_buf;
    rb_init(rb_usart3, sizeof(rb_buffer3)/sizeof(rb_buffer3[0]), rb_buffer3);

    rb_uart4 = &uart4_buf;
    rb_init(rb_uart4, sizeof(rb_buffer4)/sizeof(rb_buffer4[0]), rb_buffer4);

    rb_uart5 = &uart5_buf;
    rb_init(rb_uart5, sizeof(rb_buffer5)/sizeof(rb_buffer5[0]), rb_buffer5);

   // if (WeightConst_SBType==4||WeightConst_SBType==6) {
        rb_display = &display_buf;
        rb_init(rb_display, sizeof(rb_display_buffer)/sizeof(rb_display_buffer[0]), rb_display_buffer);
  //  }

    SendWeightQueue = &_SendWeightQueue;
    rb_init(SendWeightQueue, sizeof(SendWeightQueue_bf)/sizeof(SendWeightQueue_bf[0]), SendWeightQueue_bf);

    XbeeQueue = &_XbeeQueue;
    rb_init(XbeeQueue, sizeof(XbeeQueue_bf)/sizeof(XbeeQueue_bf[0]), XbeeQueue_bf);

    XbeeAPIQueue = &_XbeeAPIQueue;
    rb_init(XbeeAPIQueue, sizeof(XbeeAPIQueue_bf)/sizeof(XbeeAPIQueue_bf[0]), XbeeAPIQueue_bf);

		RecWeightQueue = &_RecWeightQueue;
    rb_init(RecWeightQueue, sizeof(RecWeightQueue_bf)/sizeof(RecWeightQueue_bf[0]), RecWeightQueue_bf);

    RemoteQueue = &_RemoteQueue;
    rb_init(RemoteQueue, sizeof(RemoteQueue_bf)/sizeof(RemoteQueue_bf[0]), RemoteQueue_bf);
}

void USART_Push(unsigned char address, unsigned char data)
{
    if (address==1)
    {
        rb_push_insert(rb_usart1, data);
    }
    else if (address==2)
    {
        rb_push_insert(rb_usart2, data);
    }
    else if (address==3)
    {
        rb_push_insert(rb_usart3, data);
    }
    else if (address==4)
    {
        rb_push_insert(rb_uart4, data);
    }
    else if (address==5)
    {
        rb_push_insert(rb_uart5, data);
    }
}

void RX_API(void) {
    unsigned char  tempval, checksum = 0;
    if (rb_full_count(XbeeAPIQueue)> 0) {
        tempval = rb_remove(XbeeAPIQueue);  //继续存数据
		//		rt_kprintf(" %02X",tempval);
        switch (XbeeConst_ReadAPINum) {
        case 0:
            if (tempval == 0x7e) { //协议头前面1个7E判断
                XbeeConst_FRxBufAPI[0] = tempval;
                XbeeConst_ReadAPINum = 1;
            }
            break;
        case 1:
            if (tempval == 0x7e) { //协议头前面1个7E判断
                XbeeConst_FRxBufAPI[0] = tempval;
                XbeeConst_ReadAPINum = 1;
            }
            else {
                if (tempval == 0x7D)	XbeeConst_needEscape = 1;
                else
                {
                    if (XbeeConst_needEscape) {
                        XbeeConst_needEscape=0;
                        tempval = tempval^0x20;
                    }
                    XbeeConst_FRxBufAPI[XbeeConst_ReadAPINum++] = tempval;
                }
            }
            break;
        case 2:
            if (tempval == 0x7e) { //协议头前面1个7E判断
                XbeeConst_FRxBufAPI[0] = tempval;
                XbeeConst_ReadAPINum = 1;
            }
            else {
                if (tempval == 0x7D)	XbeeConst_needEscape = 1;
                else
                {
                    if (XbeeConst_needEscape) {
                        XbeeConst_needEscape=0;
                        tempval = tempval^0x20;
                    }
                    XbeeConst_FRxBufAPI[XbeeConst_ReadAPINum++] = tempval;
                }
            }
            break;
        default:
            if (tempval == 0x7e) { //协议头前面1个7E判断
                XbeeConst_FRxBufAPI[0] = tempval;
                XbeeConst_ReadAPINum = 1;
            }
            else {
                if (tempval == 0x7D)	XbeeConst_needEscape = 1;
                else
                {
                    if (XbeeConst_needEscape) {
                        XbeeConst_needEscape=0;
                        tempval = tempval^0x20;
                    }
                    XbeeConst_FRxBufAPI[XbeeConst_ReadAPINum++] = tempval;
                }
            }

            if (XbeeConst_ReadAPINum > XbeeConst_FRxBufAPI[2]+3) {     //读完已知长度的数据
						//	  rt_kprintf("\n");
                for (unsigned short i = 3; i < XbeeConst_FRxBufAPI[2]+3; i++)
                    checksum += XbeeConst_FRxBufAPI[i];
                if (((0xff - checksum)&0xFF) == XbeeConst_FRxBufAPI[XbeeConst_FRxBufAPI[2]+3])  //校验成功
                {
                    if (XbeeConst_FRxBufAPI[3] == 0x90) {
                        if((XbeeConst_FRxBufAPI[18]&0xF0) == 0xA0 || (XbeeConst_FRxBufAPI[18]) == 0x14)       //铲车数据
                        {
                            if ((XbeeConst_FRxBufAPI[18] == 0xA1 && WeightConst_SBType == 2) ||
                                    (XbeeConst_FRxBufAPI[18] != 0xA1 ))
                                for (unsigned short i = 15; i < XbeeConst_FRxBufAPI[2]+3; i++)
                                    rb_push_insert(RemoteQueue, XbeeConst_FRxBufAPI[i]);  //赋值数据到数组
                        }
                        else if (XbeeConst_FRxBufAPI[18] == 0xB2 && XbeeConst_FRxBufAPI[19] == 0x03)        //收到设置铲车定向
                        {
                            for (unsigned short i = 0; i < 8; i++)	XbeeConst_CCAddress[i] = XbeeConst_FRxBufAPI[i+4];
                            ccsend = (XbeeConst_FRxBufAPI[17] == XbeeConst_DeviceAddress);
                        }
                        else if (XbeeConst_FRxBufAPI[17] == XbeeConst_DeviceAddress || XbeeConst_FRxBufAPI[17] == 0xFF)
                            for (unsigned short i = 15; i < XbeeConst_FRxBufAPI[2]+3; i++)
                                rb_push_insert(XbeeQueue, XbeeConst_FRxBufAPI[i]);  //赋值数据到数组
												
                        if(XbeeConst_FRxBufAPI[18]==0x18)
                            for (unsigned short i = 0; i < 8; i++)
                                XbeeConst_CenterAddress[i] = XbeeConst_FRxBufAPI[i+4];
                    }
                }
                XbeeConst_ReadAPINum = 0; //循环计数清零
            }
            break;
        }
    }
}

static unsigned char planreget_save,planreget = 0;
void RX(void) {
    unsigned char tempval, checksum = 0;

    if (rb_full_count(XbeeQueue)> 0) {

        tempval = rb_remove(XbeeQueue);  //继续存数据

        switch (XbeeConst_ReadNum0) {
        case 0:
            if (tempval == 0x7e) { //协议头前面1个7E判断
                XbeeConst_FRxBuf0[XbeeConst_ReadNum0] = tempval;
                XbeeConst_ReadNum0++;
            }
            break;
        case 1:
            XbeeConst_FRxBuf0[XbeeConst_ReadNum0] = tempval;           //这个字节为帧长度
            XbeeConst_ReadNum0++;
            XbeeConst_FrameLength0 = XbeeConst_FRxBuf0[1] + 1; //算出帧长度
            break;
        default:
            XbeeConst_FRxBuf0[XbeeConst_ReadNum0] = tempval;       //读数据

            if (XbeeConst_ReadNum0 >= XbeeConst_FrameLength0) {     //读完已知长度的数据
                for (unsigned short i = 0; i < XbeeConst_FrameLength0; i++)
                    checksum += XbeeConst_FRxBuf0[i];

                if ((0xff - checksum) == XbeeConst_FRxBuf0[XbeeConst_FrameLength0])  //校验成功
                {
									  rt_strncpy(dataframehead.Buffer, XbeeConst_FRxBuf0, 4);
                    if ((dataframehead.data.addr&0x0F) == XbeeConst_DeviceAddress || dataframehead.data.addr == 0xFF) { //收到本设备的数据，不是本设备的不处理
                        if (dataframehead.data.frameType == 0x10) {   //实时重量
                            if (XbeeConst_FRxBuf0[XbeeConst_FrameLength0-1] == XbeeConst_FrameNumLast) {
															  save_read_ts('w');	
                                XbeeConst_CanSend=0x01;//取消无线发送锁定	
                            }
                        }

                        else if (dataframehead.data.frameType == 0x11 ) { //1号键回复
                            if (XbeeConst_FRxBuf0[XbeeConst_FrameLength0-1] == XbeeConst_FrameNumLast) {	
															  save_read_ts('k');																									
                                XbeeConst_CanSend=0x01;//取消无线发送锁定				
                            }
                        }

                        else if (dataframehead.data.frameType == 0x12 || dataframehead.data.frameType == 0x15) { //2号键内容
//												   	rt_kprintf("%02X//////////////////////////%02X \n",dataframehead.data.frameType,XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1]);				
                            if (dataframehead.data.frameType == 0x12)
                            {   
                               // if(isn==1) {
                               //   clearScreenAll_();
                               //   delay_ms(100);
                               // }
															if (getPlaning==1)
															{
																planreget_save = 0;
																
																readyPop = 1;
																for (int i=0;i<XbeeConst_FRxBuf0[1]+2;i++)
																	XbeeConst_FRxBuf12[i] = XbeeConst_FRxBuf0[i];  
															}
															 //setWeightConst_initProduct();
															
												       //	rt_sem_release(pop_product_sem);//需要弹出最新内容
                            }
                            else if (dataframehead.data.frameType == 0x15)
                            {				
//															  rt_kprintf("--XbeeConst_lastsort:%02X \n", XbeeConst_lastsort);				
//                                rt_kprintf("0x15 ------index:%02X \n",XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1]);															
																if (XbeeConst_lastsort==0xFD && XbeeConst_lastsort < XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1])
																	 reset_read_ts('p');
															  if (readyPop==1 && getPlaning==1)
																{
																	//	readyPop = 0;
																		getPlaning=0;			
																		planreget = XbeeConst_FRxBuf12[XbeeConst_FRxBuf12[1]-3]&0x01;
																		reset_read_ts('p');
																		ts_pushArrary(&ts_plandb, XbeeConst_FRxBuf12, XbeeConst_FRxBuf12[1]+2); 
																			
//																		rt_kprintf("\n");			
//																	rt_kprintf("\n");			
//																	rt_kprintf("\n");			
//																		rt_kprintf("0x12-----------------------------\n");				
//																		rt_kprintf("pop index:%02X \n",XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1]);
																		rt_sem_release(pop_product_sem);//需要弹出最新内容																		
																}			
																if (planreget_save==0 && planreget==1){																  
																	 XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-2] = XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-2] || 0x01;
																}
																planreget_save =1; 
																if (XbeeConst_lastsort != XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1])
																		ts_pushArrary(&ts_plandb, XbeeConst_FRxBuf0, XbeeConst_FRxBuf0[1]+2); 
																save_read_ts('k');																
																XbeeConst_CanSend=0x01;//取消无线发送锁定	
																
																if (XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1]<0xFD && getPlaning==0) { //如果没有结束，则发送05协议			
																															
																			XbeeConst_lastsort = XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1];
																			XbeeConst_lastinorout = XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-3]>>7;
//															    		rt_kprintf("XbeeConst_lastsort get:%02X \n", XbeeConst_lastsort);				
																			sendkey2(0xF5); 											
																}																	
																else if (XbeeConst_lastsort==0xFD && readyPop==0 && XbeeConst_lastsort < XbeeConst_FRxBuf0[XbeeConst_FRxBuf0[1]-1])
																{
																		rt_sem_release(pop_product_sem);//如果直接收到15，如全天结束或空计划，需要弹出最新内容
																}
																if (readyPop==1) readyPop = 0;													
                            }
															
                        }
                        else if (dataframehead.data.frameType == 0x14) {      //无线按键处理
                            RemoteSendDisplay = 0;

                            if (isCC)
                                checksum =0x7E + 0x05 +  ((XbeeConst_FRxBuf0_Remote[2] & 0x0F) << 4 ) || XbeeConst_DeviceAddress
                                          + 0xA4 + XbeeConst_FRxBuf0[4] + XbeeConst_FRxBuf0[5];
                            else
                                checksum =0x7E + 0x05 + XbeeConst_DeviceAddress + 0xA4 + XbeeConst_FRxBuf0[4] + XbeeConst_FRxBuf0[5];
                            XbeeConst_FRemoteTxBuf[0] = 0x07;
                            XbeeConst_FRemoteTxBuf[1] = 0x7E;
                            XbeeConst_FRemoteTxBuf[2] = 0x05;
                            if (isCC)
                                XbeeConst_FRemoteTxBuf[3] = ((XbeeConst_FRxBuf0_Remote[2] & 0x0F) << 4 ) | XbeeConst_DeviceAddress;
                            else
                                XbeeConst_FRemoteTxBuf[3] = XbeeConst_DeviceAddress;
                            XbeeConst_FRemoteTxBuf[4] = 0xA4;
                            XbeeConst_FRemoteTxBuf[5] = XbeeConst_FRxBuf0[4];
                            XbeeConst_FRemoteTxBuf[6] = XbeeConst_FRxBuf0[5];
                            XbeeConst_FRemoteTxBuf[7] = 0xff - checksum;
                            if (XbeeConst_UseAPI>0) {
																rt_sem_take(uart2_lock, RT_WAITING_FOREVER);				
                                setSendFrame_t(0x01,0x01,0xC1);
                                XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);		
                                for (int i = 1; i <= XbeeConst_PTxBuf0[0]; i++) //将缓冲区发出
                                    usart2_send_data[i - 1] = XbeeConst_PTxBuf0[i];
															  HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
																rt_sem_release(uart2_lock);
                            }
                            else {
																rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
                                for (int i = 1; i <= XbeeConst_FRemoteTxBuf[0]; i++) //将缓冲区发出
                                    usart2_send_data[i - 1] = XbeeConst_FRemoteTxBuf[i];
															  HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_FRemoteTxBuf[0]);
																rt_sem_release(uart2_lock);
                            }

                            RemoteSendDisplay = 1;
                            if (XbeeConst_FRxBuf0[4]==0x01 && button_state.b1 ==0 && delayKeying == 0)
														{		
																if (WeightConst_DELAYON == 0 && delayKeying == 0)
																{			
																		rt_sem_release(delay_key_sem); 			//收到1号键
																		sendkey1(0xF3, ((XbeeConst_FRxBuf0_Remote[2] >> 4) & 0x0F) );																	
																		WeightConst_AutoSingleNSecondPoint=0; // 防止自动跳转，重置自动记录指针
																		WeightConst_oksum = 0;
																		WeightConst_allsum = 0;
																		button_state.b1=1;  //5秒内不可按键
																}	
														}
                            else if (XbeeConst_FRxBuf0[4]==0x02 && button_state.b2==0) //收到2号键
                              //  sendkey2(0xF4);
																if(!WeightConst_DELAYON)
																{
																		if (tsdb_recordcount('k')==0)
																		{		
																				sendkey2(0xF4);
//																				WeightConst_AutoSingleNSecondPoint=0; // 防止自动跳转，重置自动记录指针
//																				WeightConst_oksum = 0;
//																				WeightConst_allsum = 0;
																				
																		}
																}
																button_state.b2=1;  //5秒内不可按键
                        }
												

                        else if (dataframehead.data.frameType == 0xA1) {      //铲车显示处理
                            for (unsigned short i=4; i < 22; i++) {  //显示重量
                                uart4_send_data[i - 4] = XbeeConst_FRxBuf0[i];

                                uart4_send_data[18]=0;
                                for (int i = 2; i <18; i++)   //校验和
                                    uart4_send_data[18]=uart4_send_data[18]+uart4_send_data[i];
																rt_sem_take(uart4_lock, RT_WAITING_FOREVER);
															  HAL_UART_Transmit_DMA(&huart4, uart4_send_data, 19);
																rt_sem_release(uart4_lock);
                            }
                        }

                        else if (dataframehead.data.frameType == 0xA4) {      //无线按键成功处理
                            beep();
                        }
                        else if (dataframehead.data.frameType == 0x16) {   //设置无线模式
                            XbeeConst_UseAPI = XbeeConst_FRxBuf0[4];
                            write_Flash("API", &XbeeConst_UseAPI, sizeof(XbeeConst_UseAPI));       
                            setAPI();
                            beep();
                        }
                        else if (dataframehead.data.frameType == 0x17) {   //设置屏幕类型
                            WeightConst_SBType = XbeeConst_FRxBuf0[4];                            
                            write_Flash("PM", &WeightConst_SBType, sizeof(WeightConst_SBType)); 
                            beep();
                        }
                        else if (dataframehead.data.frameType == 0x19) {   //设置地址
                            XbeeConst_DeviceAddress = XbeeConst_FRxBuf0[4];
                            write_Flash("CH", &XbeeConst_DeviceAddress, sizeof(XbeeConst_DeviceAddress)); 
                            beep();
                        }
                        else if (dataframehead.data.frameType == 0x1A) {   //设置提取类型
                            WeightConst_TQ = XbeeConst_FRxBuf0[4];
                            write_Flash("TQ", &WeightConst_TQ, sizeof(WeightConst_TQ)); 
                        //----------    USART_Configuration();
                            beep();
                        }
                        else if (dataframehead.data.frameType == 0x1B) {   //设置网络编号
                            beep();
                            XbeeConst_NetAddress = XbeeConst_FRxBuf0[4];
                            write_Flash("WL", &XbeeConst_NetAddress, sizeof(XbeeConst_NetAddress)); 
                            setNetAddress();
                            beep();
                        }
                        else if (dataframehead.data.frameType == 0x18) {   //设置时间																									
                            XbeeConst_CanSend=0x01;//取消无线发送锁定
													  TimeConst.Buffer[0] = XbeeConst_FRxBuf0[7];													
													  TimeConst.Buffer[1] = XbeeConst_FRxBuf0[6];													
													  TimeConst.Buffer[2] = XbeeConst_FRxBuf0[5];													
													  TimeConst.Buffer[3] = XbeeConst_FRxBuf0[4];		
                            save_read_ts('k');
                            write_Flash("TimeConst", &TimeConst, sizeof(TimeConst));		
                            CanButton = 0x01;
                            beep();
                        }
                    
										}
                }
                XbeeConst_ReadNum0 = 0; //循环计数清零
                XbeeConst_FrameLength0 = 0;
            } else {
                XbeeConst_ReadNum0++;  //小于帧长度
                if (XbeeConst_ReadNum0>90)
                {
                    XbeeConst_ReadNum0 = 0; //循环计数清零
                    XbeeConst_FrameLength0 = 0;
                }
            }
            break;
        }
    }
}

void RX_Remote(void) {
    unsigned char tempval, checksum = 0;
    if (rb_full_count(RemoteQueue) > 0) {
        tempval = rb_remove(RemoteQueue);  //继续存数据

        switch (XbeeConst_ReadNum0_Remote) {
        case 0:
            if (tempval == 0x7e) { //协议头前面1个7E判断
                XbeeConst_FRxBuf0_Remote[XbeeConst_ReadNum0_Remote] = tempval;
                XbeeConst_ReadNum0_Remote++;
            }
            break;
        case 1:
            XbeeConst_FRxBuf0_Remote[XbeeConst_ReadNum0_Remote] = tempval;           //这个字节为帧长度
            XbeeConst_ReadNum0_Remote++;
            XbeeConst_FrameLength0_Remote= XbeeConst_FRxBuf0_Remote[1] + 1; //算出帧长度
            break;
        default:
            XbeeConst_FRxBuf0_Remote[XbeeConst_ReadNum0_Remote] = tempval;       //读数据
            if (XbeeConst_ReadNum0_Remote >= XbeeConst_FrameLength0_Remote) {     //读完已知长度的数据
                for (unsigned short i = 0; i < XbeeConst_FrameLength0_Remote; i++)
                    checksum += XbeeConst_FRxBuf0_Remote[i];

                if ((0xff - checksum) == XbeeConst_FRxBuf0_Remote[XbeeConst_FrameLength0_Remote])  //校验成功
                {
                    if (
                        (((XbeeConst_FRxBuf0_Remote[2] & 0x0F) == XbeeConst_RemoteAddress && (WeightConst_SBType == 2 || WeightConst_SBType == 7))
                         ||
                         ((XbeeConst_FRxBuf0_Remote[2] & 0x0F) == XbeeConst_DeviceAddress && (WeightConst_SBType != 2 && WeightConst_SBType != 7)))
                        ||
                        (XbeeConst_RemoteAddress==0 && XbeeConst_FRxBuf0_Remote[2]==0x0A)) { //收到本设备的数据，不是本设备的不处理
                        if ((XbeeConst_FRxBuf0_Remote[3] == 0xA1 && (WeightConst_SBType == 2 || WeightConst_SBType == 7) )
                                ||  (XbeeConst_FRxBuf0_Remote[3] == 0xF1 && (WeightConst_SBType == 2 || WeightConst_SBType == 7) )
                           )
                        {   //铲车显示处理
                            if (XbeeConst_FRxBuf0_Remote[14]==0xC8 && XbeeConst_FRxBuf0_Remote[15]==0xB7) //确认开始
                            {
                                XbeeConst_FRxBuf0_Remote[21] = 0x20;
                                XbeeConst_FRxBuf0_Remote[20] = 0x20;
                                XbeeConst_FRxBuf0_Remote[19] = 0x20;
                                XbeeConst_FRxBuf0_Remote[18] = 0x20;
                                XbeeConst_FRxBuf0_Remote[17] = 0x20;
                                XbeeConst_FRxBuf0_Remote[16] = 0x20;
                                XbeeConst_FRxBuf0_Remote[15] = 0x20;
                                XbeeConst_FRxBuf0_Remote[14] = XbeeConst_RemoteAddress + 0x30;
                            }
                            else
                            {
                                XbeeConst_FRxBuf0_Remote[16] = XbeeConst_FRxBuf0_Remote[15];
                                XbeeConst_FRxBuf0_Remote[15] = XbeeConst_FRxBuf0_Remote[14];
                                XbeeConst_FRxBuf0_Remote[14] = XbeeConst_RemoteAddress + 0x30;
                            }

                            for (unsigned short i=4; i < 22; i++)   //显示重量
                                uart4_send_data[i - 4] = XbeeConst_FRxBuf0_Remote[i];
                            uart4_send_data[18]=0;
                            for (int i = 2; i <18; i++)   //校验和
                                uart4_send_data[18]=uart4_send_data[18]+uart4_send_data[i];
//                            if (WeightConst_SBType == 7)
//                            {
//                                for (int SendDisplay_i = 0; SendDisplay_i < 8; SendDisplay_i++) {
//                                    WeightConst_feed[SendDisplay_i] = uart4_send_data[2+SendDisplay_i] ;
//                                    WeightConst_Weight_display[7-SendDisplay_i] = uart4_send_data[10+SendDisplay_i] ;
//                                    WeightConst_Weight_display[7] = 0x20;
//                                }
//                                get_msg(4,//Xstart
//                                        8,//Ystart
//                                        1,//size
//                                        0,//reset
//                                        0,//speed
//                                        8,//字符个数
//                                        3,//界面数
//                                        0,//id
//                                        0x5B,//Xend
//                                        0,
//                                        &WeightConst_feed[0]//字码
//                                       );
//                                delay_ms(200);
//                                get_msg(92,//Xstart
//                                        4,//Ystart
//                                        2,//size
//                                        0,//reset
//                                        0,//speed
//                                        8,//字符个数
//                                        3,//界面数
//                                        1,//id
//                                        0xBF,//Xend
//                                        1,
//                                        &WeightConst_Weight_display[0]//字码
//                                       );

//                            }
//                            else
                            {
															 rt_sem_take(uart4_lock, RT_WAITING_FOREVER);
                               HAL_UART_Transmit_DMA(&huart4, uart4_send_data, 19);
															 rt_thread_mdelay(50);
															 rt_sem_release(uart4_lock);
                            }
                        }
                        else if (XbeeConst_FRxBuf0_Remote[3] == 0xA4 && (WeightConst_SBType == 2 || WeightConst_SBType == 7) )
                        {   //铲车收到无线按键成功处理
                            beep();
                        }
                        else if (
                            (XbeeConst_FRxBuf0_Remote[3] == 0xA2 ||  XbeeConst_FRxBuf0_Remote[3] == 0x14)
                            && (WeightConst_SBType != 2 && WeightConst_SBType != 7) && XbeeConst_FRxBuf0_Remote[1] == 0x05
                            //      (XbeeConst_FRxBuf0_Remote[3] == 0xA2 && WeightConst_SBType != 2 && XbeeConst_UseAPI)
                            //  ||  (XbeeConst_FRxBuf0_Remote[3] == 0x14 && WeightConst_SBType != 2 && !XbeeConst_UseAPI)
                        ) {      //TMR收到无线按键处理
                            RemoteSendDisplay = 0;
                            XbeeConst_FRemoteTxBuf[0] = 7;
                            XbeeConst_FRemoteTxBuf[1] = 0x7E;
                            XbeeConst_FRemoteTxBuf[2] = 0x05;
                            if (isCC)
                                XbeeConst_FRemoteTxBuf[3] = ((XbeeConst_FRxBuf0_Remote[2] & 0x0F) << 4 ) | XbeeConst_DeviceAddress;
                            else
                                XbeeConst_FRemoteTxBuf[3] = XbeeConst_DeviceAddress;
                            XbeeConst_FRemoteTxBuf[4] = 0xA4;
                            XbeeConst_FRemoteTxBuf[5] = XbeeConst_FRxBuf0_Remote[4];
                            XbeeConst_FRemoteTxBuf[6] = XbeeConst_FRxBuf0_Remote[5];
                            XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0;
                            for (int i = 1; i < XbeeConst_FRemoteTxBuf[0]; i++)
                                XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] =
                                    XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]]+XbeeConst_FRemoteTxBuf[i];

                            XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0xFF - XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]];

                            if (XbeeConst_UseAPI>0) {
																rt_sem_take(uart2_lock, RT_WAITING_FOREVER);			
                                setSendFrame_t(0x01,0x01,0xC1);
                                XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);			
                                for (int i = 1; i <= XbeeConst_PTxBuf0[0]; i++) //将缓冲区发出
                                    usart2_send_data[i - 1] = XbeeConst_PTxBuf0[i];
															  HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
																rt_thread_mdelay(50);
																rt_sem_release(uart2_lock);
                            }
                            else
                            {
																rt_sem_take(uart3_lock, RT_WAITING_FOREVER);
                                for (int i = 1; i <= XbeeConst_FRemoteTxBuf[0]; i++) //将缓冲区发出
                                    usart3_send_data[i - 1] = XbeeConst_FRemoteTxBuf[i];
															  HAL_UART_Transmit_DMA(&huart3, usart3_send_data, XbeeConst_FRemoteTxBuf[0]);
																rt_thread_mdelay(50);
																rt_sem_release(uart3_lock);
                            }

                            RemoteSendDisplay = 1;
                            if (XbeeConst_FRxBuf0_Remote[4]==0x01 && button_state.b1==0)//收到1号键
                            {
                                if (pf_comp.data.needBegin == 1 && WeightConst_WeightHaveBegin==0)
                                {
                                    beep();
																		rt_sem_take(weight_lock, RT_WAITING_FOREVER);
                                    WeightConst_WeightBegin = WeightConst_WeightCur;
																		rt_sem_release(weight_lock);
																	  write_Flash("beginweight", &WeightConst_WeightBegin, sizeof(WeightConst_WeightBegin));			
                                    getTargetWeight();
                                }
                                else if (pf_comp.data.pfsort != 0xFE && delayKeying == 0)
																{	
																		if (WeightConst_DELAYON == 0 && delayKeying == 0)
																		{		
																				rt_sem_release(delay_key_sem); 			
																				sendkey1(0xF3, ((XbeeConst_FRxBuf0_Remote[2] >> 4) & 0x0F));
																				WeightConst_AutoSingleNSecondPoint=0; // 防止自动跳转，重置自动记录指针
																				WeightConst_oksum = 0;
																				WeightConst_allsum = 0;
																				button_state.b1=1;  //5秒内不可按键
																		}
															  }
                            }
                            else if (XbeeConst_FRxBuf0_Remote[4]==0x02 && button_state.b2==0) //收到2号键
                            {
                                if(!WeightConst_DELAYON)
																{
																		if (tsdb_recordcount('k')==0)
																				sendkey2(0xF4);
																}
//                                WeightConst_AutoSingleNSecondPoint=0; // 防止自动跳转，重置自动记录指针
//                                WeightConst_oksum = 0;
//                                WeightConst_allsum = 0;
                                button_state.b2=1;  //5秒内不可按键
                            }
                        }
                    }
                }
                XbeeConst_ReadNum0_Remote = 0; //循环计数清零
                XbeeConst_FrameLength0_Remote = 0;
            } else {
                XbeeConst_ReadNum0_Remote++;   //小于帧长度
                if (XbeeConst_ReadNum0_Remote>90)
                {
                    XbeeConst_ReadNum0_Remote = 0; //循环计数清零
                    XbeeConst_FrameLength0_Remote = 0;
                }
            }
            break;
        }
    }
}

static void threadEmptyBuffer_entry(void *parameter)
{
		unsigned char tempval;
	
		while(1)
		{
			 rt_sem_take(thread_EmptyBuffer_sem, RT_WAITING_FOREVER);   //等待串口消息
			
			 while (rb_full_count(rb_usart1) > 0) {
				  ReadWeight(); 
			 }
			 
			 while (rb_full_count(rb_usart2) > 0) {
				 if (XbeeConst_UseAPI>0) {
            rb_push_insert(XbeeAPIQueue,rb_remove(rb_usart2));
            RX_API();
					}
					else
							rb_push_insert(XbeeQueue,rb_remove(rb_usart2));

					while(rb_full_count(XbeeQueue)>0) RX();
					while(rb_full_count(RemoteQueue)>0) RX_Remote();
			 }

			 while (rb_full_count(rb_usart3) > 0) {
				  if (XbeeConst_UseAPI>0) {
							if (WeightConst_BLE==1 || WeightConst_BLE== 3)
							{
									rb_push_insert(rb_usart1, rb_remove(rb_usart3));
							}
							else {
									rb_remove(rb_usart3);
							}	 //不能删除，用于清空3号串口
					}
					else
					{
							rb_push_insert(RemoteQueue, rb_remove(rb_usart3));
							while(rb_full_count(RemoteQueue)>0) RX_Remote();
					}
			 }

			 while (rb_full_count(rb_uart4) > 0) {
				 tempval = rb_remove(rb_uart4);  //继续存数据
         //if (tempval==0x59 && finish_send==0) finish_send=1;
			 }
		}
}

static rt_thread_t uart_tid = RT_NULL;
void threadUart_init(void)
{		  
    uart_tid = rt_thread_create("EmptyBuf",
                            threadEmptyBuffer_entry, RT_NULL,
                            0x500,
                            4, 5);

    /* 如果获得线程控制块，启动这个线程 */
    if (uart_tid != RT_NULL) rt_thread_startup(uart_tid);
}
