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

📄 armupdatedlg.cpp

📁 s3c2410 wince bootload update tools
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	return TRUE;
}

void CARMUpdateDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	CWnd *p=GetDlgItem(IDC_CHARWINDWS);
	if(p)
		p->MoveWindow(0,0,cx,cy-20);
	if(StatusBar)
		StatusBar.MoveWindow(0,cy-20,cx,20);
	if(m_Prograss)
		m_Prograss.MoveWindow(cx-100,0,100,20);
}
extern HANDLE ghCom;
void CARMUpdateDlg::ReSetupCom(void)
{
	ComSetup.ConvertRegtoDCB();	//读取串口的相关参数
	if(ComSetup.InitCom(ComSetup.m_ComName)!=0)
		MessageBox("当前选择的串口不能打开,请重新选择串口!","错误",MB_OK);
	else
	{
		ghCom=ComSetup.m_Handle;	//设置全局变量
		m_hThread=CreateThread(NULL,0,&ComPro,ComSetup.m_Handle,CREATE_SUSPENDED,NULL);
		ResumeThread(m_hThread);
	}
}
void CARMUpdateDlg::OnMenuComsetup() 
{
	if(ComSetup.DoModal()==IDOK)
	{
		TerminateThread(m_hThread,0);
		if(m_hThread)CloseHandle(m_hThread);
		ComSetup.CloseCom();
		Sleep(50);
		ReSetupCom();
	}
}

void CARMUpdateDlg::OnMenuFtpsetup() 
{
	FtpSetup.DoModal();
}

static BOOL VerifyChecksum (DWORD cbRecord, LPBYTE pbRecord, DWORD dwChksum)
{
    // Check the CRC
    DWORD dwCRC = 0;
    DWORD i;
    for (i = 0; i < cbRecord; i++)
        dwCRC += *pbRecord ++;
    return (dwCRC == dwChksum);
}

BOOL ConvertFile(CString InFileName,CString OutFileName,DWORD* ImageStart,DWORD* ImageLength,DWORD* LauchAddr)
{
	CFile InFile;
	CFile OutFile;
	LPBYTE p;
	LPBYTE RecData;
	DWORD dwRecAddr,dwRecLen,dwRecChk;
	DWORD ReadByte=0;

	if(!InFile.Open (InFileName,CFile::modeRead))return false;
	if(!OutFile.Open (OutFileName,CFile::modeCreate|CFile::modeWrite))return false;
	p=(LPBYTE)malloc(7);
	InFile.Read(p,7);
	if(memcmp(p,"B000FF\x0A",7)!=0)
	{
		free(p);
		return false;
	}
	free(p);
	InFile.Read((void*)ImageStart,4);
	InFile.Read((void *)ImageLength,4);
	while (InFile.Read((LPBYTE) &dwRecAddr,sizeof(DWORD)) &&
		InFile.Read((LPBYTE) &dwRecLen,sizeof(DWORD))  &&
		InFile.Read((LPBYTE) &dwRecChk,sizeof(DWORD)))
	{
		// last record of .bin file uses sentinel values for address and checksum.
		if (!dwRecAddr && !dwRecChk)
		{
			*LauchAddr=dwRecLen;
			break;
		}
		if(dwRecAddr!=ReadByte+*ImageStart)
		{
			DWORD Len=dwRecAddr-ReadByte-*ImageStart;
			RecData=(LPBYTE)malloc(Len);
			memset(RecData,0,Len);
			OutFile.Write(RecData,Len);
			free(RecData);
			ReadByte+=Len;
		}
		RecData=(LPBYTE)malloc(dwRecLen);
		
		// read data block
		if (!InFile.Read (RecData,dwRecLen))
		{
			return (FALSE);
		}
		
		if (!VerifyChecksum (dwRecLen, RecData, dwRecChk))
		{
			return (FALSE);
		}
		OutFile.Write(RecData,dwRecLen);
		ReadByte+=dwRecLen;
		free(RecData);
	}
	if(*ImageLength!=ReadByte)
	{
		DWORD Len=*ImageLength-ReadByte;
		RecData=(LPBYTE)malloc(Len);
		memset(RecData,0,Len);
		OutFile.Write(RecData,Len);
		free(RecData);
	}
	InFile.Close();
	OutFile.Close();
	return true;
}
CString UserBinPath;
void CARMUpdateDlg::OnMenuBintohex() 
{
	const char szFilters[]="BIN Files (*.bin)|*.bin|All Files (*.*)|*.*||";
	const char FileName[]="$LFTemp.tmp";
	CFile InFile,OutFile;
	CString szPath,szOutPath;
	char temp[512];
	CString Buf,xx;
	DWORD ImageStart,ImageLength,LauchAddr;
	DWORD len;
	UINT ret;

	CFileDialog fdlg (TRUE, "BIN", "*.bin",OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, NULL);
	if(fdlg.DoModal()!=IDOK) return;
	GetTempPath(256,szPath.GetBuffer(256));
	szPath.Format("%s%s",szPath,FileName);
	szOutPath.Format("%s",szPath);
	szOutPath=szOutPath.Left(szOutPath.Find(".")+1);
	szOutPath=szOutPath+"nb0";
	UserBinPath=fdlg.GetPathName();
	ConvertFile(UserBinPath,szPath,&ImageStart,&ImageLength,&LauchAddr);

	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),"转换成功!");
	xx.Format("ImageStart:0x%X",ImageStart);
	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CAPS),xx);
	xx.Format("ImageLength:0x%X",ImageLength);
	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_NUM),xx);
	xx.Format("LauchAddr:0x%X",LauchAddr);
	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_SCRL),xx);


	InFile.Open(szPath,CFile::modeReadWrite);
	len=InFile.GetLength();
	OutFile.Open(szOutPath,CFile::modeReadWrite|CFile::modeCreate);
	m_Prograss.SetRange(0,int(len/100000));	//设定进度条的范围
	for(ULONG i=0;i<len;)
	{
		ret=InFile.Read(temp,32);
		Buf.Empty();
		if(isAddrShow)	//如果显示地址
		{
			xx.Format("%08X: ",i);
			Buf+=xx;
		}
		for(UINT j=0;j<ret;j++)
		{
			xx.Format("%02X ",(BYTE)(temp[j]));
			Buf += xx;
		}
		if(isStringShow)	//如果显示字串的内容
		{
			for(UINT k=0;k<ret;k++)
			{
				xx.Format("%c",temp[k]<32?'.':temp[k]);
				Buf+=xx;
			}
		}
		xx.Format("\r\n");
		Buf+=xx;
		OutFile.Write(Buf,Buf.GetLength());
		i+=ret;
		m_Prograss.SetPos(i/100000);	//更新状态条显示
	}
	InFile.Close();
	char *p=(char*)malloc(OutFile.GetLength());
	OutFile.Seek(0,SEEK_SET);
	OutFile.Read(p,OutFile.GetLength());
	OutFile.Close();
	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),"正在填充将数据...");
	SetDlgItemText(IDC_CHARWINDWS,p);
	free(p);
	if(OutFile.Open(szOutPath,CFile::modeRead))
	{
		OutFile.Close();
		OutFile.Remove(szOutPath);
	}
	if(OutFile.Open(szPath,CFile::modeRead))
	{
		OutFile.Close();
		OutFile.Remove(szPath);
	}
	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),"数据处理完成...");
	CMenu *menu=GetMenu();
	if(menu)
		menu=menu->GetSubMenu(0);
	if(!menu)
		return;
	int State=menu->GetMenuState(ID_MENU_SAVENB0,MF_BYCOMMAND);
}

