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

📄 sevraldatasdlg.cpp

📁 windows 界面编程
💻 CPP
字号:
// SevralDatasDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SevralDatas.h"
#include "SevralDatasDlg.h"
#include "USB20D.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#define N 4096*3
#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()

/////////////////////////////////////////////////////////////////////////////
// CSevralDatasDlg dialog

CSevralDatasDlg::CSevralDatasDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSevralDatasDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSevralDatasDlg)
		// 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);
}

void CSevralDatasDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSevralDatasDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSevralDatasDlg, CDialog)
	//{{AFX_MSG_MAP(CSevralDatasDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_WRITE, OnWrite)
	ON_BN_CLICKED(IDC_ERASE, OnErase)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSevralDatasDlg message handlers

BOOL CSevralDatasDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CSevralDatasDlg::OnStart() 
{
	// TODO: Add your control notification handler code here

		
		CString str_Report;
		if (USB20D_EnumDeviceCount()==0)
		{
			MessageBox("No device was found!");
		}
		else
		{  
			
		    HANDLE m_hDevice=USB20D_Init(0,1);
			USB20D_EndDMA(m_hDevice);
			USB20D_ResetDMAFIFO(m_hDevice);
			
			if (!USB20D_WorkAtHighSpeed(m_hDevice)) MessageBox("Initial Failed!");
			else
			{  
			//	MessageBox("Initial Suceed!");
				CFile f("shuju.txt",CFile::modeCreate | CFile::modeWrite);
				USB20D_SetCPUClk(m_hDevice,CPU48MHz,FALSE,FALSE);
				BOOL n=USB20D_SetDMAClk(m_hDevice,TRUE,TRUE,TRUE,FALSE,FALSE,TRUE);
				if (n)
				{
					DWORD  dataneed=N;
					int i;
					WORD* buffer;	   
					DWORD* m_nRead;
			//		BOOL  OutFIFOEmpty;
			//		BOOL  OutFIFOFull;
			//		BOOL  InFIFOEmpty;
			//		BOOL  InFIFOFull;
					
					 
						buffer=(WORD*)malloc(N);
						m_nRead=(DWORD*)buffer-2;
						USB20D_EndDMA(m_hDevice);
						USB20D_ResetDMAFIFO(m_hDevice);
				//		BOOL fifo3=USB20D_DMAFIFOStatus(m_hDevice,&OutFIFOEmpty, &OutFIFOFull, &InFIFOEmpty, &InFIFOFull);
				//		if(InFIFOEmpty) MessageBox("Start前:InFIFOEmpty=1");
						USB20D_StartDMA(m_hDevice,TRUE,TRUE,0);
				//		BOOL fifo1=USB20D_DMAInFIFOEmpty(m_hDevice);
				//		if(fifo1) MessageBox("Start后Read前:InFIFOEmpty=1");
					//	Sleep(10);
					//	USB20D_EndDMA(m_hDevice);
					
						{
						//	USB20D_EndDMA(m_hDevice);
						//	USB20D_ResetDMAFIFO(m_hDevice);
						//	USB20D_Output(m_hDevice, 0x4,	0x2);
						}
						BOOL m_bReadSuceed=USB20D_DMARead(m_hDevice,buffer,dataneed,m_nRead,4000);
						USB20D_EndDMA(m_hDevice);
						if(!m_bReadSuceed){
						CString str1;						
						int Error=USB20D_GetLastError(m_hDevice);
						str1.Format("Error=%d",Error);
						MessageBox(str1);
						}
						//	USB20D_StartDMA(m_hDevice,TRUE,TRUE,0);
					//	Sleep(1000);
					 //   USB20D_EndDMA(m_hDevice);	
				//		BOOL fifo2=USB20D_DMAInFIFOEmpty(m_hDevice);
				//		if(fifo2) MessageBox("Read后:InFIFOEmpty=1");
						if (dataneed==(*m_nRead))
						{
	//						CArchive ar(&f,CArchive::store,N); 
							CString str;
							for(i=0;i<(N/2);i++)
							{
								if(!(i%16)&&i) f.Write("\r\n",2);
								str.Format("%4x ",*(buffer+i));
								f.Write(str,str.GetLength());
								
							}
							MessageBox("Reading Suceeded");
							free(buffer);
						}   
						else 
						{
							MessageBox("Reading Failed");
							free(buffer);
						//	break;
						}
						
				}			
			}
	//		BOOL fifo4=USB20D_DMAInFIFOEmpty(m_hDevice);
	//		if(fifo4) MessageBox("结束前:InFIFOEmpty=1");
			USB20D_EndDMA(m_hDevice);
			USB20D_Done(m_hDevice);
		}
		
}






















void CSevralDatasDlg::OnWrite() 
{
	// TODO: Add your control notification handler code here
	if (USB20D_EnumDeviceCount()==0)
	{
		MessageBox("No device was found!");
	}
	else
	{  		
		HANDLE m_hDevice=USB20D_Init(0,1);
		if (!USB20D_WorkAtHighSpeed(m_hDevice)) {
			MessageBox("Not in high speed!");
			USB20D_Done(m_hDevice);
		}
		else {
			int i=0,j=0;
			int finish;
			CFile f("finish.txt",CFile::modeCreate | CFile::modeWrite);
			if(USB20D_EndDMA(m_hDevice)) MessageBox("开始写");
	//		MessageBox("开始写");
			USB20D_Output(m_hDevice, 4, 0x07);
			USB20D_Output(m_hDevice, 0, 0x00);
			while (1) {
				Sleep(1);
				if(j==50){
					MessageBox("写失败");
					USB20D_Output(m_hDevice, 4, 0x08);  //停止写
					break;
				}
				else j=j+1;
				finish=USB20D_Input(m_hDevice, 5);					
	//			CArchive ar(&f,CArchive::store,10); 
				CString str;
				str.Format("%4x ",finish);
				f.Write(str,str.GetLength());
					
				if(finish==0x10){
					MessageBox("64页写成功");
					USB20D_Output(m_hDevice, 4, 0x08);  //停止写
					break;				
				}
			}
		}
	}	
}




void CSevralDatasDlg::OnErase() 
{
	// TODO: Add your control notification handler code here
	    HANDLE hDevice=USB20D_Init(0,1);
		if (!USB20D_WorkAtHighSpeed(hDevice)) {
			MessageBox("Not in high speed!");
			USB20D_Done(hDevice);
		}
		else{
		USB20D_EndDMA(hDevice);
		if(USB20D_Output(hDevice, 1, 0x60)) {
			MessageBox("确定格式化?");
			USB20D_Output(hDevice, 3, 0xD0);
		}
		int i=0;
		int finish;
		while (1) {
			if(i==50) { MessageBox("擦除失败,限定时间到");break;}
			else i=i+1;
			finish=USB20D_Input(hDevice, 2);
			if(finish==0x03){
				MessageBox("第一块擦除成功.");
				break;
			} else {
				if(finish==0x04){
					MessageBox("第一块擦除失败.");
					break;
				}
				
			}
		}
		//	}
		//	else MessageBox("没能开始擦除");
		USB20D_Output(hDevice, 3, 0x65);
		USB20D_Done(hDevice);
		}
	
}

⌨️ 快捷键说明

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