📄 lpc2xxisp.cpp.bak
字号:
// 擦除/烧写FLASH
pDlg->m_ComPort.ClearBuffer();
//2119与2368不一样
// 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();
Sleep(10);
// E 0 1
tPos=0;
rCnt=0;
//sprintf(outbuf,"E 0 %d\r\n",(xLen-1)/8192);
memset(stt,0,sizeof(stt));
sprintf(outbuf,"E 0 %d\r\n",SECTORS);
//sprintf(outbuf,"E 0 %d\r\n",0);
ixx=strlen(outbuf);
pDlg->m_ComPort.SendData((BYTE*)outbuf,ixx);
Sleep(500);
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();
//////////////////////////////////
// U 23130
rCnt=0;
tPos=0;
memset(stt,0,sizeof(stt));
strcpy(str1,"U 23130\r\n");
//pComm->SendData((BYTE*)str1,strlen(str1));
do
{
pDlg->m_ComPort.SendData((BYTE*)str1,strlen(str1));
Sleep(30);
pDlg->m_ComPort.ReadData((BYTE*)&stt[tPos],strlen("U 23130\r\n0\r\n"),&iPos);
tPos+=iPos;
if(tPos>=strlen("U 23130\r0\r")) break;
//Sleep(5);
rCnt++;
}while(rCnt<100);
if(rCnt==100)
{
AfxMessageBox("操作失败!");
return 1;
}
//stt[tPos]=0;
//if(strcmp(stt,"U 23130\r0\r\n")!=0)
if((strstr(stt,"\r0")==NULL)&&(strstr(stt,"\n0")==NULL))
{
AfxMessageBox("操作失败!");
return 1;
}
pDlg->m_ComPort.ClearBuffer();
// 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();
iPos=0;
//开始发送数据(UU编码)到RAM.
//每次W/C4096个数据
//xLen为bin文件的总长度
while(iPos<xLen)
{
if(xLen-iPos>MAX_LINELEN)
linelen=MAX_LINELEN;
else
linelen=xLen-iPos;
if((iPos%4096)==4095)
{
outbuf[oPos++]=ENCODE_BYTE(1);
// file2.Write(outbuf,1);
outbuf [oPos++] = ENCODE_BYTE ((sBuf[iPos] & 0xFC) >> 2);
outbuf [oPos++] = ENCODE_BYTE ((sBuf[iPos] & 0x03) << 4);
outbuf [oPos++] = ENCODE_BYTE (0);
outbuf [oPos++] = ENCODE_BYTE (0);
// outbuf [oPos++] = '\0';
xCrc+=sBuf[iPos];
iPos++;
linecnt-=1;
outbuf[oPos++]='\r';
outbuf[oPos++]='\n';
outbuf[oPos++]=0;
// +++++++++++++++++++++++++++++++++++
// DATA
rCnt=0;
tPos=0;
ixx=strlen(outbuf);
pDlg->m_ComPort.SendData((BYTE*)outbuf,ixx);
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);
if(rCnt==100)
{
AfxMessageBox("操作失败!");
return 1;
}
stt[tPos]=0;
if(strcmp(stt,outbuf)!=0)
{
AfxMessageBox("操作失败!");
return 1;
}
// +++++++++++++++++++++++++++++++++++
// 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(20);
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();
// 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"
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();
// 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("烧写完毕!");
iDownStep ++;
break;
default:
break;
}
if(iDownStep == -1) break;
}while(1);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -