/*************************** (C) COPYRIGHT 2017 CXHY ****************************
 * 文件名  ：protocol.c
 * 描  述  ：协议处理程序
 * 版  本  ：V1.0
 * 作  者  ：XuZhongPing
 * 日  期  ：2017/03/16 
*********************************************************************************/
#include "tcp_protocol.h"
#include "delay.h"
#include "usart1.h"
#include "usart2.h"
#include "usart3.h"
#include "message.h"
#include "string.h"
#include "stdlib.h"  
#include "data_typedef.h"	
#include "device.h"


protocol_TypeDef userInfoQuery;				//用户信息查询指令
protocol_TypeDef tradingDataUpload;		//商品交易数据上传，查询是否可以出货

uint16_t dataLength = 0;							//一帧数据的长度
uint16_t operateType = 0;							//与后台服务器交互的指令类型
uint16_t sendOrderCode = 0x00;				//向后台发送的协议指令码	
uint16_t recreceiveOrderCode = 0;			//接收到后台下发的协议指令码
uint8_t  TCP_recreceiveStatus = 0;		//TCP数据接收标志，1为接收到TCP数据，0无

/*
***********************************************************************
*	函 数 名: ProtocolInit
*	功能说明: 协议变量初始化
*	形    参：	          
*	返 回 值: 
***********************************************************************
*/
void ProtocolInit(void)
{
	memset(GPRS_DATA_BUF, 0x00, GPRS_MAX_SEND_LEN);
	
	//下位机与后台服务器通讯指令
	userInfoQuery.protocolCode = 4002;
	userInfoQuery.parseState = 0;
	
	tradingDataUpload.protocolCode = 4016;
	tradingDataUpload.parseState = 0;
	
	//发放机的上位机与下位机通讯指令
// 	VM_ProtocolInit();
}

