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

📄 lpc2xxisp.cpp.bak

📁 一款不错的关于串口升级硬件的软件
💻 BAK
📖 第 1 页 / 共 3 页
字号:
				Sleep(10);
				rCnt++;
			}while(rCnt<100);
			if(rCnt==100)
			{
				AfxMessageBox("操作失败!");
				return 1;
			}
			stt[tPos]=0;
			if(strcmp(stt,outbuf)!=0)
			{
				AfxMessageBox("操作失败!");
				return 1;
			}
			*/
			
			//	P 0 1
			tPos=0;
			rCnt=0;
			memset(stt,0,sizeof(stt));
			sprintf(outbuf,"P 0 %d\r\n",SECTORS);
			ixx=strlen(outbuf);
			pDlg->m_ComPort.SendData((BYTE*)outbuf,ixx);
			Sleep(20);
			strcat(outbuf,"0\r\n");
			ixx+=3;
			do
			{
				//pComm->ReadData((BYTE*)&stt[tPos],100,&rLen);
				pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],ixx,&rLen);
				tPos+=rLen;
				if(tPos>=(ixx-2))	break;
				Sleep(10);
				rCnt++;
			}while(rCnt<100);
			if(rCnt==100)
			{
				AfxMessageBox("操作失败!");
				return 1;
			}
			//stt[tPos]=0;
			//if(strcmp(stt,outbuf)!=0)
			if((strstr(stt,"\r0")==NULL)&&(strstr(stt,"\n0")==NULL))
			{
				AfxMessageBox("操作失败!");
				return 1;
			}
        		
			pDlg->m_ComPort.ClearBuffer();
			
			
			//	C Y 1073742336 4096
			sprintf(outbuf,"C %d 1073742336 4096\r\n",iSect*4096);
			ixx=strlen(outbuf);
			pDlg->m_ComPort.SendData((BYTE*)outbuf,ixx);
			strcat(outbuf,"0\r\n");
			ixx+=3;
			Sleep(20);
			tPos=0;
			do
			{
				//pComm->ReadData((BYTE*)&stt[tPos],100,&rLen);
				pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],strlen(outbuf),&rLen);
				tPos+=rLen;
				if(tPos>=(ixx-2))	break;
				Sleep(10);
				rCnt++;
			}while(rCnt<100);
			if(rCnt==100)
			{
				AfxMessageBox("操作失败!");
				return 1;
			}
			//stt[tPos]=0;
			if((strstr(stt,"\r0")==NULL)&&(strstr(stt,"\n0")==NULL))
			{
				AfxMessageBox("操作失败!");
				return 1;
			}
				
			pDlg->m_ComPort.ClearBuffer();

			iSect++;

			if(xLen-iPos>4096)
			{
				/*
				//	W 40000200H 4096
				//	"W 1073742336 4096\r\n"
				strcpy(outbuf,"W 1073742336 4096\r\n");
				ixx=strlen(outbuf);
				pComm->SendData((BYTE*)outbuf,ixx);
				strcat(outbuf,"0\r\n");
				ixx+=3;
				Sleep(20);
				tPos=0;
				do
				{
					//pComm->ReadData((BYTE*)&stt[tPos],100,&rLen);
					pComm->ReadData((BYTE*)&stt[tPos],strlen(outbuf),&rLen);
					tPos+=rLen;
					if(tPos>=ixx)	break;
					Sleep(10);
					rCnt++;
				}while(rCnt<100);
				if(rCnt==100)
				{
					AfxMessageBox("操作失败!");
					return 1;
				}
				stt[tPos]=0;
				if(strcmp(stt,outbuf)!=0)
				{
					AfxMessageBox("操作失败!");
					return 1;
				}
				*/
				//	W 40000200H 4096
				//	"W 1073742336 4096\r\n"
				tPos=0;
				rCnt=0;
				memset(stt,0,sizeof(stt));
				strcpy(outbuf,"W 1073742336 4096\r\n");
				ixx=strlen(outbuf);
				pDlg->m_ComPort.SendData((BYTE*)outbuf,ixx);
				Sleep(30);
				//strcat(outbuf,"0\r\n");
				strcpy(outbuf,"W 1073742336 4096\r\n0\r\n");
				ixx=strlen(outbuf);
				do
				{
					//pComm->ReadData((BYTE*)&stt[tPos],100,&rLen);
					pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],ixx,&rLen);
					tPos+=rLen;
					if(tPos>=(ixx-2))	break;
					Sleep(10);
					rCnt++;
				}while(rCnt<100);
				if(rCnt==100)
				{
					AfxMessageBox("操作失败!");
					return 1;
				}
				//stt[tPos]=0;
				//if(strcmp(stt,outbuf)!=0)
				if((strstr(stt,"\r0")==NULL)&&(strstr(stt,"\n0")==NULL))
				{
					AfxMessageBox("操作失败!");
					return 1;
				}
				
				pDlg->m_ComPort.ClearBuffer();
			}
			else
			{
				//	W 40000200H 4096
				//	"W 1073742336 4096\r\n"
				sprintf(outbuf,"W 1073742336 %d\r\n",xLen-iPos);
				ixx=strlen(outbuf);
				pDlg->m_ComPort.SendData((BYTE*)outbuf,ixx);
				strcat(outbuf,"0\r\n");
				ixx+=3;
				Sleep(10);
				tPos=0;
				do
				{
					//pComm->ReadData((BYTE*)&stt[tPos],100,&rLen);
					pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],strlen(outbuf),&rLen);
					tPos+=rLen;
					if(tPos>=(ixx-2))	break;
					Sleep(10);
					rCnt++;
				}while(rCnt<100);
				if(rCnt==100)
				{
					AfxMessageBox("操作失败!");
					return 1;
				}
				//stt[tPos]=0;
				if((strstr(stt,"\r0")==NULL)&&(strstr(stt,"\n0")==NULL))
				{
					AfxMessageBox("操作失败!");
					return 1;
				}
				
				pDlg->m_ComPort.ClearBuffer();
			}
		}
		else
		{
			//???????????????????????????????????
			if(iPos>12370)
			{
				iPos++;
				iPos--;
			}

			outbuf[oPos++]=ENCODE_BYTE(linelen);
//			file2.Write(outbuf,1);
			for (linecnt=linelen;linecnt>0;)
			{
				outbuf [oPos++] = ENCODE_BYTE ((sBuf[iPos] & 0xFC) >> 2);
				outbuf [oPos++] = ENCODE_BYTE (((sBuf[iPos] & 0x03) << 4) +
										((sBuf[iPos+1] & 0xF0) >> 4));
				outbuf [oPos++] = ENCODE_BYTE (((sBuf[iPos+1] & 0x0F) << 2) +
										((sBuf[iPos+2] & 0xC0) >> 6));
				outbuf [oPos++] = ENCODE_BYTE (sBuf[iPos+2] & 0x3F);
				xCrc+=sBuf[iPos]+sBuf[iPos+1]+sBuf[iPos+2];
//				outbuf [4] = '\0';
				iPos+=3;
				linecnt-=3;
//				file2.Write(outbuf,4);
			}
			//	+++++++++++++++++++++++++++++++++++
			//	DATA
			outbuf[oPos++]='\r';
			outbuf[oPos++]='\n';
			outbuf[oPos++]=0;
			rCnt=0;
			tPos=0;
			ixx=strlen(outbuf);
			pDlg->m_ComPort.SendData((BYTE*)outbuf,ixx);
			TRACE("WRITE:%s\n",outbuf);
			Sleep(20);
			do
			{
				//pComm->ReadData((BYTE*)&stt[tPos],100,&rLen);
				pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],strlen(outbuf),&rLen);
				tPos+=rLen;
				if(tPos>=ixx)	break;
				Sleep(10);
				rCnt++;
			}while(rCnt<100);
			TRACE("rCnt:%d iPos:%d\n",rCnt,iPos);
			if(rCnt==100)
			{
				AfxMessageBox("操作失败!");
				return 1;
			}
			stt[tPos]=0;
			if(strcmp(stt,outbuf)!=0)
			{
				AfxMessageBox("操作失败!");
				return 1;
			}

			oPos=0;

			//发送校验
			if(((iPos%4096)%900)==0)
			{
				//	+++++++++++++++++++++++++++++++++++
				//	CRC + OK
				sprintf(outbuf,"%d\r\n",xCrc);
				pDlg->m_ComPort.SendData((BYTE*)outbuf,strlen(outbuf));
				TRACE("CRC:%s\n",outbuf);
				sprintf(outbuf,"%d\r\nOK\r\n",xCrc);
				ixx=strlen(outbuf);
				xCrc=0;
				oPos=0;
				//	..
				rCnt=0;
				tPos=0;
				Sleep(10);
				do
				{
					//pComm->ReadData((BYTE*)&stt[tPos],100,&rLen);
					pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],strlen(outbuf),&rLen);
					tPos+=rLen;
					if(tPos>=(ixx-2))	break;
					Sleep(10);
					rCnt++;
				}while(rCnt<100);
				if(rCnt==100)
				{
					AfxMessageBox("操作失败!");
					return 1;
				}
				//stt[tPos]=0;
				//if(strcmp(stt,outbuf)!=0)
				if((strstr(stt,"\rOK")==NULL)&&(strstr(stt,"\nOK")==NULL))
				{
					AfxMessageBox("操作失败!");
					return 1;
				}
				
				pDlg->m_ComPort.ClearBuffer();
				//	接收"OK\r\n"
			}
		}
