www.pudn.com > LoadMachineCode111.rar > DownDialog.cpp


// DownDialog.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "LoadMachineCode.h" 
#include "DownDialog.h" 
#include "ComOperate.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDownDialog dialog 
 
CComOperate comOpt; 
CDownDialog downdlg; 
 
CEvent g_eventKill; 
CEvent g_eventStart; 
int nFilelength=0; 
int g_count=0; 
int com; 
CFile cf; 
UINT downloadprogram(LPVOID pParam) 
{ 
	int i; 
	BOOL result=TRUE; 
 
	CDownDialog downDlg; 
 
	::WaitForSingleObject(g_eventStart,INFINITE); 
 
	if(!cf.Open("LOAD.BIN",CFile::modeRead)) 
	{ 
		AfxMessageBox("打开文件不成功"); 
		return FALSE; 
	} 
	nFilelength=cf.GetLength (); 
	g_count=nFilelength; 
	result=comOpt.Open_remoteportm(com); 
	if(!result) 
	{ 
		AfxMessageBox("打开串口失败"); 
		cf.Close(); 
		return FALSE; 
	} 
 
	char cfBuffer[nLoadlength+1]; 
	while(nFilelength>=0) 
	{ 
		char Resource[2049]; 
		 
		if(::WaitForSingleObject(g_eventKill,0)==WAIT_OBJECT_0){ 
		     comOpt.Port_Res(); 
			 cf.Close(); 
			::PostMessage((HWND)pParam,WM_THREADFINISHED,0,0); 
			return -1; 
		} 
 
		 
		memset(cfBuffer,'\0',nLoadlength+1); 
		int nReadlength=cf.Read(cfBuffer,nLoadlength); 
		 
		if(nReadlength==0) 
		{			 
			if(nFilelength>0) 
			{ 
			AfxMessageBox("文件中没有数据,安装不成功"); 
			comOpt.Port_Res(); 
			cf.Close();	 
			return -1; 
			} 
			else 
			{ 
			memset(Resource,'\0',2049); 
			result=comOpt.PORT_RW(END,Resource); 
			comOpt.Port_Res(); 
			cf.Close(); 
            AfxMessageBox("程序安装成功"); 
			return 0; 
			} 
			 
		} 
		if(nReadlength==1024) 
		{ 
			memset(Resource,'\0',2049); 
 
			for(i=0;i<1024;i++) 
			{ 
				Resource[2*i]=(cfBuffer[i]>>4)&0x0f; 
					if(Resource[2*i]>=10) 
                      Resource[2*i]=Resource[2*i]-10+0x41; 
                    else 
                      Resource[2*i]=Resource[2*i]+0x30; 
 
				Resource[2*i+1]=(cfBuffer[i])&0x0f; 
                    if(Resource[2*i+1]>=10) 
                      Resource[2*i+1]=Resource[2*i+1]-10+0x41; 
                    else 
                      Resource[2*i+1]=Resource[2*i+1]+0x30; 
            } 
 
			result=comOpt.PORT_RW(SEND,Resource); 
 
			if(result==FALSE) 
			{ 
			AfxMessageBox("串口通讯有错误!"); 
			comOpt.Port_Res(); 
			cf.Close();	 
			return -1; 
			} 
		} 
		if(nReadlength<1024) 
		{ 
			char Buffer[2049]; 
			memset(Buffer,'\0',2049); 
			memset(Resource,'\0',2049); 
 
            for(i=0;i>4)&0x0f; 
				Resource[2*i+1]=(cfBuffer[i])&0x0f; 
 
				if(Resource[2*i]>=10) 
					Resource[2*i]=Resource[2*i]-10+0x41; 
				else 
					Resource[2*i]=Resource[2*i]+0x30; 
				if(Resource[2*i+1]>=10) 
					Resource[2*i+1]=Resource[2*i+1]-10+0x41; 
				else 
					Resource[2*i+1]=Resource[2*i+1]+0x30; 
			} 
			for(i=0;i<2048-2*nReadlength;i++) 
				Buffer[i]=0x46; 
			strcat(Resource,Buffer); 
			result=comOpt.PORT_RW(SEND,Resource); 
		} 
		if(result==FALSE) 
		{ 
			comOpt.Port_Res(); 
			cf.Close(); 
		} 
		else 
			nFilelength=nFilelength-1024;		 
	} 
    return 0; 
} 
 
CDownDialog::CDownDialog(CWnd* pParent /*=NULL*/) 
	: CDialog(CDownDialog::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDownDialog) 
	m_cbocom = _T("2"); 
	//}}AFX_DATA_INIT 
} 
 
 
void CDownDialog::DoDataExchange(CDataExchange* pDX) 
{ 
 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDownDialog) 
	DDX_Control(pDX, IDC_PROGRESS, m_progress); 
	DDX_Control(pDX, IDC_COMBO_COM, m_ccbocom); 
	DDX_CBString(pDX, IDC_COMBO_COM, m_cbocom); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CDownDialog, CDialog) 
	ON_MESSAGE(WM_THREADFINISHED, OnThreadFinished) 
	//{{AFX_MSG_MAP(CDownDialog) 
	ON_BN_CLICKED(ID_DOWNLOAD, OnDownload) 
	ON_WM_TIMER() 
	ON_CBN_SELCHANGE(IDC_COMBO_COM, OnSelchangeComboCom) 
	ON_BN_CLICKED(IDC_ZHONGDUAN, OnZhongduan) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDownDialog message handlers 
 
void CDownDialog::OnDownload()  
{ 
	 
	AfxBeginThread(downloadprogram,GetSafeHwnd()); 
	m_progress.SetPos(0); 
	m_nTimer=SetTimer(1,100,NULL); 
	ASSERT(m_nTimer!=0); 
	g_eventStart.SetEvent(); 
} 
 
void CDownDialog::OnCancel()  
{ 
	// TODO: Add extra cleanup here 
	CDialog::OnCancel(); 
 
} 
LRESULT CDownDialog::OnThreadFinished(WPARAM wParam, LPARAM lParam) 
{ 
	CDialog::OnOK(); 
    return 0; 
} 
 
void CDownDialog::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CProgressCtrl* pBar = (CProgressCtrl*) GetDlgItem(IDC_PROGRESS); 
	pBar->SetPos((g_count-nFilelength)*100/g_count); 
	//CDialog::OnTimer(nIDEvent); 
} 
 
BOOL CDownDialog::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	// TODO: Add extra initialization here 
	UpdateData(TRUE); 
	com=atoi(m_cbocom); 
	m_progress.SetRange(0,100); 
	m_progress.SetPos(0); 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // 
 
 
void CDownDialog::OnSelchangeComboCom()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	com=atoi(m_cbocom); 
} 
 
//BOOL CDownDialog::downloadprogram(int com,LPVOID pParam) 
 
void CDownDialog::OnZhongduan()  
{ 
	// TODO: Add your control notification handler code here 
	if(g_count>0) 
		g_eventKill.SetEvent(); 
	else 
		AfxMessageBox("没有必要执行中断操作"); 
}