⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdacomm.cpp

📁 采用LPC的芯片做的气体排放检测,包括气体成分分析,数据的处理,数据传输给工作站.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//MdaComm.cpp
#include "StdAfx.h"
#ifndef _AFXDLL
	#include "config.h"
#endif
#define		_GET_CO2_
#define		FLOAT_LAMBDA
extern		U8		g_ucKeyDownData;
//7E 01 F4 0A 7E 1C 00 01 16 A9 00 03 07 00 00 00 00 00 00 0C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0D
//7E 01 F4 0A 7E 1C 00 01 16 A9 00 03 07 00 00 00 00 00 00 0C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0D
//NO平台通讯协议
const        U8 ZERO_calibration[]={0x04,0x01,0x20,0xdb};
const        U8 GET_NOdata[]={0x04,0x01,0x40,0xbb};
const        U8 Zero_replay[]={0x06,0x20,0x00,0xda};
const        U8 Span_replay[]={0x06,0x11,0x00,0xe9};
U8 SPAN_calibration[7]={0x04,0x04,0x11,0x00,0x00,0x00,0x00};
//5气平台通讯协议        
const	U8 GetPenuVar[]={0x7e,0x01,0xf4,0x0a,0x7e,0x1c, 0,1, 0x16,0xa9,
						 0,3,7,	0,0,0,0,0,0,		0x0c,
						 0,0,0,0,0,0,0,0,0,0,0,0,0,0,		0x0D	};

const U8 get_data[]      ={0x7e,0x01,0xf4,0x0a,0x7e,0x04,0x00,0x01,0x02,0x02,0xf6};
const U8 zero_data[]     ={0x7e,0x01,0xf4,0x0a,0x7e,0x04,0x00,0x01,0x16,0x45,0x9f};
const U8 After_zero_data[]={0x7e,0x05,0x01,0x01,0x16,0x00,0x00,0xE2};

#ifdef HWD_FGA_4100A
	const U8 pumb_on_data[12]  ={0x7e,0x01,0xf4,0x0a,0x7e,0x05,0x00,0x01,0x15,0xe8,0x00,0xfc};
	const U8 pumb_off_data[12] ={0x7e,0x01,0xf4,0x0a,0x7e,0x05,0x00,0x01,0x15,0xe8,0x22,0xda};
#else
	const U8 pumb_on_data[17]  ={0x7e,0x01,0xf4,0x0a,0x7e,0x0a,0x00,0x01,0x0e,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0xe3};
	const U8 pumb_off_data[17] ={0x7e,0x01,0xf4,0x0a,0x7e,0x0a,0x00,0x01,0x0e,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0xe4};
#endif

const U8 get_co2_data[11]  ={0x7e,0x01,0xf4,0x0a,0x7e,0x04,0x00,0x01,0x05,0x06,0xef};
const U8 calib_span_reset[]  ={	0x7e,0x01,0xf4,0x0a,0x7e,
								0x09,0x00,0x01,0x02,0x06,		//CHECKSUM
								0x01,0x01,0x01,0x01,0x01,		0xe8};
										//	7E 01 F4 0A 7E        04    00   01 05 06 EF
const U8 FGA4100_RIGHT_ANSWER[]={0x7e,0x01,0xc6,0x38,0x7e,0x01,0x81,0x7d,
								0x7e,0x28,0x01,0x01};

const U8 get_calib_span_data[]={0x7E,0x01,0xF4,0x0A,0x7E,0x24,0x00,0x01,0x16,0xA9,0x00,0x07,0x09,0x00,
					0x00,0x00,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
					0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF7};
const U8 set_calib_conc_data[]={0x7E,0x01,0xF4,0x0A,0x7E,0x19,0x00,0x01,0x02,0x05,0x00};
const U8 get_PEF_value[]={0x7E,0x01,0xF4,0x0A,0x7E,0x14,0x00,0x01,0x16,0xA9,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x1F};
const U8 get_AD_COUNT[]={0x7E,0x01,0xF4,0x0A,0x7E,0x04,0x00,0x01,0x14,0xB6,0x30};
const U8 get_COMP_DATA[]={0x7E,0x01,0xF4,0x0A,0x7E,0x04,0x00,0x01,0x05,0x06,0xef};