//		outbuf [oPos++]='\r';
//		outbuf [oPos++]='\n';
//		outbuf [oPos++]=0;
//		file2.Write(outbuf,strlen(outbuf));
//		oPos=0;
	}
	//	+++++++++++++++++++++++++++++++++++
	//	CRC + OK
	sprintf(outbuf,"%d\r\n",xCrc);
	TRACE("CRC:%s\n",outbuf);
	pDlg->m_ComPort.SendData((BYTE*)outbuf,strlen(outbuf));
	sprintf(outbuf,"%d\r\nOK\r\n",xCrc);
	ixx=strlen(outbuf);
	xCrc=0;
	oPos=0;
	//	..
	rCnt=0;
	tPos=0;
	Sleep(10);
	do
	{
		pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],100,&rLen);
		tPos+=rLen;
		if(tPos>=(ixx-2))	break;
		Sleep(10);
		rCnt++;
	}while(rCnt<100);
	if(rCnt==100)
	{
		AfxMessageBox("操作失败!");
		return 1;
	}
	//stt[tPos]=0;
	if((strstr(stt,"\rOK")==NULL)&&(strstr(stt,"\nOK")==NULL))
	{
		AfxMessageBox("操作失败!");
		return 1;
	}
	
	pDlg->m_ComPort.ClearBuffer();

	/*
	//	PROGRAM
	//	P X X
	//sprintf(outbuf,"P %d %d\r\n",iSect/2,iSect/2);
	sprintf(outbuf,"P %d %d\r\n",0,27);
	ixx=strlen(outbuf);
	pComm->SendData((BYTE*)outbuf,ixx);
	strcat(outbuf,"0\r\n");
	ixx+=3;
	Sleep(10);
	tPos=0;
	do
	{
		pComm->ReadData((BYTE*)&stt[tPos],100,&rLen);
		tPos+=rLen;
		if(tPos>=ixx)	break;
		Sleep(10);
		rCnt++;
	}while(rCnt<100);
	if(rCnt==100)
	{
		AfxMessageBox("操作失败!");
		return 1;
	}
	stt[tPos]=0;
	if(strcmp(stt,outbuf)!=0)
	{
		AfxMessageBox("操作失败!");
		return 1;
	}
	*/
	//	P 0 1
	tPos=0;
	rCnt=0;
	memset(stt,0,sizeof(stt));
	sprintf(outbuf,"P 0 %d\r\n",SECTORS);
	ixx=strlen(outbuf);
	pDlg->m_ComPort.SendData((BYTE*)outbuf,ixx);
	Sleep(20);
	strcat(outbuf,"0\r\n");
	ixx+=3;
	do
	{
		//pComm->ReadData((BYTE*)&stt[tPos],100,&rLen);
		pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],ixx,&rLen);
		tPos+=rLen;
		if(tPos>=(ixx-2))	break;
		Sleep(10);
		rCnt++;
	}while(rCnt<100);
	if(rCnt==100)
	{
		AfxMessageBox("操作失败!");
		return 1;
	}
	//stt[tPos]=0;
	//if(strcmp(stt,outbuf)!=0)
	if((strstr(stt,"\r0")==NULL)&&(strstr(stt,"\n0")==NULL))
	{
		AfxMessageBox("操作失败!");
		return 1;
	}

	pDlg->m_ComPort.ClearBuffer();
	
	
	//	C Y 1073742336 Z
	sprintf(outbuf,"C %d 1073742336 4096\r\n",iSect*4096);
	ixx=strlen(outbuf);
	pDlg->m_ComPort.SendData((BYTE*)outbuf,ixx);
	strcat(outbuf,"0\r\n");
	ixx+=3;
	Sleep(50);
	tPos=0;
	do
	{
		pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],100,&rLen);
		tPos+=rLen;
		if(tPos>=(ixx-2))	break;
		Sleep(10);
		rCnt++;
	}while(rCnt<100);
	if(rCnt==100)
	{
		AfxMessageBox("操作失败!");
		return 1;
	}
	//stt[tPos]=0;
	if((strstr(stt,"\r0")==NULL)&&(strstr(stt,"\n0")==NULL))
	{
		AfxMessageBox("操作失败!");
		return 1;
	}
		
	pDlg->m_ComPort.ClearBuffer();
	
	//使进入正常工作状态
	pDlg->m_ComPort.SetBaudRate(19200);
	pDlg->m_ComPort.ClearBuffer();
	Sleep(20);
	pDlg->m_ComPort.SendData((BYTE*)("\x55\xAA\x00\x00\x00\x00\xC0\x05\x68\x7f\x4f\x5D"),12);
	Sleep(100);
	
	AfxMessageBox("烧写完毕!");
	return 0;
}


