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

📄 downloderdlg.cpp

📁 vc++编写的adu812下载程序源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// downloderDlg.cpp : implementation file
//

#include "stdafx.h"
#include "downloder.h"
#include "downloderDlg.h"
#include "math.h"
#include "stdio.h"
#include "direct.h"
#include "string.h"
#include "process.h"
#include "conio.h"
#include "stdlib.h"


#define MAXBLOCK 4096 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BOOL okdata=TRUE;
char strBufd[256];
char strBuf[256];
int sumlength=0;
char m_data[300];
OVERLAPPED m_osRead,m_osWrite;
HANDLE     hCommDev;
int m_Break=1;
int timenum=0;



UINT   CommWatchThread(LPVOID pParam);
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDownloderDlg dialog

CDownloderDlg::CDownloderDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDownloderDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDownloderDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
   comThread=NULL;
}

void CDownloderDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDownloderDlg)
	DDX_Control(pDX, IDC_FILELIST, m_filelist);
	DDX_Control(pDX, IDC_DIRLIST, m_dirlist);
	DDX_Control(pDX, IDC_PROGRESS1, m_progress);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDownloderDlg, CDialog)
	//{{AFX_MSG_MAP(CDownloderDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PROGRAM, OnProgram)
	ON_LBN_DBLCLK(IDC_DIRLIST, OnDblclkDirlist)
	ON_LBN_SELCHANGE(IDC_FILELIST, OnSelchangeFilelist)
	ON_WM_CREATE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDownloderDlg message handlers

BOOL CDownloderDlg::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_dirlist.Dir (DDL_DRIVES|DDL_DIRECTORY|DDL_EXCLUSIVE,"*.*");
		m_filelist.Dir(DDL_READWRITE|DDL_ARCHIVE,"*.*");
	    getcwd(strBufd,256);
		GetDlgItem(IDC_DIRTEXT)->SetWindowText(strBufd);
 //       m_EditFilename.GetWindowText(strBuff,256);
	//////////////////////////////////////
	///////////////////curve display thread
	
	 
	   //////////////////////////////////////
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDownloderDlg::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 CDownloderDlg::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 CDownloderDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDownloderDlg::OnProgram() 
{
	// TODO: Add your control notification handler code here
    ////////////////////////////////verify aduc812
   AfxBeginThread(&programm,(LPVOID)this);
 
 }
////////////////////////////////////////////////////////////////
UINT CDownloderDlg::programm(LPVOID lParam)//program thread
{

CDownloderDlg *pt= (CDownloderDlg *)lParam;
int m_nProgress=0;//初始进度条为0
BOOL ok_verify=TRUE,ok_erase=TRUE,ok_program=TRUE;
char tempbuf[MAXBLOCK/4];

timenum=0;
sumlength=0;


pt->m_progress.SetPos(1);
pt->GetDlgItem(IDC_STATE)->SetWindowText(" 正在准备编程!");

unsigned char buffer1[]={0x21,0x5a,0x00,0xa6};//send to aduc812
pt->WriteComm(buffer1,4);

 // strcpy(m_data,"trruyADI 812   V201");//////////////////////////

do{
 
  strncpy(tempbuf,m_data,sumlength);

  for(int i=0;i<sumlength;i++)
  {
 
  if((tempbuf[i]=='A')&&(tempbuf[i+1]=='D')&&(tempbuf[i+2]=='I'))
  {
	 for(int j=0;j<25;j++)tempbuf[j]=tempbuf[j+i];

     if(!strncmp(tempbuf,"ADI 812   V2",12))
	 { 
		 ok_verify=FALSE;
         pt->GetDlgItem(IDC_STATE)->SetWindowText(" 器件验证正确!");
         sumlength=0;
		 break;
	 }
     else 
	 {
		 pt->GetDlgItem(IDC_STATE)->SetWindowText(" 器件不匹配!");
         pt->m_progress.SetPos(0);
		 return FALSE;
	 }
  }
 }
  if(timenum>10){
	  pt->GetDlgItem(IDC_STATE)->SetWindowText(" 找不到器件!");
      pt->m_progress.SetPos(0);
	  return FALSE;
  }
}while(ok_verify);
/////////////////////////////erase aduc812
pt->GetDlgItem(IDC_STATE)->SetWindowText(" 正在擦除器件!");
unsigned char buffer2[]={0x07,0x0e,0x01,0x43,0xbc};//send to aduc812
pt->WriteComm(buffer2,5);

//strcpy(m_data,"trruyADI 812   V201");/////////////////////////
//m_data[19]=0x06;/////////////////////////////////////////////
//sumlength=20;/////////////////////////////////////////////////
timenum=0;
do{
 
  strncpy(tempbuf,m_data,sumlength);

  for(int k=0;k<sumlength;k++)
  {
 
	  if(tempbuf[k]==0x06){ 
		  ok_erase=FALSE;
           pt->GetDlgItem(IDC_STATE)->SetWindowText("擦除成功!");
           sumlength=0;
		  break;
	  }
      if(tempbuf[k]==0x07) {
		  pt->GetDlgItem(IDC_STATE)->SetWindowText("擦除错误 !");
          return FALSE;
	  }
  }
  if(timenum>10){
	  pt->GetDlgItem(IDC_STATE)->SetWindowText("擦除错误 !");
      pt->m_progress.SetPos(0);
	  return FALSE;
  }
}while(ok_erase);

///////////////////////////////send program data
//file process
    
       int  lenofFile=0;
       CFile  DataFile;
       HANDLE hFile;
       int  lencount=0;
       LPSTR    pFile;
 
  
     if(strstr(strBuf,".hex")!=NULL)// check file type
	 {

       if(DataFile.Open(strBuf,CFile::modeRead|CFile::modeRead|
		   CFile::typeBinary|CFile::shareExclusive)!=NULL)// 是否有此文件
	   {
	   lenofFile=DataFile.SeekToEnd();
       lencount=lenofFile;//////////////

	   hFile=::GlobalAlloc(GHND,(long)(lenofFile+100));
	   pFile=(LPSTR)::GlobalLock(hFile);
	   memset(pFile,0xffff,(long)(lenofFile+100));

	   DataFile.SeekToBegin();
       DataFile.ReadHuge(pFile,(long)(lenofFile+100));
	   DataFile.Close();
        }
	   else
	   {
        pt->GetDlgItem(IDC_STATE)->SetWindowText("找不到此文件或文件路径错误!");
        return FALSE;
	   }
  }

   else
   {
     pt->GetDlgItem(IDC_STATE)->SetWindowText("文件类型错误!");
     return FALSE;
   }

///////////////////////////////////////////////
        int framecount=0;
	
        for(int ip=0;ip<lencount;ip++)
		 {
              if(pFile[ip]==':')
			  {
                  framecount++;
			  }
		 }
       pt->m_progress.SetRange(0,framecount);


	   int kk=0,team=1,bytes=0;
	   unsigned char buffer3[50];
	

       pt->GetDlgItem(IDC_STATE)->SetWindowText("正在编程!");
       framecount--;

       while(framecount>0)
	   {    
             int num=0;
			 unsigned char chknum=0;

⌨️ 快捷键说明

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