WORD CHexFileParser::GetHighAddrFromFile(const CString& strLine)
{	
	WORD dwTemp = 0;
	char ch[128] = {0};
	for (int i = 1; i < strLine.GetLength(); i++)
	{
		ch[i-1] = (char)strLine.GetAt(i);
	}
	turn_hex_file_line_to_unsigned_char(ch);
	TS_UC szBuf[64] = {0};
	turn_int_to_unsigned_char(ch, strLine.GetLength()-1, szBuf);
	dwTemp = szBuf[4]*0x100 + szBuf[5];

	return dwTemp;
}
////////////////////////////////////////////////////////////////////////////
// Parameter:
// 1, CFile&  : hex file preference, to be read
// 2, char*  : a memory pointer, has been allocate in heep, it's a valid memory space.
// 3, int      :  parameter 2, memory space's size.
// return:
// int			: it's the truely length of the used buffer, also useful byte number after hex file been parsed.
int CHexFileParser::ReadNormalHexFile( CFile& hexFile,  char* pBuf, int nBufLen)
{
	ASSERT(nBufLen >= 0xFFFF);
	//CString m_get_data_from_file;
	int nBufCount = 0;
//*****************inspect the file*********************
	unsigned int nLineNum=0;

	char a[256];
	ZeroMemory(a, 256);
	
	//while(NULL!=ar.ReadString(m_get_data_from_file))//循环读取文件,直到文件结束
	while(ReadLineFromFile(hexFile, a))
	{//get a line from the file,check "crc" for total file
	
#ifdef _DEBUG
		if (a[8] != '0')
		{
			int n = 0;
		}
#endif

		nLineNum++;						//the line number that the wrong hex file;
		TS_UC get_hex[128]={0};		//get hex data,it is get from the line char
	
		UINT i = 0;
		for( i=0;i<strlen(a); i++)		//get a line//去掉第一个
		{
			a[i] = a[i+1];
		}
		int nLen = strlen(a)-2;			// 减去回车换行符号
		if(strlen(a)%2==0)
			turn_hex_file_line_to_unsigned_char(a);		// turn every char to int 
		else
		{
			wchar_t p_c_temp[74]={'\0'};
			swprintf_s(p_c_temp,_T("Error: the hex file had error at %d line!"),nLineNum);
			nBufCount = 0;
			if(!auto_flash_mode)
				AfxMessageBox(p_c_temp);
			//close_com();
			return 0;
		}
		turn_int_to_unsigned_char(a,nLen,get_hex);	//turn to hex 
		if(get_hex[3]==1)	//for to seektobegin() function,because to end of the file
			break;

		TS_UC uctemp2=0;
		for(int j=0;j<(nLen/2);j++)
			uctemp2+=get_hex[j];
		if(uctemp2%256!=0)
		{
			wchar_t p_c_temp[74]={'\0'};
			swprintf_s(p_c_temp,_T("Error: the hex file had error at %d line!"),nLineNum);
			nBufCount = 0;
			if(!auto_flash_mode)
				AfxMessageBox(p_c_temp,MB_OK);
			//close_com();
			return 0;
		}
		if(get_hex[1]==0 && get_hex[2]==0)
			get_hex[4]=255;	//for the 0000 register to write 255
		unsigned int ltemp;
		ltemp=get_hex[1]*256+get_hex[2];

		for(int j=0;j<get_hex[0];j++)
		{
			pBuf[ltemp+j]=get_hex[4+j];//get the data
		}

		if((UINT)nBufCount<(ltemp+get_hex[0]))
		{
			nBufCount=ltemp+get_hex[0]-1;
		}

		ZeroMemory(a, 256);
	}//end while


	hexFile.Close();//close the file		
	
	return nBufCount;
}
////////////////////////////////////////////////////////////////////////////
// Parameter:
// 1, CFile&  : hex file preference, to be read
// 2, char*  : a memory pointer, has been allocate in heep, it's a valid memory space.
// 3, int      :  specify parameter 2, memory space's size.
// return:
// int			: it's the truely length of the used buffer, also useful byte number after hex file been parsed.
// 读扩展线性地址记录的hex file-最大32位的地址空间
BOOL CHexFileParser::ReadExtLinearHexFile(CFile& hexfile, char* pBuf, int nBufLen)
{
	//CArchive ar(&m_hex_file,CArchive::load);
	//ASSERT(nBufLen >= 0xFFFF);
	if (pBuf)
	{
		delete[] pBuf;
		pBuf = NULL;
	}

	pBuf = new char[c_nHexFileBufLen];
		
	CString strGetData;
	int nBufCount = 0;
//*****************inspect the file*********************
	unsigned int nLineNumErr=0;

	DWORD dwHiAddr = 0; // 高位地址
	char a[256];
	ZeroMemory(a, 256);
	
	//while(NULL!=ar.ReadString(strGetData))	//循环读取文件,直到文件结束
	while(ReadLineFromFile(hexfile, a))
	{
		// 取得高位地址,可能不止一处扩展
		if( a[8] == '4')
		{
			CString strTemp(a);
			dwHiAddr = GetHighAddrFromFile(strTemp);
			dwHiAddr <<= 16;

			if( nBufCount != 0)
			{
				m_szFlags.push_back(nBufCount);
			}
			// do CRC
			continue;
		}

		//get a line from the file,check "crc" for total file
		nLineNumErr++;	//the line number that the wrong hex file;
		TS_UC get_hex[128]={0};//get hex data,it is get from the line char
		//the number is (i-1)
		//int nLen = strGetData.GetLength();
		for(UINT i=0; i<strlen(a); i++) // 去掉冒号
		{
			a[i]=a[i+1];
		}

		int nLen = strlen(a)-2; // 不算回车换行的长度
		if(strlen(a)%2==0)
			turn_hex_file_line_to_unsigned_char(a);//turn every char to int 
		else
		{
			wchar_t p_c_temp[74]={'\0'};
			swprintf_s(p_c_temp,_T("Error: the hex file had error at %d line!"),nLineNumErr);
			
			//AddStringToOutPuts(p_c_temp);
			nBufCount = 0;
			AfxMessageBox(p_c_temp);
			//close_com();
			goto END;
			return 0;
		}
		turn_int_to_unsigned_char(a,nLen,get_hex);//turn to hex 
		if(get_hex[3]==1)	//for to seektobegin() function,because to end of the file
			break;


		if(!DoCRC( get_hex, nLen/2))
		{
			wchar_t p_c_temp[74]={'\0'};
			swprintf_s(p_c_temp,_T("Error: the hex file had error at %d line!"),nLineNumErr);
			//AddStringToOutPuts(p_c_temp);	
			nBufCount = 0;	
			AfxMessageBox(p_c_temp, MB_OK);
			
			//close_com();
			goto END;
			return 0;
		}

	//	if(get_hex[1]==0 && get_hex[2]==0)
	//		get_hex[4]=255;//for the 0000 register to write 255
	    int temp;
		char temp_buf[32];
		
		if (nLineNumErr==9)
		{

		    for (int i=0;i<32;i++)
		    {
			  temp_buf[i]=a[i+8];
		    }
			for (int i=0;i<20;i++)
			{   
			    temp=temp_buf[2*i]*16+temp_buf[2*i+1];
				m_DeviceInfor[i]=temp;
			}
			
			//m_softwareRev = (temp_buf[30] + temp_buf[31]*256)/10.0;


			//m_ProductModel=m_DeviceInfor[0];
			m_softwareRev= ((a[38]*16+a[39]) + (a[40]*16 + a[41])*256)/10.0;
			m_ProductName.Empty();
			for (int i=0;i<10;i++)
			{
				CString temp1;
				temp1.Format(_T("%C"),m_DeviceInfor[5+i]);
				m_ProductName = m_ProductName + temp1;
			}
		}



		unsigned int ltemp;
		ltemp=get_hex[1]*256+get_hex[2] + dwHiAddr;
//         if (m_IsRAM)
//         {
//             ltemp-=0x00008000; 
//         }
		for(int j=0;j<get_hex[0];j++)
			pBuf[ltemp+j]=get_hex[4+j];//get the data
		if((UINT)nBufCount<(ltemp+get_hex[0]))
			nBufCount=ltemp+get_hex[0];


		ZeroMemory(a, 256);
	}//end while
	
	m_szFlags.push_back(nBufCount);
	

END:
	hexfile.Close();//close the file

	return nBufCount;
	//return 0 ;
}
int get_hex_file_data(unsigned char *p,CString hex_file_path)
{
	CFile m_hex_file;//the hex file
	for(long ltemp=0;ltemp<65535;ltemp++)
		p[ltemp]=255;
	unsigned int the_max_register_number=0;//the max register number to write from hex file
	if(m_hex_file.Open(hex_file_path,CFile::modeRead | CFile::shareDenyNone))
	{
		CArchive ar(&m_hex_file,CArchive::load);
		CString m_get_data_from_file;
///////////*****************inspect the file*********************
		unsigned int ii=0;
		while(NULL!=ar.ReadString(m_get_data_from_file))//循环读取文件,直到文件结束
		{//get a line from the file,check "crc" for total file
			ii++;//the line number that the wrong hex file;
			char a[256]={'a'};
			TS_UC get_hex[128]={0};//get hex data,it is get from the line char
								//the number is (i-1)
			int ddd=m_get_data_from_file.GetLength();
			for(int i=1;i<ddd;i++)//get a line
				a[i-1]=m_get_data_from_file.GetAt(i);
			if(strlen(a)%2==0)
				turn_hex_file_line_to_unsigned_char(a);//turn every char to int 
			else
			{
//				char p_c_temp[74]={'\0'};
//				sprintf(p_c_temp,"Error: the hex file had error at %d line!",ii);
//				AfxMessageBox(p_c_temp);
				return -2;
			}
			turn_int_to_unsigned_char(a,i-1,get_hex);//turn to hex 
			if(get_hex[3]==1)//for to seektobegin() function,because to end of the file
				break;

			TS_UC uctemp2=0;
			for(int j=0;j<i/2;j++)
				uctemp2+=get_hex[j];
			if(uctemp2%256!=0)
			{
//				char p_c_temp[74]={'\0'};
//				sprintf(p_c_temp,"Error: the hex file had error at %d line!",ii);
//				AfxMessageBox(p_c_temp);				
				return -2;
			}
			if(get_hex[1]==0 && get_hex[2]==0)
				get_hex[4]=255;//for the 0000 register to write 255
			unsigned int ltemp;
			ltemp=get_hex[1]*256+get_hex[2];
			for(j=0;j<get_hex[0];j++)
				p[ltemp+j]=get_hex[4+j];//get the data
			if(the_max_register_number<(ltemp+get_hex[0]))
				the_max_register_number=ltemp+get_hex[0]-1;
		}//end while
		//flash
		ar.Close();//close the CArchive
		m_hex_file.Close();//close the file		
//*******************up is to get the register value from the file***************
		return the_max_register_number;
	}
	else
		return -3;//open hex file failure
}