UINT ThreadNxpIspFlash(LPVOID pParam)
{
	CFlashISPDlg *pDlg = (CFlashISPDlg *)pParam;
	CProgressCtrl *pDownProgress = (CProgressCtrl*)pDlg->GetDlgItem(IDC_UPDATEPROGRESS);
	int iDownStep = 0;
	char str1[50]="Synchronized\r\n";
	char stt[100];
	int		linelen;
	int		linecnt;
	char	outbuf[100];
	int		iSect=0;
	DWORD	xLen,rLen;
	CFile	file;
	DWORD	tPos=0;
	DWORD	rCnt=0;
	DWORD	iPos=0;
	DWORD	xCrc=0;
	int		oPos=0;
	DWORD	ixx;
	int		iProgress;
	BYTE	*sBuf;
	do
	{
		switch(iDownStep)
		{
		case 0:				// start
			pDownProgress->SetPos(0);
	    	if(pDlg->m_ComPort.IsRunning())
			{
				AfxMessageBox("串口错误!");
				return 1;
			}
			else
			{
				pDlg->m_ComPort.SetRunning(true);
				iDownStep ++;
			}
			break;
		case 1:			// 1st step, open file
			if(file.Open("D:\\SANY.bin",CFile::modeRead,NULL)==NULL)
			{
				AfxMessageBox("BIN文件打开失败!");
				CWnd* pnd=pDlg->GetDlgItem(IDOK);
				pnd->SetWindowText("开始");
				pDlg->m_ComPort.Close();
				return 1;
			}
			xLen=file.GetLength();
			sBuf=(BYTE*)GlobalAlloc(LMEM_FIXED,xLen+3);
			sBuf[xLen]=0;
			sBuf[xLen+1]=0;
			sBuf[xLen+2]=0;
			if(sBuf==NULL)
			{
				file.Close();
				AfxMessageBox("分配内存失败!");
				return 1;
			}
			if(file.Read(sBuf,xLen)!=xLen)
			{
				file.Close();
				AfxMessageBox("读文件失败!");
				return 1;
			}
			file.Close();
			iDownStep ++;
			break;
		case 2:			//激活ISP
			iProgress = 0;
			pDlg->m_CStrPrompt.Empty();
			pDlg->m_CStrPrompt = "正在复位接头...";
			pDlg->SendMessage(WM_MYUPDATEDATA,false);
			pDlg->m_ComPort.SetBaudRate(19200);
			pDlg->m_ComPort.ClearBuffer();
			Sleep(20);
			pDlg->m_ComPort.SendData((BYTE*)("\x55\xAA\x00\x00\x00\x00\xC0\x05\x68\x7f\x49\x5B"),12);
			for(iProgress=0;iProgress<100;iProgress++)
			{
				pDownProgress->SetPos(iProgress);
				Sleep(2);
			}
			iDownStep ++;
			break;
		case 3:			// 尝试连接接头
			pDlg->m_ComPort.SetBaudRate(pDlg->m_iBaudRate);
			pDlg->m_ComPort.ClearBuffer();
			Sleep(20);
			//自动波特率
			pDlg->m_CStrPrompt.Empty();
			pDlg->m_CStrPrompt = "正在连接接头...";
			pDlg->SendMessage(WM_MYUPDATEDATA,false);
			pDownProgress->SetPos(iProgress);
			do
			{
				stt[0]=0x3F;
				pDlg->m_ComPort.SendData((BYTE*)stt,1);
				Sleep(20);
				//pComm->ReadData((BYTE*)stt,100,&tPos);
				pDlg->m_ComPort.ReadData((BYTE*)stt,strlen(str1),&tPos);
				rCnt++;
			}while(((tPos==0)||(tPos==1&&stt[0]==0x3F))&&rCnt<100);
			if(rCnt==100)
			{
				AfxMessageBox("操作失败!");
				return 1;
			}
			stt[tPos]=0;
			if(strcmp(stt,str1)!=0)
				return 1;
			iDownStep ++;
			break;
		case 4:
			tPos = 0;
			memset(stt,0,sizeof(stt));
			strcpy(str1,"Synchronized\r\n");
			pDlg->m_ComPort.SendData((BYTE*)str1,strlen(str1));
			Sleep(25);
			//pComm->ReadData((BYTE*)&stt[tPos],30,&iPos);
			tPos = 0;
			memset(stt,0,sizeof(stt));
			strcpy(str1,"8000\r\n");
			pDlg->m_ComPort.SendData((BYTE*)str1,strlen(str1));
			Sleep(17);
			//pComm->ReadData((BYTE*)&stt[tPos],30,&iPos);
			tPos = 0;
			memset(stt,0,sizeof(stt));
			strcpy(str1,"J\r\n");
			pDlg->m_ComPort.SendData((BYTE*)str1,strlen(str1));
			Sleep(18);
			//pComm->ReadData((BYTE*)&stt[tPos],30,&iPos);
			tPos = 0;
			memset(stt,0,sizeof(stt));
			strcpy(str1,"R 1073742336 4\r\n");
			pDlg->m_ComPort.SendData((BYTE*)str1,strlen(str1));
			Sleep(40);
			//pComm->ReadData((BYTE*)&stt[tPos],50,&iPos);
			tPos = 0;
			memset(stt,0,sizeof(stt));
			strcpy(str1,"OK\r\n");
			pDlg->m_ComPort.SendData((BYTE*)str1,strlen(str1));
			Sleep(65);
			//pComm->ReadData((BYTE*)&stt[tPos],10,&iPos);
			tPos = 0;
			memset(stt,0,sizeof(stt));
			strcpy(str1,"U 23130\r\n");
			pDlg->m_ComPort.SendData((BYTE*)str1,strlen(str1));
			Sleep(20);
			pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],20,&iPos);
		///////////////////////////////////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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