const U8 NVRamCmdCode[]={0x7e,0x01,0xf4,0x0a,0x7e,
	//LENGTH00H 01H 16H  A9H   MODE TYPE SUBTYPE
	0x0,   0x0,   0x1,0x16,0xA9,0x01};
extern	_VEHICLE_INFO		g_VehicleInfo;
BOOL	g_bDevConnected;

void	FloatToU8(void *pDest,void *pSrc)
{
	U8 *p1,*p2;
	p1 = (U8*)pDest;
	p2 = (U8*)pSrc;
	p1[0] = p2[3];
	p1[1] = p2[2];
	p1[2] = p2[1];
	p1[3] = p2[0];
}
/**********************************************************
**根据国标<<GB 18285-2005>>Page12的计算公式,编写此函数
**********************************************************/
#ifdef FLOAT_LAMBDA
double CalLambda(double HCVAL,double COVAL,double CO2VAL,double O2VAL,unsigned char Fuel_Index)
{
	double THC,K,Hcv,Ocv,Up_data,Down_data,Lambda,temp1;
	THC      =	6*HCVAL/10000;
	K        =	COVAL/CO2VAL;
	
	if(Fuel_Index==enuFuelType_Petro)  //汽油
	{Hcv=1.85;Ocv=0.0176;}
	else
	if(Fuel_Index==enuFuelType_LPG)   //LPG
	{ Hcv=2.525;Ocv=0; }
	else
	if(Fuel_Index==enuFuelType_CNG)    //CNG
	{ Hcv=4.0;Ocv=0;}
	else			//enuFuelType_Alcohol
	{ Hcv=3.0;Ocv=0.5;}	//酒精
	temp1 = Hcv/4*3.5/(3.5+K);
	temp1 = (temp1-Ocv/2)*(COVAL+CO2VAL);
	//Up_data  =CO2VAL+COVAL/2+O2VAL+(Hcv/4*3.5/(3.5+K)-Ocv/2)*(COVAL+CO2VAL);
	Up_data	= CO2VAL+COVAL/2+O2VAL+ temp1;
	Down_data=(1+Hcv/4-Ocv/2)*(COVAL+CO2VAL+ THC);
	if((Up_data<0)||(CO2VAL<3.0)||(Down_data<0.1))
	     Lambda=0.0;
	else
	     Lambda   =Up_data/Down_data;
	if(Lambda>2.0)
	     Lambda=2.0;
	return Lambda;
}
#endif
	float	GetFloatByUnion(U8* Buf)
	{
		U8	myBuf[8];
		myBuf[0] = Buf[3];
		myBuf[1] = Buf[2];
		myBuf[2] = Buf[1];
		myBuf[3] = Buf[0];
		return *(float*)myBuf;
	}