void CARMUpdateDlg::OnMenuShowaddr() 
{
	CMenu *menu=GetMenu();
	if(menu)
		menu=menu->GetSubMenu(0);
	if(!menu)
		return;
	int State=menu->GetMenuState(ID_MENU_SHOWADDR,MF_BYCOMMAND)&MFS_CHECKED?MFS_UNCHECKED:MFS_CHECKED;
	menu->CheckMenuItem(ID_MENU_SHOWADDR,State);
	isAddrShow=(State==MFS_CHECKED);
}
void CARMUpdateDlg::OnMenuShowstring() 
{	CMenu *menu=GetMenu();
	if(menu)
		menu=menu->GetSubMenu(0);
	if(!menu)
		return;
	int State=menu->GetMenuState(ID_MENU_SHOWSTRING,MF_BYCOMMAND)&MFS_CHECKED?MFS_UNCHECKED:MFS_CHECKED;
	menu->CheckMenuItem(ID_MENU_SHOWSTRING,State);
	isStringShow=(State==MFS_CHECKED);
}
ULONG __stdcall bootloadthread(void * param1)
{
	CARMUpdateDlg *dlg=(CARMUpdateDlg *)param1;
	return dlg->FtpSetup.TFTPPutBootLoadFile(&dlg->m_Prograss);
}
ULONG __stdcall NKthread(void * param1)
{
	CARMUpdateDlg *dlg=(CARMUpdateDlg *)param1;
	return dlg->FtpSetup.TFTPPutNKFile(&dlg->m_Prograss);
}
ULONG __stdcall WatchThread(void * param1)
{
	HANDLE WThread=(HANDLE)param1;
	DWORD Ret=0;
	CStatusBar *p=(CStatusBar *)AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_STATUS_BAR);

	while(1)
	{
		if(GetExitCodeThread(WThread,&Ret))
		{
			if(Ret!=STILL_ACTIVE)
				break;
			else
				Sleep(100);
		}
		else
			break;
	}
	if(Ret==TFTP_OK)
		p->SetPaneText(p->CommandToIndex(ID_INDICATOR_CLOCK),"传送文件成功!");
	else
		p->SetPaneText(p->CommandToIndex(ID_INDICATOR_CLOCK),"传送文件失败!");
	return 0;
}

void CARMUpdateDlg::OnMenuBootloadupdate() 
{
	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),"开始传送BOOTLOAD文件!");
	m_BootLoadThread=CreateThread(NULL,0,&bootloadthread,this,CREATE_SUSPENDED,NULL);
	ResumeThread(m_BootLoadThread);
	CreateThread(NULL,0,&WatchThread,m_BootLoadThread,NULL,NULL);
}

void CARMUpdateDlg::OnMenuNkupdate() 
{
	DWORD Ret=0;

	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),"开始传送NK文件!");
	m_NKThread=CreateThread(NULL,0,&NKthread,this,CREATE_SUSPENDED,NULL);
	ResumeThread(m_NKThread);
	CreateThread(NULL,0,&WatchThread,m_NKThread,NULL,NULL);
}


void CARMUpdateDlg::OnMenuUpdatecancel() 
{
	if(m_BootLoadThread)
	{
		TerminateThread(m_BootLoadThread,-1);
		m_BootLoadThread=NULL;
	}
	if(m_NKThread)
	{
		TerminateThread(m_NKThread,-1);
		m_NKThread=NULL;
	}

}

void CARMUpdateDlg::OnMenuExit() 
{
	EndDialog(0);
}

void CARMUpdateDlg::OnMenuAbout() 
{
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

void CARMUpdateDlg::OnMenuSavenb0() 
{
	const char szFilters[]="NB0 Files (*.nb0)|*.nb0|All Files (*.*)|*.*||";
	DWORD ImageStart,ImageLength,LauchAddr;

	CFileDialog fdlg (FALSE, "nb0", "*.nb0",OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, NULL);
	if(fdlg.DoModal()==IDOK)
	{
		StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),"正在进行转换...");
		ConvertFile(UserBinPath,fdlg.GetPathName(),&ImageStart,&ImageLength,&LauchAddr);
		StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),"格式转换完成...");
	}
}

⌨️ 快捷键说明

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