/*
***********************************************************************
*	函 数 名: CRC_Check
*	功能说明: 计算检验位
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
static uint8_t CRC_Check(uint8_t *pData, uint16_t len)
{
	uint8_t crc;
	uint16_t i;
	//CRC校验码
	crc = *pData;
	for(i = 0; i < len-1; i++)
	{
		crc ^= *(++pData);
	}
	return crc;
}

/*
***********************************************************************
*	函 数 名: ShowUploadData
*	功能说明: 显示回收箱上传投放数据
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
static void ShowUploadData(uint8_t type, uint16_t inputVal, uint8_t inputDistance)
{
	switch(type)
	{
		case PAPER:
			printf("\r\n上传纸类重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);	
			break;
			
		case PLASTIC:
			printf("\r\n上传塑料重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);
			break;
			
		case GLASS:
			printf("\r\n上传玻璃重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);	
			break;
			
		case METAL:
			printf("\r\n上传金属重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);
			break;
			
		case RECYCLED:
			printf("\r\n上传可回收物重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);
			break;
			
		case KITCHEN:
			printf("\r\n上传厨余重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);
			break;
			
		case SMALL_BOTTLE:
			printf("\r\n上传小塑料瓶子数量 = %d 个， 距离 = %d cm\r\n", inputVal, inputDistance);
			break;
			
		case BIG_BOTTLE:
			printf("\r\n上传大塑料瓶子数量 = %d 个， 距离 = %d cm\r\n", inputVal, inputDistance);
			break;
			
		case CANS:
			printf("\r\n上传易拉罐数量 = %d 个， 距离 = %d cm\r\n", inputVal, inputDistance);
			break;
			
		case BATTERY:
			printf("\r\n上传电池数量 = %d 个， 距离 = %d cm\r\n", inputVal, inputDistance);
			break;
			
		case FABRIC:
			printf("\r\n上传织物重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);
			break;
			
		case BOOK:
			printf("\r\n上传书本重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);
			break;
			
		case NEWSPAPER:
			printf("\r\n上传报纸重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);
			break;
			
		case KITCHEN_1:
			printf("\r\n上传厨余1重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);	
			break;
			
		case KITCHEN_2:
			printf("\r\n上传厨余2重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);	
			break;
			
		case PLASTIC_2:
			printf("\r\n上传塑料2重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);	
			break;
			
		case OTHER:
			printf("\r\n上传其它重量 = %2.2f Kg， 距离 = %d cm\r\n", (float)inputVal/100, inputDistance);	
			break;
		
		default:
			break;
	}
}

/*
***********************************************************************
*	函 数 名: ShowDeviceRunningStatus
*	功能说明: 显示上传设备运行状态信息
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
static void ShowDeviceRunningStatus(uint8_t type, uint8_t inputDistance)
{	
	switch(type)
	{
		case PAPER:
			printf(" 纸类 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case PLASTIC:
			printf(" 塑料 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case GLASS:
			printf(" 玻璃 = %3.1f cm ", (float)inputDistance);		
			break;
			
		case METAL:
			printf(" 金属 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case RECYCLED:
			printf(" 可回收物 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case KITCHEN:
			printf(" 厨余 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case SMALL_BOTTLE:
			printf(" 小塑料瓶子 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case BIG_BOTTLE:
			printf(" 大塑料瓶子 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case CANS:
			printf(" 易拉罐 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case BATTERY:
			printf(" 电池 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case FABRIC:
			printf(" 织物 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case BOOK:
			printf(" 书本 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case NEWSPAPER:
			printf(" 报纸 = %3.1f cm ", (float)inputDistance);	
			break;
			
		case KITCHEN_1:
			printf(" 厨余1 = %3.1f cm ", (float)inputDistance);		
			break;
			
		case KITCHEN_2:
			printf(" 厨余2 = %3.1f cm ", (float)inputDistance);		
			break;
			
		case PLASTIC_2:
			printf(" 塑料2 = %3.1f cm ", (float)inputDistance);		
			break;
			
		case OTHER:
			printf(" 塑料2 = %3.1f cm ", (float)inputDistance);		
			break;
			
		default:
			break;
	}
}

/*
***********************************************************************
*	函 数 名: ShowUploadAllData
*	功能说明: 显示回收箱上传投放数据（包含开门前后数据）
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
static void ShowUploadAllData(uint8_t type, uint16_t inputVal, uint8_t inputDistance, uint16_t beforeVal, uint16_t afterVal)
{
	switch(type)
	{
		case PAPER:
			printf("\r\n上传纸类");	
			break;
			
		case PLASTIC:
			printf("\r\n上传塑料");
			break;
			
		case GLASS:
			printf("\r\n上传玻璃");	
			break;
			
		case METAL:
			printf("\r\n上传金属");
			break;
			
		case RECYCLED:
			printf("\r\n上传可回收物");
			break;
			
		case KITCHEN:
			printf("\r\n上传厨余");
			break;
			
		case SMALL_BOTTLE:
			printf("\r\n上传小塑料瓶子数量 = %d 个， 距离 = %d cm\r\n", inputVal, inputDistance);
			break;
			
		case BIG_BOTTLE:
			printf("\r\n上传大塑料瓶子数量 = %d 个， 距离 = %d cm\r\n", inputVal, inputDistance);
			break;
			
		case CANS:
			printf("\r\n上传易拉罐");
			break;
			
		case BATTERY:
			printf("\r\n上传电池");
			break;
			
		case FABRIC:
			printf("\r\n上传织物");
			break;
			
		case BOOK:
			printf("\r\n上传书本");
			break;
			
		case NEWSPAPER:
			printf("\r\n上传报纸");
			break;
			
		case KITCHEN_1:
			printf("\r\n上传厨余1");	
			break;
			
		case KITCHEN_2:
			printf("\r\n上传厨余2");	
			break;
			
		case PLASTIC_2:
			printf("\r\n上传塑料2");	

			break;
			
		case OTHER:
			printf("\r\n上传其它");	

			break;
	}
	
	printf(": %2.2f Kg，%2.2f Kg -> %2.2f Kg，距离：%3.1f cm\r\n",\
				(float)inputVal/100, (float)beforeVal/100, (float)afterVal/100, (float)inputDistance);
}

/*
***********************************************************************
*	函 数 名: ProtocolPackaging
*	功能说明: 根据协议打包发送数据
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
uint16_t SendProtocolPackaging(uint16_t proNum)
{
	uint16_t proLen = 0;									//打包后的协议总长度
	uint16_t dataLen = 0;									//发送的数据长度
	uint8_t crc;
	uint16_t tmpeData;
	uint8_t transformPutInType = 0;
	
	uint8_t len = 0;
	
	static uint16_t serial_number = 0;		//协议流水号
	
	//帧头
	GPRS_DATA_BUF[0] = 0x48;
	GPRS_DATA_BUF[1] = 0x45;
	GPRS_DATA_BUF[2] = 0x52;
	GPRS_DATA_BUF[3] = 0x4F;
	proLen += 4;
	
	//2字节数据长度，到最后计算完成后载入
// 	GPRS_DATA_BUF[proLen++] = (uint8_t)(dataLen >> 8);
// 	GPRS_DATA_BUF[proLen++] = (uint8_t)dataLen;
	
	proLen += 2;		//2字节用于保存数据长度
	//设备号
	memcpy(&GPRS_DATA_BUF[proLen], gsm_msg.IMEI_HEX, 8);
	proLen += 8;
	
	//指令类别
	GPRS_DATA_BUF[proLen++] = (uint8_t)(proNum >> 8);
	GPRS_DATA_BUF[proLen++] = (uint8_t)proNum;
	
	//加载发送的数据及其数据长度
	switch(proNum)
	{
		case 1001:		//上传心跳
			GPRS_DATA_BUF[proLen++] = 1;												//设备类型（1：回收箱；2：发放机；3:POS机）
			break;
			
		case 1003:		//设备硬件信息上传
			GPRS_DATA_BUF[proLen++] = 0;												//协议版本号
			GPRS_DATA_BUF[proLen++] = 1;
			
			GPRS_DATA_BUF[proLen++] = 1;												//设备类型（1：回收箱；2：发放机；3:POS机）
			
			len = strlen(VersionNumber);
			GPRS_DATA_BUF[proLen++] = len;											//硬件版本号长度
			memcpy(&GPRS_DATA_BUF[proLen], VersionNumber, len);	//硬件版本号
			proLen += len;
			
			len = strlen((const char*)deviceIdBuf);
			GPRS_DATA_BUF[proLen++] = len;											//设备编号长度
			memcpy(&GPRS_DATA_BUF[proLen], deviceIdBuf, len);		//设备编号
			proLen += len;
			
			memcpy(&GPRS_DATA_BUF[proLen], gsm_msg.ICCID, 20);	//SIM卡ICCID号
			proLen += 20;
			
			switch(deviceType)
			{
				case THREE_CLASSIFICATION:		//三分类
					GPRS_DATA_BUF[proLen++] = 0;												//设备型号编码
					GPRS_DATA_BUF[proLen++] = deviceType;
					
					GPRS_DATA_BUF[proLen++] = 3;												//投口数量
					
					GPRS_DATA_BUF[proLen++] = devChannel1Type >> 8;			//投口垃圾类型（当前类型为8位）
					GPRS_DATA_BUF[proLen++] = devChannel1Type;
					GPRS_DATA_BUF[proLen++] = 1;												//投口型号
					
					GPRS_DATA_BUF[proLen++] = devChannel2Type >> 8;
					GPRS_DATA_BUF[proLen++] = devChannel2Type;
					GPRS_DATA_BUF[proLen++] = 1;
					
					GPRS_DATA_BUF[proLen++] = devChannel3Type >> 8;
					GPRS_DATA_BUF[proLen++] = devChannel3Type;
					GPRS_DATA_BUF[proLen++] = 1;
					break;
					
				case FOUR_CLASSIFICATION:		//四分类
					GPRS_DATA_BUF[proLen++] = 0;												//设备型号编码
					GPRS_DATA_BUF[proLen++] = deviceType;
					
					GPRS_DATA_BUF[proLen++] = 4;												//投口数量
					
					GPRS_DATA_BUF[proLen++] = devChannel1Type >> 8;			//投口垃圾类型（当前类型为8位）
					GPRS_DATA_BUF[proLen++] = devChannel1Type;
					GPRS_DATA_BUF[proLen++] = 1;												//投口型号
					
					GPRS_DATA_BUF[proLen++] = devChannel2Type >> 8;
					GPRS_DATA_BUF[proLen++] = devChannel2Type;
					GPRS_DATA_BUF[proLen++] = 1;
					
					GPRS_DATA_BUF[proLen++] = devChannel3Type >> 8;
					GPRS_DATA_BUF[proLen++] = devChannel3Type;
					GPRS_DATA_BUF[proLen++] = 1;
					
					GPRS_DATA_BUF[proLen++] = devChannel4Type >> 8;
					GPRS_DATA_BUF[proLen++] = devChannel4Type;
					GPRS_DATA_BUF[proLen++] = 1;
					break;
			
			}
			
			GPRS_DATA_BUF[proLen++] = 0;												//
			GPRS_DATA_BUF[proLen++] = 0;
			GPRS_DATA_BUF[proLen++] = 0;
			GPRS_DATA_BUF[proLen++] = 0;
			GPRS_DATA_BUF[proLen++] = 0;
			GPRS_DATA_BUF[proLen++] = 0;
			GPRS_DATA_BUF[proLen++] = 0;
			GPRS_DATA_BUF[proLen++] = 0;
			
			printf("\r\n软件版本: %s, len: %d\r\n", VersionNumber, strlen(VersionNumber));
			printf("\r\n设备编号: %s, len: %d\r\n", deviceIdBuf, strlen((const char*)deviceIdBuf));
			printf("\r\nICCID号: %s\r\n", gsm_msg.ICCID);
			
			break;
			
		case 4001:		//获取用户信息			
		case 4011:		//获取用户信息（返回信息包含垃圾袋二维码）前期的协议与兑换机的库存上传重复了,用4401代替
		case 4201:		//获取用户信息用于POS机授权控制开门投放次数（返回信息包含垃圾袋二维码）
		case 4401:		//替换4011（返回信息包含垃圾袋二维码，用于打印或绑定人脸）
			if(sendPutInType == 0x05)														//二维码贴纸开门
			{
				transformPutInType = 0x04;												//使用垃圾袋二维码查询用户信息的类型为0x04
			}else{
				transformPutInType = sendPutInType;
			}
			GPRS_DATA_BUF[proLen++] = transformPutInType;				//身份识别方式
			GPRS_DATA_BUF[proLen++] = sendDataLength;						//发送身份信息的长度
			memcpy(&GPRS_DATA_BUF[proLen], sendDataBuf, 33);		//加载身份信息
			proLen += 33;
			
			printf("\r\n用户身份查询:");	
			printf("\r\n--> Type: %d", sendPutInType);
			printf("\r\n--> Len: %d", sendDataLength);
			if(getPutInType == 1)
			{
				printf("\r\n--> Data: ");
				show(sendDataBuf, sendDataLength);
			}else{
				printf("\r\n--> Data: %s\r\n", sendDataBuf);
			}
			break;
		
		case 4301:		//用于分区域控制是否能打开投放口门
			GPRS_DATA_BUF[proLen++] = 1;												//设备类型（1：回收箱；2：发放机；3:POS机）
			if(sendPutInType == 0x05)														//二维码贴纸开门
			{
				transformPutInType = 0x04;												//使用垃圾袋二维码查询用户信息的类型为0x04
			}else{
				transformPutInType = sendPutInType;
			}
			GPRS_DATA_BUF[proLen++] = transformPutInType;				//身份识别方式
			GPRS_DATA_BUF[proLen++] = sendDataLength;						//发送身份信息的长度
			memcpy(&GPRS_DATA_BUF[proLen], sendDataBuf, 33);		//加载身份信息
			proLen += 33;
			
			printf("\r\n用户身份查询:");	
			printf("\r\n--> Type: %d", sendPutInType);
			printf("\r\n--> Len: %d", sendDataLength);
			if(getPutInType == 1)
			{
				printf("\r\n--> Data: ");
				show(sendDataBuf, sendDataLength);
			}else{
				printf("\r\n--> Data: %s\r\n", sendDataBuf);
			}
// 			show(sendDataBuf, 33);
			break;
			
		case 4701:		//收运清理上传
			GPRS_DATA_BUF[proLen++] = collectorSendAuthType;							//身份识别方式
			GPRS_DATA_BUF[proLen++] = collectorSendDataLength;						//发送身份信息的长度
			memcpy(&GPRS_DATA_BUF[proLen], collectorSendDataBuf, 33);							//加载身份信息
			proLen += 33;
// 			printf("\r\ntype：%d\r\n", sendPutInType);
// 			printf("\r\nlen：%d\r\n", sendDataLength);
// 			printf("\r\ndata：%s\r\n", sendDataBuf);
			
// 			show(GPRS_DATA_BUF, proLen);
			
// 			show(sendDataBuf, 33);
			break;
			
		case 4801:		//可疑投放上传
			GPRS_DATA_BUF[proLen++] = sendPutInType;						//身份识别方式
			GPRS_DATA_BUF[proLen++] = sendDataLength;						//发送身份信息的长度
			memcpy(&GPRS_DATA_BUF[proLen], sendDataBuf, 33);		//加载身份信息
			proLen += 33;
			
			switch(deviceType)
			{
				case DOUBLE_CLASSIFICATION:		//双桶480L
					//加载左桶投放上传数据
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//垃圾类型（玻璃类）
					
					GPRS_DATA_BUF[proLen++] = exceptionWeight1 >> 8;				//重量高8位
					GPRS_DATA_BUF[proLen++] = exceptionWeight1;
				
					GPRS_DATA_BUF[proLen++] = exceptionWeightBefore1 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = exceptionWeightBefore1;
					GPRS_DATA_BUF[proLen++] = exceptionWeightAfter1 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = exceptionWeightAfter1;
					
					ShowUploadAllData(leftBarrelType, exceptionWeight1, getDistance1, exceptionWeightBefore1, exceptionWeightAfter1);
					
					//加载右桶投放上传数据
					GPRS_DATA_BUF[proLen++] = rightBarrelType;											//垃圾类型（金属）
					
					GPRS_DATA_BUF[proLen++] = exceptionWeight2 >> 8;				//重量高8位
					GPRS_DATA_BUF[proLen++] = exceptionWeight2;
					
					GPRS_DATA_BUF[proLen++] = exceptionWeightBefore2 >> 8;				//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = exceptionWeightBefore2;
					GPRS_DATA_BUF[proLen++] = exceptionWeightAfter2 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = exceptionWeightAfter2;
					
					ShowUploadAllData(rightBarrelType, exceptionWeight2, getDistance2, exceptionWeightBefore2, exceptionWeightAfter2);
					break;
					
				case DOUBLE_CLASSIFICATION_BOTTLE:		//双桶480L含瓶子	
					//加载左桶投放上传数据
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//可回收物类型
					
					//瓶子暂时未设置异常检测
					smallBottleCount = 0;
				
					GPRS_DATA_BUF[proLen++] = smallBottleCount >> 8;		//小塑料瓶数量（高8位）
					GPRS_DATA_BUF[proLen++] = smallBottleCount;					//小塑料瓶数量（低8位）
					
					GPRS_DATA_BUF[proLen++] = 0;												//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = 0;
					GPRS_DATA_BUF[proLen++] = 0;												//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = 0;
					
					ShowUploadAllData(leftBarrelType, smallBottleCount, getDistance1, 0, 0);
					
					//加载右桶投放上传数据
					GPRS_DATA_BUF[proLen++] = rightBarrelType;					//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = exceptionWeight2 >> 8;					//重量高8位
					GPRS_DATA_BUF[proLen++] = exceptionWeight2;
					
					GPRS_DATA_BUF[proLen++] = exceptionWeightBefore2 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = exceptionWeightBefore2;
					GPRS_DATA_BUF[proLen++] = exceptionWeightAfter2 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = exceptionWeightAfter2;
					
					ShowUploadAllData(rightBarrelType, exceptionWeight2, getDistance2, exceptionWeightBefore2, exceptionWeightAfter2);
					break;
					
				case SINGLE_CLASSIFICATION:		//单桶660L
					//加载左桶投放上传数据
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//垃圾类型

					GPRS_DATA_BUF[proLen++] = exceptionWeight1 >> 8;					//重量高8位
					GPRS_DATA_BUF[proLen++] = exceptionWeight1;
					
					GPRS_DATA_BUF[proLen++] = exceptionWeightBefore1 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = exceptionWeightBefore1;
					GPRS_DATA_BUF[proLen++] = exceptionWeightAfter1 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = exceptionWeightAfter1;
					
					ShowUploadAllData(leftBarrelType, exceptionWeight1, getDistance1, exceptionWeightBefore1, exceptionWeightAfter1);
					break;
					
				default:
					break;
			}					
			break;
		
		case 6001:		//上传投放数据 
			GPRS_DATA_BUF[proLen++] = sendPutInType;						//身份识别方式
			GPRS_DATA_BUF[proLen++] = sendDataLength;						//发送身份信息的长度
			memcpy(&GPRS_DATA_BUF[proLen], sendDataBuf, 33);		//加载身份信息
			proLen += 33;
			
			printf("\r\nsendUserData:");
			show(sendDataBuf, 33);
			
			switch(deviceType)
			{
				case DOUBLE_CLASSIFICATION:		//双桶480L
					//加载左桶上传投放数据
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight1 >> 8;					//重量高8位
					GPRS_DATA_BUF[proLen++] = getWeight1;
					
					GPRS_DATA_BUF[proLen++] = getDistance1;							//测距值
					
					ShowUploadData(leftBarrelType, getWeight1, getDistance1);		//显示上传的信息
					
					//加载右桶上传投放数据
					GPRS_DATA_BUF[proLen++] = rightBarrelType;					//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight2 >> 8;					//重量高8位
					GPRS_DATA_BUF[proLen++] = getWeight2;	
					
					GPRS_DATA_BUF[proLen++] = getDistance2;							//测距值
					
					ShowUploadData(rightBarrelType, getWeight2, getDistance2);		//显示上传的信息
					break;
					
				case DOUBLE_CLASSIFICATION_BOTTLE:		//双桶480L含瓶子	
					//加载左桶上传投放数据
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//可回收物类型
					
					GPRS_DATA_BUF[proLen++] = smallBottleCount >> 8;		//小塑料瓶数量（高8位）
					GPRS_DATA_BUF[proLen++] = smallBottleCount;					//小塑料瓶数量（低8位）
					
					GPRS_DATA_BUF[proLen++] = getDistance1;							//测距值
					
					ShowUploadData(leftBarrelType, smallBottleCount, getDistance1);		//显示上传的信息	
					
					//加载右桶上传投放数据
					GPRS_DATA_BUF[proLen++] = rightBarrelType;					//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight2 >> 8;			//重量高8位
					GPRS_DATA_BUF[proLen++] = getWeight2;
					
					GPRS_DATA_BUF[proLen++] = getDistance2;					//测距值
					
					ShowUploadData(rightBarrelType, getWeight2, getDistance2);		//显示上传的信息
					break;
					
				case SINGLE_CLASSIFICATION:		//单桶660L	
					//加载桶上传投放数据
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//垃圾类型（纸类）

					GPRS_DATA_BUF[proLen++] = getWeight1 >> 8;				//重量高8位
					GPRS_DATA_BUF[proLen++] = getWeight1;
	
					GPRS_DATA_BUF[proLen++] = getDistance1;						//测距值
					
					ShowUploadData(leftBarrelType, getWeight1, getDistance1);		//显示上传的信息
					break;
					
				default:
					break;
			}
			
			//预留字段
			GPRS_DATA_BUF[proLen++] = 0x00;		
			GPRS_DATA_BUF[proLen++] = 0x00;			
			GPRS_DATA_BUF[proLen++] = 0x00;
			GPRS_DATA_BUF[proLen++] = 0x00;
					
			break;

//-----------------------------上传设备运行状态信息（温度、信号、测距）---------------------------------			
		case 6004:		//上传设备运行状态信息（温度、信号、测距）
			GPRS_DATA_BUF[proLen++] = 0x00;											//正负温度表示位
			tmpeData = temperature*10;
			GPRS_DATA_BUF[proLen++] = tmpeData >> 8;						//温度高8位
			GPRS_DATA_BUF[proLen++] = tmpeData;									//温度低8位
			
			GPRS_DATA_BUF[proLen++] = signal;										//GSM信号
			
			switch(deviceType)
			{
				case DOUBLE_CLASSIFICATION:		//双桶480L
					//加载左桶上传设备运行状态信息
					GPRS_DATA_BUF[proLen++] = leftBarrelType;					//垃圾类型			
					GPRS_DATA_BUF[proLen++] = getDistance1;						//测距值			
					
					//加载右桶上传设备运行状态信息
					GPRS_DATA_BUF[proLen++] = rightBarrelType;				//垃圾类型	
					GPRS_DATA_BUF[proLen++] = getDistance2;						//测距值
					
					printf("\r\n-----> 上传设备运行状态为：\r\n");	
					printf("\r\n温度 = %2.1f ℃, ", (float)temperature);
					printf("信号 = %d \r\n", signal);
					printf("\r\n测距（");	
					ShowDeviceRunningStatus(leftBarrelType, getDistance1);
					ShowDeviceRunningStatus(rightBarrelType, getDistance2);
					printf("）\r\n");	
					break;
					
				case DOUBLE_CLASSIFICATION_BOTTLE:		//双桶480L含瓶子	
					//加载左桶上传设备运行状态信息
					GPRS_DATA_BUF[proLen++] = leftBarrelType;				//垃圾类型			
					GPRS_DATA_BUF[proLen++] = getDistance1;					//测距值			
					
					//加载右桶上传设备运行状态信息
					GPRS_DATA_BUF[proLen++] = rightBarrelType;			//垃圾类型
					GPRS_DATA_BUF[proLen++] = getDistance2;					//测距值
					
					printf("\r\n-----> 上传设备运行状态为：\r\n");	
					printf("\r\n温度 = %2.1f ℃, ", (float)temperature);
					printf("信号 = %d \r\n", signal);
					printf("\r\n测距（");	
					ShowDeviceRunningStatus(leftBarrelType, getDistance1);
					ShowDeviceRunningStatus(rightBarrelType, getDistance2);
					printf("）\r\n");	
					break;
					
				case SINGLE_CLASSIFICATION:		//单桶660L
					//加载左桶上传设备运行状态信息
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//垃圾类型			
					GPRS_DATA_BUF[proLen++] = getDistance1;							//测距值			
					
					printf("\r\n-----> 上传设备运行状态为：\r\n");	
					printf("\r\n温度 = %2.1f ℃, ", (float)temperature);
					printf("信号 = %d \r\n", signal);
					printf("\r\n测距（");	
					ShowDeviceRunningStatus(leftBarrelType, getDistance1);
					printf("）\r\n");						
					break;
					
				case THREE_CLASSIFICATION:		//三分类
					//加载第1通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel1Type;						//垃圾类型
					GPRS_DATA_BUF[proLen++] = getDistance1;								//测距值
					
					//加载第2通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel2Type;						//垃圾类型
					GPRS_DATA_BUF[proLen++] = getDistance2;								//测距值
					
					//加载第3通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel3Type;						//垃圾类型
					GPRS_DATA_BUF[proLen++] = getDistance3;								//测距值
					break;
					
				case FOUR_CLASSIFICATION:		//四分类
					//加载第1通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel1Type;						//垃圾类型
					GPRS_DATA_BUF[proLen++] = getDistance1;								//测距值
					
					//加载第2通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel2Type;						//垃圾类型
					GPRS_DATA_BUF[proLen++] = getDistance2;								//测距值
					
					//加载第3通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel3Type;						//垃圾类型
					GPRS_DATA_BUF[proLen++] = getDistance3;								//测距值

					//加载第4通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel4Type;						//垃圾类型
					GPRS_DATA_BUF[proLen++] = getDistance4;								//测距值
					break;
					
				default:
					break;
					
			}
			break;
			
		case 6005:		//上传设备故障码			
			switch(deviceType)
			{
				case DOUBLE_CLASSIFICATION:		//双桶480L
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//垃圾类型
					GPRS_DATA_BUF[proLen++] = 0x00;											//故障码					
					GPRS_DATA_BUF[proLen++] = faultCode_1;							//故障码		
					
					GPRS_DATA_BUF[proLen++] = rightBarrelType;					//垃圾类型
					GPRS_DATA_BUF[proLen++] = 0x00;											//故障码
					GPRS_DATA_BUF[proLen++] = faultCode_2;							//故障码
					
					printf("\r\n-----> 上传故障码\r\n");	
					printf("\r\n左桶 = %d, ", faultCode_1);
					printf("右桶 = %d\r\n", faultCode_2);
					break;
					
				case DOUBLE_CLASSIFICATION_BOTTLE:		//双桶480L含瓶子	
					faultCode_1 = 0;																		//目前瓶子桶没有设置故障码
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//垃圾类型	
					GPRS_DATA_BUF[proLen++] = 0x00;											//故障码		
					GPRS_DATA_BUF[proLen++] = faultCode_1;							//故障码			
					
					GPRS_DATA_BUF[proLen++] = rightBarrelType;					//垃圾类型
					GPRS_DATA_BUF[proLen++] = 0x00;											//故障码
					GPRS_DATA_BUF[proLen++] = faultCode_2;							//故障码
					
					printf("\r\n-----> 上传故障码\r\n");	
					printf("\r\n左桶 = %d, ", faultCode_1);
					printf("右桶 = %d\r\n", faultCode_2);
					break;
					
				case SINGLE_CLASSIFICATION:		//单桶660L
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//垃圾类型
					GPRS_DATA_BUF[proLen++] = 0x00;											//故障码
					GPRS_DATA_BUF[proLen++] = faultCode_1;							//故障码	
					
					printf("\r\n-----> 上传故障码\r\n");	
					printf("\r\n故障码 = %d\r\n", faultCode_1);	
					break;
			}
			break;

//-------------------------------------成都稳重台-------------------------------------			
		case 6008:			//成都称重台称重协议
		
			break;
		
//-------------------------------------上传用户投放信息（含开关门的桶内重量）-------------------------------------		
		case 6011:		//上传投放数据 
			GPRS_DATA_BUF[proLen++] = sendPutInType;						//身份识别方式
			GPRS_DATA_BUF[proLen++] = sendDataLength;						//发送身份信息的长度
			memcpy(&GPRS_DATA_BUF[proLen], sendDataBuf, 33);		//加载身份信息
			proLen += 33;
			
// 			printf("\r\nsendUserData:");
// 			show(sendDataBuf, 33);
			
			switch(deviceType)
			{
				case DOUBLE_CLASSIFICATION:		//双桶480L
					//加载左桶投放上传数据
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//垃圾类型（玻璃类）
					
					GPRS_DATA_BUF[proLen++] = getWeight1 >> 8;				//重量高8位
					GPRS_DATA_BUF[proLen++] = getWeight1;
					
					GPRS_DATA_BUF[proLen++] = getDistance1;						//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore1 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore1;
					GPRS_DATA_BUF[proLen++] = getWeightAfter1 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter1;
					
					ShowUploadAllData(leftBarrelType, getWeight1, getDistance1, getWeightBefore1, getWeightAfter1);
					
					//加载右桶投放上传数据
					GPRS_DATA_BUF[proLen++] = rightBarrelType;											//垃圾类型（金属）
					
					GPRS_DATA_BUF[proLen++] = getWeight2 >> 8;				//重量高8位
					GPRS_DATA_BUF[proLen++] = getWeight2;
					
					GPRS_DATA_BUF[proLen++] = getDistance2;						//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore2 >> 8;				//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore2;
					GPRS_DATA_BUF[proLen++] = getWeightAfter2 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter2;
					
					ShowUploadAllData(rightBarrelType, getWeight2, getDistance2, getWeightBefore2, getWeightAfter2);
					break;
					
				case DOUBLE_CLASSIFICATION_BOTTLE:		//双桶480L含瓶子	
					//加载左桶投放上传数据
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//可回收物类型
					
					GPRS_DATA_BUF[proLen++] = smallBottleCount >> 8;		//小塑料瓶数量（高8位）
					GPRS_DATA_BUF[proLen++] = smallBottleCount;					//小塑料瓶数量（低8位）
					
					GPRS_DATA_BUF[proLen++] = getDistance1;							//测距值
					
					GPRS_DATA_BUF[proLen++] = 0;												//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = 0;
					GPRS_DATA_BUF[proLen++] = 0;												//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = 0;
					
					ShowUploadAllData(leftBarrelType, smallBottleCount, getDistance1, 0, 0);
					
					//加载右桶投放上传数据
					GPRS_DATA_BUF[proLen++] = rightBarrelType;					//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight2 >> 8;					//重量高8位
					GPRS_DATA_BUF[proLen++] = getWeight2;
					
					GPRS_DATA_BUF[proLen++] = getDistance2;							//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore2 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore2;
					GPRS_DATA_BUF[proLen++] = getWeightAfter2 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter2;
					
					ShowUploadAllData(rightBarrelType, getWeight2, getDistance2, getWeightBefore2, getWeightAfter2);
					break;
					
				case SINGLE_CLASSIFICATION:		//单桶660L
					//加载左桶投放上传数据
					GPRS_DATA_BUF[proLen++] = leftBarrelType;						//垃圾类型

					GPRS_DATA_BUF[proLen++] = getWeight1 >> 8;					//重量高8位
					GPRS_DATA_BUF[proLen++] = getWeight1;
	
					GPRS_DATA_BUF[proLen++] = getDistance1;							//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore1 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore1;
					GPRS_DATA_BUF[proLen++] = getWeightAfter1 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter1;
					
					ShowUploadAllData(leftBarrelType, getWeight1, getDistance1, getWeightBefore1, getWeightAfter1);
					break;
					
				case THREE_CLASSIFICATION:		//三分类
					//加载第1通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel1Type;						//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight1 >> 8;						//重量
					GPRS_DATA_BUF[proLen++] = getWeight1;
					
					GPRS_DATA_BUF[proLen++] = getDistance1;								//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore1 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore1;
					GPRS_DATA_BUF[proLen++] = getWeightAfter1 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter1;
					
					ShowUploadAllData(devChannel1Type, getWeight1, getDistance1, getWeightBefore1, getWeightAfter1);
					
					//加载第2通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel2Type;						//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight2 >> 8;						//重量
					GPRS_DATA_BUF[proLen++] = getWeight2;
					
					GPRS_DATA_BUF[proLen++] = getDistance2;								//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore2 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore2;
					GPRS_DATA_BUF[proLen++] = getWeightAfter2 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter2;
					
					ShowUploadAllData(devChannel2Type, getWeight2, getDistance2, getWeightBefore2, getWeightAfter2);
					
					//加载第3通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel3Type;						//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight3 >> 8;						//重量
					GPRS_DATA_BUF[proLen++] = getWeight3;
					
					GPRS_DATA_BUF[proLen++] = getDistance3;								//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore3 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore3;
					GPRS_DATA_BUF[proLen++] = getWeightAfter3 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter3;
					
					ShowUploadAllData(devChannel3Type, getWeight3, getDistance3, getWeightBefore3, getWeightAfter3);
					break;
					
				case FOUR_CLASSIFICATION:		//四分类
					//加载第1通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel1Type;						//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight1 >> 8;						//重量
					GPRS_DATA_BUF[proLen++] = getWeight1;
					
					GPRS_DATA_BUF[proLen++] = getDistance1;								//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore1 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore1;
					GPRS_DATA_BUF[proLen++] = getWeightAfter1 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter1;
					
					ShowUploadAllData(devChannel1Type, getWeight1, getDistance1, getWeightBefore1, getWeightAfter1);
					
					//加载第2通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel2Type;						//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight2 >> 8;						//重量
					GPRS_DATA_BUF[proLen++] = getWeight2;
					
					GPRS_DATA_BUF[proLen++] = getDistance2;								//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore2 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore2;
					GPRS_DATA_BUF[proLen++] = getWeightAfter2 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter2;
					
					ShowUploadAllData(devChannel2Type, getWeight2, getDistance2, getWeightBefore2, getWeightAfter2);
					
					//加载第3通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel3Type;						//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight3 >> 8;						//重量
					GPRS_DATA_BUF[proLen++] = getWeight3;
					
					GPRS_DATA_BUF[proLen++] = getDistance3;								//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore3 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore3;
					GPRS_DATA_BUF[proLen++] = getWeightAfter3 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter3;
					
					ShowUploadAllData(devChannel3Type, getWeight3, getDistance3, getWeightBefore3, getWeightAfter3);
					
					//加载第4通道投放数据
					GPRS_DATA_BUF[proLen++] = devChannel4Type;						//垃圾类型
					
					GPRS_DATA_BUF[proLen++] = getWeight4 >> 8;						//重量
					GPRS_DATA_BUF[proLen++] = getWeight4;
					
					GPRS_DATA_BUF[proLen++] = getDistance4;								//测距值
					
					GPRS_DATA_BUF[proLen++] = getWeightBefore4 >> 8;			//投放前桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightBefore4;
					GPRS_DATA_BUF[proLen++] = getWeightAfter4 >> 8;				//投放后桶内总重量
					GPRS_DATA_BUF[proLen++] = getWeightAfter4;
					
					ShowUploadAllData(devChannel4Type, getWeight4, getDistance4, getWeightBefore4, getWeightAfter4);
					break;
					
				default:
					break;
			}					
			break;

	}
	//流水号
	GPRS_DATA_BUF[proLen++] = (uint8_t)serial_number >> 8;
	GPRS_DATA_BUF[proLen++] = (uint8_t)serial_number;
	
	crc = CRC_Check(&GPRS_DATA_BUF[6], proLen - 6);		//除去帧头和长度6个字节
	
	//CRC校验码
	GPRS_DATA_BUF[proLen++] = crc;
	
	//发送的数据长度
	dataLen = proLen - 6;				//协议总长度 - （4字节帧头 + 2字节长度）
	
	GPRS_DATA_BUF[4] = (uint8_t)(dataLen >> 8);
	GPRS_DATA_BUF[5] = (uint8_t)dataLen;
	
	printf("\r\nTCP --> No = %4d; Len = %d\r\n", proNum, proLen);
// 	show(GPRS_DATA_BUF, proLen);
	
	serial_number++;
	return proLen;							//返回本协议的总长度
}

/*
***********************************************************************
*	函 数 名: GetOrderCode
*	功能说明: 取得指令码
*	形    参：无
*	返 回 值: 接收到数据的指令
***********************************************************************
*/
uint16_t GetOrderCode(uint8_t *pInputData)
{
	uint16_t returnData;
	returnData = pInputData[14] << 8;
	returnData |= pInputData[15];
	return returnData;
}

/*
***********************************************************************
*	函 数 名: CheckOrderCodeUploadAck
*	功能说明: 检查指令上传是否成功应答
*	形    参：无
*	返 回 值: 接收到数据的指令
***********************************************************************
*/
uint8_t CheckOrderCodeUploadAck(uint16_t upCmd, uint16_t ackCmd)
{
	uint8_t returnSta = 0;
	
	switch(upCmd)
	{
		case 1001: if(ackCmd == 10011) returnSta = 1; break;
		case 1003: if(ackCmd == 1003) returnSta = 1; break;
		case 2001: if(ackCmd == 2001) returnSta = 1; break;
		case 2003: if(ackCmd == 2003) returnSta = 1; break;
		case 4001: if(ackCmd == 4002) returnSta = 1; break;
		case 4011: if(ackCmd == 4022) returnSta = 1; break;
		case 4201: if(ackCmd == 4202) returnSta = 1; break;
		case 4301: if(ackCmd == 4302) returnSta = 1; break;
		case 4401: if(ackCmd == 4402) returnSta = 1; break;
		case 4111: if(ackCmd == 4222) returnSta = 1; break;
		case 6011: if(ackCmd == 60011) returnSta = 1; break;
		case 6021: if(ackCmd == 6021) returnSta = 1; break;
		case 4701: if(ackCmd == 4702) returnSta = 1; break;
		case 4801: if(ackCmd == 4802) returnSta = 1; break;
		case 4901: if(ackCmd == 4901) returnSta = 1; break;
		case 5001: if(ackCmd == 5001) returnSta = 1; break;
		case 5002: if(ackCmd == 5002) returnSta = 1; break;
		case 6004: if(ackCmd == 60041) returnSta = 1; break;
			
			
		default:
			break;
	}
	return returnSta;
}