#ifdef _GET_CO2_
#endif
/*===================================**
**	处理0x7e:	1.rxd:接收要过滤
				2.txd:发送要重发
**===================================*/
void	Proccess_0x7E(U8* pReadData,U32* pucLen,BOOL	bRxd)
{
	U32	i,j,nLen;
	U8	Buf[256];
	nLen = (* pucLen);
	if(bRxd){							//1.rxd:接收要过滤
					//i = *(U32*)pReadData;
					if((pReadData[3] == 0x3c) ||(pReadData[2] == 0xc2))	{	
						i=4;
						(* pucLen) -= 4;
		#ifndef HWD_FGA_4100A
							#ifdef WIN32
									S8	RecordBuf[256];
									int j;
									memset(RecordBuf,0,sizeof(RecordBuf));
									sprintf(RecordBuf,"%2x,%2x,%2x,%2x\r\n",pReadData[0],pReadData[1],pReadData[2],pReadData[3]);
									j = strlen(RecordBuf);
									FILEWrite2TXT((U8*)RecordBuf,j);
							#endif
		#endif
					}else	i=0;
		for (j=0;i<nLen;i++,j++){
			Buf[j] = pReadData[i];
			if((pReadData[i]==0x7e) && (pReadData[i+1]==0x7e)) {
				i++;
				(* pucLen)--;
			}
		}
		memcpy(pReadData,Buf,(* pucLen));
	}else{								//2.txd:发送要重发
		for (i=5,j=0;i<nLen;i++,j++){
			Buf[j] = pReadData[i];
			if(pReadData[i]==0x7e) {
				j++;
				Buf[j] = 0x7e;
				(* pucLen)++;
			}
		}
		memcpy(pReadData+5,Buf,(* pucLen)-5);
	}
}
unsigned char	 GetCheckSum(unsigned char* pByte,unsigned int nLen)
{
	unsigned char j;
	unsigned int  i;
	j=0;
	for(i=0;i<nLen;i++)
		j += pByte[i];
	return j;
}
U16	Char4ToShort(U8* psrcBYTE)
{
	if((psrcBYTE[0]<'0')||(psrcBYTE[0]>'9'))		return 0;
	return 	(U16)((psrcBYTE[0]-'0')*1000+(psrcBYTE[1]-'0')*100
			+(psrcBYTE[2]-'0')*10+(psrcBYTE[3]-'0'));
}
void SendCmdToDev(U8 ucCmdCode,U8* pSendBuf,float* pf,U8 uc_rt)	
{
	U8	uctmp;
	U32	nLen;
	switch(ucCmdCode)
	{
	case enuFGA4100Cmd_GetData:
		#ifdef _AFXDLL
				FFPortSendData(uc_rt,(const char *)get_data,sizeof(get_data));
		#else
				SendBlock(uc_rt,get_data,sizeof(get_data));
		#endif		
		return;
	case enuFGA4100Cmd_Zero:
		#ifdef _AFXDLL		
				FFPortSendData(uc_rt,(const char *)zero_data,sizeof(zero_data));
		#else
				SendBlock(uc_rt,zero_data,sizeof(zero_data));
		#endif		
		return;
	case enuFGA4100Cmd_PumpOn:
		#ifdef _AFXDLL		
				FFPortSendData(uc_rt,(const char *)pumb_on_data,sizeof(pumb_on_data));	
		#else
				SendBlock(uc_rt,pumb_on_data,sizeof(pumb_on_data));	
		#endif		
		return;
	case enuFGA4100Cmd_PumpOff:
		#ifdef _AFXDLL		
				FFPortSendData(uc_rt,(const char *)pumb_off_data,sizeof(pumb_off_data));
		#else
				SendBlock(uc_rt,pumb_off_data,sizeof(pumb_off_data));
		#endif		
		return;
	case enuFGA4100Cmd_ReadComp:
	case enuFGA4100Cmd_GetCO2:
		#ifdef _AFXDLL		
				FFPortSendData(uc_rt,(const char *)get_co2_data,sizeof(get_co2_data));	
		#else
				SendBlock(uc_rt,get_co2_data,sizeof(get_co2_data));	
		#endif		
		return;
	case enuFGA4100Cmd_ReadSpan:
		#ifdef _AFXDLL		
				FFPortSendData(uc_rt,(const char*)get_calib_span_data,sizeof(get_calib_span_data));	
		#else
				SendBlock(uc_rt,get_calib_span_data,sizeof(get_calib_span_data));	
		#endif		
		return;	
	case enuFGA4100Cmd_CalibReset:
		#ifdef _AFXDLL		
				FFPortSendData(uc_rt,(const char*)calib_span_reset,sizeof(calib_span_reset));	
		#else
				SendBlock(uc_rt,calib_span_reset,sizeof(calib_span_reset));	
		#endif		
		return;	
	case enuFGA4100Cmd_CalibStart://这里包含浮点数值,校准浓度值以浮点表示
		memcpy(pSendBuf,(U8*)set_calib_conc_data,11);
		for (uctmp = 11;uctmp < 11+20;uctmp+=4)	{
			FloatToU8(((U8*)pSendBuf+uctmp),pf);
			pf++;
		}
		uctmp = GetCheckSum(pSendBuf+5,26);
		*(pSendBuf+31) = ~uctmp;
		nLen = 32;
		break;	
	case enuFGA4100Cmd_GetP_E_F:
	#ifdef _AFXDLL		
			FFPortSendData(uc_rt,(const char*)get_PEF_value,sizeof(get_PEF_value));	
	#else
			SendBlock(uc_rt,get_PEF_value,sizeof(get_PEF_value));	
	#endif		
		return;	
	case enuFGA4100Cmd_GetPenuVar:
	#ifdef _AFXDLL		
			FFPortSendData(uc_rt,(const char*)GetPenuVar,sizeof(GetPenuVar));	
	#else
			SendBlock(uc_rt,GetPenuVar,sizeof(GetPenuVar));	
	#endif		
		return;	
	case enuFGA4100Cmd_ReadAD_Count:
	#ifdef _AFXDLL		
			FFPortSendData(uc_rt,(const char*)get_AD_COUNT,sizeof(get_AD_COUNT));	
	#else
			SendBlock(uc_rt,get_AD_COUNT,sizeof(get_AD_COUNT));	
	#endif		
		return;

	case enuFGA4100Cmd_WriteGrandO2:	//=19
		memcpy(pSendBuf,(U8*)NVRamCmdCode,sizeof(NVRamCmdCode));	
		//Buf[10] =			//	mode;	已经固定为 写(1)
		pSendBuf[11] = NV_GRANDO2;			//	type;
		pSendBuf[12] = 0;			//	subType;			//subtpe[6]
		memset(pSendBuf+13,0,13);
		pSendBuf[19] = 4;			//parlength1;
		pSendBuf[5]	= 16 +pSendBuf[19];
		FloatToU8(((U8*)pSendBuf+22),pf);
		uctmp=GetCheckSum(pSendBuf+5,17+pSendBuf[19]);
		nLen = 22+pSendBuf[19];
		pSendBuf[nLen] = (~uctmp);
		nLen ++;
		break;
	case enuFGA4100Cmd_WritePenuVar:	//=20
		memcpy(pSendBuf,(U8*)NVRamCmdCode,sizeof(NVRamCmdCode));	
		//Buf[10] =			//	mode;	已经固定为 写(1)
		pSendBuf[11] = NV_PNEUMATICS;			//	type;
		pSendBuf[12] = NV_SUBTYPE_PNEU_VAR;			//	subType;			//subtpe[6]
		memset(pSendBuf+13,0,13);
		pSendBuf[19] = 12;			//parlength1;
		pSendBuf[5]	= 16 +pSendBuf[19];
		for (uctmp = 22;uctmp < (22+12);uctmp+=4)	{
			FloatToU8(((U8*)pSendBuf+uctmp),pf);
			pf++;
		}
		uctmp=GetCheckSum(pSendBuf+5,17+pSendBuf[19]);
		nLen = 22+pSendBuf[19];
		pSendBuf[nLen] = (~uctmp);
		nLen ++;
		break;
	default:break;
	}	
		Proccess_0x7E(pSendBuf,&nLen,0);
		#ifdef _AFXDLL		
				FFPortSendData(uc_rt,(const char*)pSendBuf,nLen);		
		#else
				SendBlock(uc_rt,pSendBuf,nLen);	
		#endif	
}

BOOL	 ReadDevReply(unsigned char ucCmdCode,void* pReadData,unsigned int* pReadBufLen,U8 uc_rt)
{
	U32	nLen;
	ucCmdCode = ucCmdCode;
	if( FFPortGetRxBufDataLen(uc_rt)>= *pReadBufLen)
	{
		nLen=FFPortGetRxBufDataLen(uc_rt);
		if((nLen) > 240)	return FALSE;
		
		*pReadBufLen=nLen;
		FFPortReadData(uc_rt,(unsigned char*)pReadData,nLen);
		return TRUE;
	}
	return FALSE;
}
BOOL  TranslateRawData(unsigned char ucCmdCode,
					unsigned char* pReadData,
					_DEV_FGA4100_DATA* pWriteData,
					unsigned int nReadBufLen,
					float* pf)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -