flashtoolsdlg.cpp
来自「一个Flash下载工具」· C++ 代码 · 共 602 行
CPP
602 行
// FlashToolsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FlashTools.h"
#include "FlashToolsDlg.h"
#include "devioctl.h"
#include "UsbDrvIoctl.h"
#include <time.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UCHAR InBuffer[65536];
#define PACKETSIZE 54
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
UINT WriteFlash(void * pParam);
HANDLE open_dev();
HANDLE open_file( char *filename);
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()
/////////////////////////////////////////////////////////////////////////////
// CFlashToolsDlg dialog
CFlashToolsDlg::CFlashToolsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFlashToolsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFlashToolsDlg)
// 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_WriteFlag = FALSE;
}
void CFlashToolsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFlashToolsDlg)
DDX_Control(pDX, IDC_LABLETIME, m_LableTime);
DDX_Control(pDX, IDC_LABLE_STATUS, m_LabelStatus);
DDX_Control(pDX, IDC_PROGRESS, m_Progress);
DDX_Control(pDX, IDC_BTN_WRITE, m_BtnWrite);
DDX_Control(pDX, IDC_EDT_FILEPATH, m_EdtFilePath);
DDX_Control(pDX, IDC_EDT_ADDRESS, m_EdtAddress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFlashToolsDlg, CDialog)
//{{AFX_MSG_MAP(CFlashToolsDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_EDT_ADDRESS, OnChangeEdtAddress)
ON_BN_CLICKED(IDC_BTN_BROW, OnBtnBrow)
ON_BN_CLICKED(IDC_BTN_WRITE, OnBtnWrite)
ON_EN_UPDATE(IDC_EDT_ADDRESS, OnUpdateEdtAddress)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFlashToolsDlg message handlers
BOOL CFlashToolsDlg::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
// TODO: Add extra initialization here
m_EdtAddress.SetWindowText( "0xc008000" );
strcpy( (char*)m_szOldContext , "0x" );
m_ThreadEvent = CreateEvent( NULL , TRUE , FALSE , NULL );
if( NULL == m_ThreadEvent )
return FALSE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CFlashToolsDlg::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 CFlashToolsDlg::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 CFlashToolsDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFlashToolsDlg::OnOK()
{
// TODO: Add extra validation here
if( m_WriteFlag )
{
m_WriteFlag = FALSE;
WaitForSingleObject( m_ThreadEvent , INFINITE );
}
CDialog::OnOK();
}
void CFlashToolsDlg::OnCancel()
{
// TODO: Add extra cleanup here
if( m_WriteFlag )
{
m_WriteFlag = FALSE;
WaitForSingleObject( m_ThreadEvent , INFINITE );
}
CDialog::OnCancel();
}
void CFlashToolsDlg::OnChangeEdtAddress()
{
UCHAR szContext[64];
INT iCount = 0;
// POINT point;
memset( szContext , 0 , 64 );
m_EdtAddress.GetWindowText( (char*)szContext , 64 );
iCount = strlen( (char*)szContext );
for( int i = 2 ; i < iCount ; i ++ )
{
if(!((szContext[i] >= '0' && szContext[i] <= '9') ||
(szContext[i] >= 'a' && szContext[i] <= 'f') ||
(szContext[i] >= 'A' && szContext[i] <= 'F')
))
{
m_EdtAddress.SetWindowText( (char*)m_szOldContext );
return;
}
}
if( szContext[0] != '0' ||
szContext[1] != 'x' )
{
m_EdtAddress.SetWindowText( (char*)m_szOldContext );
}
else
memcpy( m_szOldContext , szContext , 64 );
}
void CFlashToolsDlg::OnBtnBrow()
{
// szFilters is a text string that includes two file name filters:
// "*.my" for "MyType Files" and "*.*' for "All Files."
char szFilters[]=
"bin Files (*.bin)|*.bin|HEX Files (*.hex)|*.hex|All Files (*.*)|*.*||";
// Create an Open dialog; the default file name extension is ".my".
CFileDialog fileDlg (TRUE, "bin", "*.bin",
OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
// Display the file dialog. When user clicks OK, fileDlg.DoModal()
// returns IDOK.
if( fileDlg.DoModal ()==IDOK )
{
CString pathName = fileDlg.GetPathName();
//CString fileName = fileDlg.GetFileTitle ();
m_EdtFilePath.SetWindowText(pathName);
}
}
void CFlashToolsDlg::OnBtnWrite()
{
if( !m_WriteFlag )
{
CHAR szPath[_MAX_PATH];
memset( szPath , 0 , _MAX_PATH );
m_EdtFilePath.GetWindowText( szPath , _MAX_PATH );
if( strlen(szPath) <= 0 )
{
AfxMessageBox( "请输入文件的路径!" );
return;
}
ResetEvent( m_ThreadEvent );
CWinThread * wt = AfxBeginThread(
WriteFlash, // thread function
this ); // argument to thread function
m_BtnWrite.SetWindowText( "取消" );
m_WriteFlag = TRUE;
}
else
{
m_WriteFlag = FALSE;
WaitForSingleObject( m_ThreadEvent , INFINITE );
m_BtnWrite.SetWindowText( "写入" );
}
}
UINT WriteFlash(void * pParam)
{
CFlashToolsDlg *dlg = (CFlashToolsDlg*)pParam;
INT i = 0;
CHAR szPath[_MAX_PATH];
HANDLE File;
HANDLE hUSB = NULL;
INT nRet;
ULONG nLen = 0;
ULONG nRetLen = 0;
CHAR szTemp[16];
ULONG StartAddress = 0;
UCHAR OutBuffer[68];
BOOL bResult = FALSE;
INT nFileLen = 0;
INT nCount = 0;
CString strTime;
time_t long_time,long_temp;
time( &long_time );
memset( szPath , 0 , _MAX_PATH );
dlg->m_EdtFilePath.GetWindowText( szPath , _MAX_PATH );
memset( szTemp , 0 , 16 );
dlg->m_EdtAddress.GetWindowText( szTemp , 16 );
//StartAddress = atoi( &szTemp[2] );
sscanf( szTemp , "0x%x" , &StartAddress );
//打开文件
File = CreateFile( szPath , GENERIC_READ , FILE_SHARE_READ , NULL , OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , NULL );
if( NULL == File )
{
AfxMessageBox( "打开文件失败!" );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
nFileLen = GetFileSize( File , NULL );
//打开USB驱动
hUSB = open_dev();
if( INVALID_HANDLE_VALUE == hUSB )
{
AfxMessageBox( "打开USB端口失败!" );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
CloseHandle( File );
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0;
}
dlg->m_LabelStatus.SetWindowText( "写入" );
//写入flash
dlg->m_Progress.SetRange( 0 , nFileLen/PACKETSIZE );
while( dlg->m_WriteFlag )
{
//从文件读数据
nRet = ReadFile( File , InBuffer+10 , PACKETSIZE , &nLen , NULL );
if( nRet == -1 )
{
AfxMessageBox( "读取文件失败!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
if( nLen == 0 )
break;
InBuffer[0] = 0x5A;
InBuffer[1] = 0xA5;
InBuffer[2] = 0x01;//写flash命令
InBuffer[3] = (UCHAR)nLen;
*(ULONG*)(InBuffer+4) = StartAddress;
InBuffer[8] = InBuffer[9] = 0;
// memset( OutBuffer , 0 , 64 );
//*
//写入Flash
bResult = DeviceIoControl(hUSB,
IOCTL_USBDRV_TRANSFER_HOST_TO_DEVICE,
(PVOID)InBuffer,
64,
OutBuffer,
14,
&nRetLen,
NULL);
if( !bResult )
{//失败了
AfxMessageBox( "USB操作失败!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
//*/
/*
if( OutBuffer[0+4] != 0x5A || OutBuffer[1+4] != 0xA5 )
{
AfxMessageBox( "USB传输错误!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
if( OutBuffer[2+4] != 0x02 )
{
AfxMessageBox( "命令执行错误!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
if( *(ULONG*)(OutBuffer+4+4) != StartAddress || OutBuffer[3+4] != nLen )
{
AfxMessageBox( "写入flash错误!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
*/
StartAddress += nLen;
nCount ++;
if( dlg->m_WriteFlag )
dlg->m_Progress.SetPos( nCount );
/*
if( nCount > nFileLen/PACKETSIZE )
break;
*/
// Sleep( 100 );
}//while( dlg->m_WriteFlag ) 写flash
time( &long_temp );
strTime.Format( "%d秒" , long_temp-long_time );
dlg->m_LableTime.SetWindowText( strTime );
/*
memset( szTemp , 0 , 16 );
dlg->m_EdtAddress.GetWindowText( szTemp , 16 );
sscanf( szTemp , "0x%x" , &StartAddress );
SetFilePointer( File , 0 , NULL , FILE_BEGIN );
dlg->m_LabelStatus.SetWindowText( "校验" );
dlg->m_Progress.SetRange( 0 , nFileLen/54 );
nCount = 0;
dlg->m_Progress.SetPos( nCount );
//校验
while( dlg->m_WriteFlag )
{
//从文件读数据
nRet = ReadFile( File , InBuffer+10 , 54 , &nLen , NULL );
if( nRet == -1 )
{
AfxMessageBox( "读取文件失败!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
if( nLen == 0 )
break;
InBuffer[0] = 0x5A;
InBuffer[1] = 0xA5;
InBuffer[2] = 0x03;//写flash命令
InBuffer[3] = (UCHAR)nLen;
*(ULONG*)(InBuffer+4) = StartAddress;
InBuffer[8] = InBuffer[9] = 0;
//校验
bResult = DeviceIoControl(hUSB,
IOCTL_USBDRV_REQUEST_ANSWER,
(PVOID)InBuffer,
10,
OutBuffer,
14+nLen,
&nRetLen,
NULL);
if( !bResult )
{//失败了
AfxMessageBox( "USB操作失败!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
if( OutBuffer[0+4] != 0x5A || OutBuffer[1+4] != 0xA5 )
{
AfxMessageBox( "USB传输错误!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
if( OutBuffer[2+4] != 0x04 )
{
AfxMessageBox( "命令执行错误!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
if( *(ULONG*)(OutBuffer+4+4) != StartAddress || OutBuffer[3+4] != nLen )
{
AfxMessageBox( "写入flash错误!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
//数据比较
if( memcmp( (InBuffer+10) , (OutBuffer+10+4) , nLen ) )
{
AfxMessageBox( "数据校验出错!" );
CloseHandle( File );
//关闭USB驱动
CloseHandle( hUSB );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
StartAddress += nLen;
//进度条
nCount ++;
if( dlg->m_WriteFlag )
dlg->m_Progress.SetPos( nCount );
Sleep( 100 );
}//while( dlg->m_WriteFlag ) 校验
*/
CloseHandle( File );
CloseHandle( hUSB );
if( dlg->m_WriteFlag )
AfxMessageBox( "写入Flash完成!" );
SetEvent( dlg->m_ThreadEvent );
dlg->m_WriteFlag = FALSE;
dlg->m_BtnWrite.SetWindowText( "写入" );
return 0 ;
}
void CFlashToolsDlg::OnUpdateEdtAddress()
{
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?