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

📄 armupdatedlg.cpp

📁 WinCE5.0 BootLoad wince 的引导程序升级及下载工具
💻 CPP
字号:
// ARMUpdateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ARMUpdate.h"
#include "ARMUpdateDlg.h"
#include "DlgProxy.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CARMUpdateDlg dialog
IMPLEMENT_DYNAMIC(CARMUpdateDlg, CDialog);

CARMUpdateDlg::CARMUpdateDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CARMUpdateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CARMUpdateDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pAutoProxy = NULL;
	isAddrShow=true;
	isStringShow=true;
	m_NKThread=m_BootLoadThread=NULL;

}

CARMUpdateDlg::~CARMUpdateDlg()
{
	// If there is an automation proxy for this dialog, set
	//  its back pointer to this dialog to NULL, so it knows
	//  the dialog has been deleted.
	if (m_pAutoProxy != NULL)
		m_pAutoProxy->m_pDialog = NULL;
	TerminateThread(m_hThread,0);
	if(m_hThread)CloseHandle(m_hThread);
}

void CARMUpdateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CARMUpdateDlg)
	DDX_Control(pDX, IDC_PROGRESS1, m_Prograss);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CARMUpdateDlg, CDialog)
	//{{AFX_MSG_MAP(CARMUpdateDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_WM_SIZE()
	ON_COMMAND(ID_MENU_COMSETUP, OnMenuComsetup)
	ON_COMMAND(ID_MENU_FTPSETUP, OnMenuFtpsetup)
	ON_COMMAND(ID_MENU_BINTOHEX, OnMenuBintohex)
	ON_COMMAND(ID_MENU_SHOWADDR, OnMenuShowaddr)
	ON_COMMAND(ID_MENU_SHOWSTRING, OnMenuShowstring)
	ON_COMMAND(ID_MENU_BOOTLOADUPDATE, OnMenuBootloadupdate)
	ON_COMMAND(ID_MENU_NKUPDATE, OnMenuNkupdate)
	ON_COMMAND(ID_MENU_UPDATECANCEL, OnMenuUpdatecancel)
	ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
	ON_WM_DESTROY()
	ON_COMMAND(ID_MENU_ABOUT, OnMenuAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CARMUpdateDlg message handlers
static UINT indicators[] =
{
//	ID_SEPARATOR, // status line indicator
	ID_INDICATOR_CLOCK,
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

ULONG __stdcall ComPro(void * param1)
{
	HANDLE handle=(HANDLE)param1;
	DWORD lpErrors,wlen;
	COMSTAT comstat;
	char *ReciBuf;
	char *buf;
	ULONG i,j;
	CRichEditCtrl *p=(CRichEditCtrl *)AfxGetApp()->m_pMainWnd->GetDlgItem(IDC_CHARWINDWS);

	while(1)
	{
		ClearCommError(handle,&lpErrors,&comstat);
		if(comstat.cbInQue>0)
		{
			ReciBuf=(char *)malloc(comstat.cbInQue);
			buf=(char *)malloc(comstat.cbInQue+1);
			memset(buf,0,comstat.cbInQue+1);
			if(ReadFile(handle,ReciBuf,comstat.cbInQue,&wlen,NULL))
			{
				for(i=0,j=0;i<wlen;i++)
				{
					if(ReciBuf[i]>0 && ReciBuf[i] <='z')
					{
 						if(ReciBuf[i]=='\b')
 						{	CHARRANGE cr;
							p->GetSel(cr);
							if(cr.cpMax)
							{	p->SetSel(cr.cpMax-1,-1);
								p->ReplaceSel(NULL);
							}
							if(j)
								j--;
							continue;
 						}
						buf[j++]=ReciBuf[i];
						//j++;
					}
					else
						continue;
				}
				buf[j]=0;
				p->SetSel(-1,-1);
				p->ReplaceSel(buf);
			}
			free(ReciBuf);
			free(buf);
		}
		Sleep(10);
	}
	ExitThread(0);
	return 0;
}

BOOL CARMUpdateDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	
	StatusBar.Create(this);
	StatusBar.SetIndicators(indicators,sizeof(indicators)/sizeof(UINT));
	StatusBar.SetPaneInfo(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),ID_INDICATOR_CLOCK,SBPS_NORMAL,200);

	CRect rt;
	rt.SetRect(0,0,100,100);

	RichEdit.Create(ES_MULTILINE|ES_AUTOVSCROLL|WS_VSCROLL|WS_HSCROLL|ES_AUTOHSCROLL ,rt,this,IDC_CHARWINDWS);
	RichEdit.ShowWindow(SW_SHOWNORMAL);
	CFont font;
	font.CreatePointFont(9,"Fixedsys");
	RichEdit.SetFont(&font,TRUE);
	RichEdit.SetBackgroundColor(0,GetSysColor(COLOR_BTNFACE));

	GetClientRect(&rt);
	rt.top=rt.bottom-20;
	StatusBar.MoveWindow(rt);
	m_Prograss.SetParent(&StatusBar);
	m_Prograss.SetRange(0,100);

	CRichEditCtrl *p=(CRichEditCtrl *)GetDlgItem(IDC_CHARWINDWS);
	p->SetEventMask(ENM_CHANGE|ENM_UPDATE);
	
	// TODO: Add extra initialization here
	this->SetWindowPos(&wndTop,0,0,640,480,SWP_NOMOVE);
	ReSetupCom();
	//RichEdit.SetBackgroundColor(false,RGB(123,123,123));

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CARMUpdateDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CARMUpdateDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CARMUpdateDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// Automation servers should not exit when a user closes the UI
//  if a controller still holds on to one of its objects.  These
//  message handlers make sure that if the proxy is still in use,
//  then the UI is hidden but the dialog remains around if it
//  is dismissed.

void CARMUpdateDlg::OnClose() 
{
	if (CanExit())
		CDialog::OnClose();
}

void CARMUpdateDlg::OnOK() 
{
	if (CanExit())
		CDialog::OnOK();
}

void CARMUpdateDlg::OnCancel() 
{
	if (CanExit())
		CDialog::OnCancel();
}

BOOL CARMUpdateDlg::CanExit()
{
	// If the proxy object is still around, then the automation
	//  controller is still holding on to this application.  Leave
	//  the dialog around, but hide its UI.
	if (m_pAutoProxy != NULL)
	{
		ShowWindow(SW_HIDE);
		return FALSE;
	}

	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;
}

void CARMUpdateDlg::OnMenuBintohex() 
{
	char szFilters[]="BIN Files (*.bin)|*.bin|All Files (*.*)|*.*||";
	DWORD ImageStart,ImageLength,LauchAddr;
	char szPath[1024];
	const char FileName[]="$LFTemp.tmp";
	CFile Nb0File;
	char *p=NULL;
	char * str;
	ULONG len;

	// Create an Open dialog; the default file name extension is ".my".
	CFileDialog fdlg (TRUE, "BIN", "*.bin",OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
	if(fdlg.DoModal()!=IDOK) return;
	GetTempPath(sizeof(szPath),szPath);
	sprintf(szPath,"%s%s",szPath,FileName);
	ConvertFile(fdlg.GetPathName(),szPath,&ImageStart,&ImageLength,&LauchAddr);
	if(Nb0File.Open(szPath,CFile::modeRead,NULL))
	{
	    len=Nb0File.GetLength();
		p=(char *)malloc(len);
		if(p==NULL)
		{
			MessageBox("不能分配内存!","错误",MB_OK);
			return;
		}
		Nb0File.Read(p,len);
		Nb0File.Close();
	}
	else
		MessageBox("不能转换打开文件!","错误",MB_OK);
	str=(char *)malloc((len+1)*5+len/32*6);
	if(str==NULL)
	{
		MessageBox("不能分配内存空间!","错误",MB_OK);
		return;
	}
	char *pDest=str;
	m_Prograss.SetRange(0,int(len/100));	//设定进度条的范围
	for(ULONG i=0;i<len;i++)
	{
		if(isAddrShow && (i%32)==0)	//如果显示地址
		{
			sprintf(pDest,"%08X: ",i);
			pDest+=10;
		}
		sprintf(pDest,"%02X ",(BYTE)(*(p+i)));
		pDest+=3;
		if((i%32)==31)
		{
			if(isStringShow)	//如果显示字串的内容
			{	for(int k=0;k<32;k++)
				{	
					sprintf(pDest+k,"%c",(BYTE)p[i-32+k]<32?'.':(BYTE)p[i-32+k]);
				}
				pDest+=32;
				*pDest='\r';
				pDest++;
			}
			else
				*(pDest-1)='\r';
			*pDest='\n';
			pDest++;

		}
		m_Prograss.SetPos(i/100);	//更新状态条显示
	}
	*pDest=0;	//做完以后结束字串
	SetDlgItemText(IDC_CHARWINDWS,str);
	free(p);
	free(str);
}

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;
	dlg->FtpSetup.TFTPPutBootLoadFile(&dlg->m_Prograss);
	return 0;
}
ULONG __stdcall NKthread(void * param1)
{
	CARMUpdateDlg *dlg=(CARMUpdateDlg *)param1;
	dlg->FtpSetup.TFTPPutNKFile(&dlg->m_Prograss);
	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);
	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),"传送BOOTLOAD文件完毕!");
}

void CARMUpdateDlg::OnMenuNkupdate() 
{
	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),"开始传送NK文件!");
	m_NKThread=CreateThread(NULL,0,&NKthread,this,CREATE_SUSPENDED,NULL);
	ResumeThread(m_NKThread);
	StatusBar.SetPaneText(StatusBar.CommandToIndex(ID_INDICATOR_CLOCK),"传送NK文件完毕!");
}


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

}

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

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

⌨️ 快捷键说明

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