/*
***********************************************************************
*	函 数 名: ProtocolParsing
*	功能说明: 对接收的数据进行协议解析
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void TCP_Protocol(uint8_t* pCdmStr, uint8_t length)
{
	char *p;
	uint8_t *pdata;						//服务器返回的数据指针
	uint32_t dataTemp;
	uint8_t tempData[32];
	
// 	p = (char*)strstr((const char*)pCdmStr, ":");
// 	pdata = (uint8_t*)(p + 1);
	
	pdata = pCdmStr;

	if((*(pdata) == 'H') && (*(pdata+1) == 'E') && (*(pdata+2) == 'R') && (*(pdata+3) == 'O'))	//是合法的协议
	{
		TCP_recreceiveStatus = 1;														//标志接收到TCP数据
		recreceiveOrderCode = GetOrderCode(pdata);					//解析出对应的指令码	
		
		//打印接收到的后台数据
// 		printf("\r\nS -> %d \r\n", recreceiveOrderCode);
// 		show(pCdmStr, length);
		
		if(CheckOrderCodeUploadAck(sendOrderCode, recreceiveOrderCode))
		{
			receiveDataMark = 1;
		}
		
		switch(recreceiveOrderCode)		//根据指令码进行内容的解析
		{
			case 10011:
				pdata += 16;
				getDeviceWorkStatusVal = *(pdata++);
				getDeviceWorkStatusMark = 1;
				break;
				
			case 4002:				//服务器返回用户查询信息
				//取得是否通过验证
				pdata += 16;
				isAuth = *(pdata++);
				
				//用户ID
				dataTemp = *(pdata++) << 24;
				dataTemp |= *(pdata++) << 16;
				dataTemp |= *(pdata++) << 8;
				dataTemp |= *(pdata++);
				userID = dataTemp;
				
				//用户账号
				dataTemp = *(pdata++) << 24;
				dataTemp |= *(pdata++) << 16;
				dataTemp |= *(pdata++) << 8;
				dataTemp |= *(pdata++);
				accountNum = dataTemp;
				
				//取得昵称的长度
				nickNameLength = *(pdata++);
				if(nickNameLength > 64)
				{
					printf("\r\nData overflow\r\n");
					memcpy(nickName, pdata, 64);
					
				}else{
					//取得昵称内容
					memcpy(nickName, pdata, nickNameLength);
				}
				pdata += nickNameLength;
				
				//取得资金
				dataTemp = *(pdata++) << 24;
				dataTemp |= *(pdata++) << 16;
				dataTemp |= *(pdata++) << 8;
				dataTemp |= *(pdata++);
				capital = dataTemp;
				
				//取得提货码数量
				residue = *(pdata++);
				
				userInfoQueryStatus = 1;					//用户信息查询成功，由外部清除
				
// 				printf("\r\nisAuth = %x \r\n", isAuth);
// 				printf("\r\nuserID = %4x \r\n", userID);
// 				printf("\r\naccountNum = %4x \r\n", accountNum);
// 				printf("\r\nnickNameLength = %x \r\n", nickNameLength);
// 				show(nickName, 20);
// 				printf("\r\ncapital = %4x \r\n", capital);
// 				printf("\r\nresidue = %x \r\n", residue);	
				break;
				
			//服务器返回用户查询信息（包含有用户的固定身份二维码信息）
			case 4022:				//前期的协议与兑换机的库存上传重复了,用4402代替
			case 4202:				//获取用户信息用于POS机授权控制开门投放次数（返回信息包含垃圾袋二维码）
			case 4302:				//用于分区域控制是否能打开投放口门
			case 4402:				//替换4011（返回信息包含垃圾袋二维码，用于打印或绑定人脸）
				//取得是否通过验证
				pdata += 16;
				isAuth = *(pdata++);
				
				//用户ID
				dataTemp = *(pdata++) << 24;
				dataTemp |= *(pdata++) << 16;
				dataTemp |= *(pdata++) << 8;
				dataTemp |= *(pdata++);
				userID = dataTemp;
				
				//用户账号
				dataTemp = *(pdata++) << 24;
				dataTemp |= *(pdata++) << 16;
				dataTemp |= *(pdata++) << 8;
				dataTemp |= *(pdata++);
				accountNum = dataTemp;
				
				//取得昵称的长度
				nickNameLength = *(pdata++);
				if(nickNameLength > 64)
				{
					printf("\r\nData overflow\r\n");
					memcpy(nickName, pdata, 64);
					
				}else{
					//取得昵称内容
					memcpy(nickName, pdata, nickNameLength);
				}
				pdata += nickNameLength;
				
				//取得资金
				dataTemp = *(pdata++) << 24;
				dataTemp |= *(pdata++) << 16;
				dataTemp |= *(pdata++) << 8;
				dataTemp |= *(pdata++);
				capital = dataTemp;
				
				//取得提货码数量
				residue = *(pdata++);
				
				//取得用户ID二维码信息（用户打印二维码）
				barcodeLength = *(pdata++);
				memset(tempData, 0, 32);
				
				if(barcodeLength > 32)
				{
					printf("\r\nData overflow\r\n");
					memcpy(tempData, pdata, 32); 
				}else{
					memcpy(tempData, pdata, barcodeLength); 
				}
				
				sprintf((char*)barcode, "yueng=%s", (char*)tempData);		//后台返回的用户ID二维码没有包含头部信息"yueng="
				barcodeLength += 6;	
				
				userInfoQueryStatus = 1;					//用户信息查询成功，由外部清除
// 				
// 				printf("\r\nisAuth = %x \r\n", isAuth);
// 				printf("\r\nuserID = %4x \r\n", userID);
// 				printf("\r\naccountNum = %4x \r\n", accountNum);
// 				printf("\r\nnickNameLength = %x \r\n", nickNameLength);
// 				show(nickName, 20);
// 				printf("\r\ncapital = %4x \r\n", capital);
// 				printf("\r\nresidue = %x \r\n", residue);	
// 				printf("\r\nbarcode = %s \r\n", barcode);	
				break;
				
			case 4702:		//收运账号认证应答
				pdata += 16;
				
				boxOpenAckStatus = *(pdata++);
				getBoxOpenAckMark = 1;
				break;
			
			case 60011:
				pdata += 16;
				//取得资金
				dataTemp = *(pdata++) << 24;
				dataTemp |= *(pdata++) << 16;
				dataTemp |= *(pdata++) << 8;
				dataTemp |= *(pdata++);
				capitalAfter = dataTemp;
				
				putInUploadSuccessMark = 1;
				
				break;
				
			case 60041:		//后台应答设备状态上传
				pdata += 16;
				
				getDevChannelNumbers = *(pdata++);
				
				if((getDevChannelNumbers >= 1) && (getDevChannelNumbers <= 4))
				{
					getDevChannel1Type = *(pdata++);
					getDevChannel1DisDepthVal = *(pdata++);
					getDevChannel1DisMaxVal = *(pdata++);
					getDevChannel1DisAlarmPercent = *(pdata++);

					if(getDevChannelNumbers >= 2)
					{
						getDevChannel2Type = *(pdata++);
						getDevChannel2DisDepthVal = *(pdata++);
						getDevChannel2DisMaxVal = *(pdata++);
						getDevChannel2DisAlarmPercent = *(pdata++);
					}

					if(getDevChannelNumbers >= 3)
					{
						getDevChannel3Type = *(pdata++);
						getDevChannel3DisDepthVal = *(pdata++);
						getDevChannel3DisMaxVal = *(pdata++);
						getDevChannel3DisAlarmPercent = *(pdata++);
					}

					if(getDevChannelNumbers >= 4)
					{
						getDevChannel4Type = *(pdata++);
						getDevChannel4DisDepthVal = *(pdata++);
						getDevChannel4DisMaxVal = *(pdata++);
						getDevChannel4DisAlarmPercent = *(pdata++);
					}

					getSetDistanceParamMark = 1;
				}else{}
				
				break;
				
			default:
				break;
		}
	}
	memset(GPRS_RX_BUF, 0, GPRS_MAX_RECV_LEN);
}

/*
***********************************************************************
*	函 数 名: SetSendProcotolSuccess
*	功能说明: 设置本次发送的协议已成功发出
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void SetSendProcotolSuccess(void)
{
	switch(sendOrderCode)
	{
		case 6008:
// 			printf("\r\n上传成功\r\n");
// 			uploadWightMark = 1;
// 			SendScaleUploadOkMark();
			break;
			
		default:
			break;
	}
}

/******************** (C) COPYRIGHT 2017 CXHY *****END OF FILE*******************/ 
