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

📄 avr_usbdlg.cpp

📁 usb 程序 usb通讯 仿真程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AVR_USBDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AVR_USB.h"
#include "AVR_USBDlg.h"
#include "usbD12dll.h"
#include "Mmsystem.h"

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


static bool cOnwendu;

/////////////////////////////////////////////////////////////////////////////
// CAVR_USBDlg dialog

CAVR_USBDlg::CAVR_USBDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAVR_USBDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAVR_USBDlg)
	m_wendu = _T("温度:");
	m_rsec = 0;
	m_rcol = 0;
	m_rrow = 0;
	m_wsec = 0;
	m_wcol = 0;
	m_wrow = 0;
	m_COMtoUSB = _T("");
	m_USBtoCOM = _T("");
	m_wrFlash = _T("");
	m_rdFlash = _T("");
	m_Erase = 0;
	m_Prd = 0;
	m_Pwr = 0;
	m_wrL = _T("");
	m_rL = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAVR_USBDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAVR_USBDlg)
	DDX_Text(pDX, IDC_WENDU, m_wendu);
	DDX_Text(pDX, IDC_REDIT1, m_rsec);
	DDX_Text(pDX, IDC_REDIT2, m_rcol);
	DDX_Text(pDX, IDC_REDIT3, m_rrow);
	DDX_Text(pDX, IDC_WEDIT1, m_wsec);
	DDX_Text(pDX, IDC_WEDIT2, m_wcol);
	DDX_Text(pDX, IDC_WEDIT3, m_wrow);
	DDX_Text(pDX, IDC_EDIT5, m_COMtoUSB);
	DDX_Text(pDX, IDC_EDIT6, m_USBtoCOM);
	DDX_Text(pDX, IDC_WEDIT4, m_wrFlash);
	DDX_Text(pDX, IDC_REDIT4, m_rdFlash);
	DDX_Text(pDX, IDC_Erase, m_Erase);
	DDX_Text(pDX, IDC_Prd, m_Prd);
	DDX_Text(pDX, IDC_Pwr, m_Pwr);
	DDX_Text(pDX, IDC_WEDIT5, m_wrL);
	DDX_Text(pDX, IDC_REDIT5, m_rL);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAVR_USBDlg, CDialog)
	//{{AFX_MSG_MAP(CAVR_USBDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON6, Onwendu)
	ON_BN_CLICKED(IDC_PB0, OnPb0)
	ON_BN_CLICKED(IDC_PB1, OnPb1)
	ON_BN_CLICKED(IDC_PB2, OnPb2)
	ON_BN_CLICKED(IDC_PB3, OnPb3)
	ON_BN_CLICKED(IDC_PB4, OnPb4)
	ON_BN_CLICKED(IDC_PB5, OnPb5)
	ON_BN_CLICKED(IDC_PB6, OnPb6)
	ON_BN_CLICKED(IDC_PB7, OnPb7)
	ON_BN_CLICKED(IDC_BUTTON7, OnUSBtoCOM)
	ON_BN_CLICKED(IDC_BUTTON1, OnCopyPage)
	ON_BN_CLICKED(IDC_BUTTON2, OnFlashErase)
	ON_BN_CLICKED(IDC_BUTTON5, OnWriteCOM)
	ON_BN_CLICKED(IDC_BUTTON4, OnWriteFlash)
	ON_BN_CLICKED(IDC_BUTTON3, OnReadFlash)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAVR_USBDlg message handlers

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

	// 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

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

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

void CAVR_USBDlg::OnFlashErase() 
{
    char temp [80],cc[80],wrBuffer[16];
    CAVR_USBDlg* testDlg;
	BOOLEAN bResult = FALSE;
    ULONG nBytes;
	HANDLE hFile=NULL;
	testDlg=this;

	SetTimer(100,-1,NULL);

	UpdateData(TRUE);
	
	if(m_Erase<0 && m_Erase>65535)
	{
		strcpy(temp, "起始行应在 0~65535 之间!");
		testDlg->MessageBox((LPCSTR)temp, "错误!");
		return;
	}
	
	hFile = openUSBfile_0();
	if(hFile == INVALID_HANDLE_VALUE)
	{
		strcpy(temp, "不能写数据到pipe_0!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
		return;
	}
	wrBuffer[0] = 0x60;
	wrBuffer[1] = m_Erase;
	wrBuffer[2] = m_Erase>>8;
	int wrLength=3;
	bResult = WriteFile(hFile,
		&wrBuffer,
		wrLength,
		&nBytes,
		NULL);
				
	CloseHandle(hFile);
	if (bResult == TRUE)
	{
		hFile = openUSBfile_1();
		if(hFile == INVALID_HANDLE_VALUE)
		{
			strcpy(temp, "不能打开pipe_1!");
			testDlg->MessageBox((LPCSTR)temp, "打开错误!");
			return;
		}
		bResult = ReadFile(hFile,
			&wrBuffer,
			wrLength,
			&nBytes,
			NULL);
		bResult = ReadFile(hFile,
			&wrBuffer,
			wrLength,
			&nBytes,
			NULL);
		if (bResult == TRUE && nBytes>0)
		{
			if(wrBuffer[0]&0x01)
			{
				strcpy (cc, "擦除失败! ");
				testDlg->MessageBox((LPCSTR)cc, "擦除错误!");
			}
		}else
		{
			strcpy (cc, "读 pipe_1 失败! ");
			testDlg->MessageBox((LPCSTR)cc, "读出错误!");
		}	
		
		CloseHandle(hFile);
	}else
	{
		strcpy (cc, "写pipe_0失败! ");
		testDlg->MessageBox((LPCSTR)cc, "写入错误!");
	}
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnCopyPage() 
{
    char temp [80],cc[80],wrBuffer[16];
    CAVR_USBDlg* testDlg;
	BOOLEAN bResult = FALSE;
    ULONG nBytes;
	HANDLE hFile=NULL;
	testDlg=this;

	SetTimer(100,-1,NULL);

	UpdateData(TRUE);

	if(m_Prd<0 && m_Prd>65535)
	{
		strcpy(temp, "拷贝起始行应在 0~65535 之间!");
		testDlg->MessageBox((LPCSTR)temp, "错误!");
		return;
	}
	if(m_Pwr<0 && m_Pwr>65535)
	{
		strcpy(temp, "拷贝行应在 0~65535 之间!");
		testDlg->MessageBox((LPCSTR)temp, "错误!");
		return;
	}
	
	hFile = openUSBfile_0();
	if(hFile == INVALID_HANDLE_VALUE)
	{
		strcpy(temp, "不能写数据到pipe_0!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
		return;
	}
	wrBuffer[0] = 0x8a;
	wrBuffer[1] = m_Prd;
	wrBuffer[2] = m_Prd>>8;
	wrBuffer[3] = m_Pwr;
	wrBuffer[4] = m_Pwr>>8;
	int wrLength=5;
	bResult = WriteFile(hFile,
		&wrBuffer,
		wrLength,
		&nBytes,
		NULL);
				
	CloseHandle(hFile);
	if (bResult == TRUE)
	{
		hFile = openUSBfile_1();
		if(hFile == INVALID_HANDLE_VALUE)
		{
			strcpy(temp, "不能打开pipe_1!");
			testDlg->MessageBox((LPCSTR)temp, "打开错误!");
			return;
		}
		bResult = ReadFile(hFile,
			&wrBuffer,
			wrLength,
			&nBytes,
			NULL);
		bResult = ReadFile(hFile,
			&wrBuffer,
			wrLength,
			&nBytes,
			NULL);
		if (bResult == TRUE && nBytes>0)
		{
			if(wrBuffer[0]&0x01)
			{
				strcpy (cc, "拷贝失败! ");
				testDlg->MessageBox((LPCSTR)cc, "拷贝错误!");
			}
		}else
		{
			strcpy (cc, "读 pipe_1 失败! ");
			testDlg->MessageBox((LPCSTR)cc, "读出错误!");
		}	
		
		CloseHandle(hFile);
	}else
	{
		strcpy (cc, "写pipe_0失败! ");
		testDlg->MessageBox((LPCSTR)cc, "写入错误!");
	}
    if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnWriteFlash() 
{
    char temp [80],cc[80],cwrBuffer[16];
    CAVR_USBDlg* testDlg;
	BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	HANDLE hFile=NULL,hFile1=NULL;
	testDlg=this;

	SetTimer(100,-1,NULL);

	hFile1 = openUSBfile_0();
	if(hFile1 == INVALID_HANDLE_VALUE)
	{
		strcpy(temp, "不能写数据到pipe_0!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
		return;
	}
	UpdateData(TRUE);
	
	ULONG wrLength;
	wrLength = m_wrFlash.GetLength();
	cwrBuffer[0] = 0x80;
	cwrBuffer[1] = m_wsec;//Mflash.sec 分区
	cwrBuffer[2] = m_wcol;//Mflash.col 列
	cwrBuffer[3] = m_wrow;//Mflash.row 行
	cwrBuffer[4] = m_wrow>>8;
	cwrBuffer[5] = wrLength;//Mflash.WriteLength
	cwrBuffer[6] = wrLength>>8;
	cwrBuffer[7] = wrLength>>16;
	cwrBuffer[8] = wrLength>>24;
	int cwrLength=9;
	bResult = WriteFile(hFile1,
		&cwrBuffer,
		cwrLength,
		&nBytes,
		NULL);

	hFile = openUSBfile_2();
	if(hFile == INVALID_HANDLE_VALUE)
	{
		strcpy(temp, "不能写数据到pipe_2!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
		CloseHandle(hFile1);
		return;
	}
	bResult = WriteFile(hFile,
		m_wrFlash,
		wrLength,
		&nBytes,
		NULL);
				
 	if (bResult == TRUE)
	{
		if(nBytes>=0)
		{
			sprintf(temp,"%d",nBytes);
			m_wrL = temp;
			UpdateData(FALSE);
		}
	}else
	{
		strcpy (cc, "写pipe_2失败!");
		testDlg->MessageBox((LPCSTR)cc, "写入错误!");
	}
	CloseHandle(hFile);
	CloseHandle(hFile1);

	if (bResult == TRUE)
	{
		hFile = openUSBfile_1();
		if(hFile == INVALID_HANDLE_VALUE)
		{
			strcpy(temp, "不能打开pipe_1!");
			testDlg->MessageBox((LPCSTR)temp, "打开错误!");
			return;
		}
		bResult = ReadFile(hFile,
			&cwrBuffer,
			wrLength,
			&nBytes,
			NULL);
		bResult = ReadFile(hFile,
			&cwrBuffer,
			wrLength,
			&nBytes,
			NULL);
		if (bResult == TRUE && nBytes>0)
		{
			if(cwrBuffer[0]&0x01)
			{
				strcpy (cc, "写入失败! ");
				testDlg->MessageBox((LPCSTR)cc, "错误!");
			}
		}else
		{
			strcpy (cc, "读 pipe_1 失败! ");
			testDlg->MessageBox((LPCSTR)cc, "读出错误!");
		}	
		
		CloseHandle(hFile);
	}
	if(cOnwendu==1)
	{
		usb_command(0x02,0);
	}
}

void CAVR_USBDlg::OnReadFlash() 
{
    char temp [80],cc[80],wrBuffer[16];
	CAVR_USBDlg* testDlg;
	BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	HANDLE hFile=NULL,hFile1=NULL;

	testDlg=this;
	
	UpdateData(TRUE);
	if(m_rL   <0 || m_rL   >34603008)
	{
		strcpy(temp, "读取长度应在 0~34603008 之间!");
		testDlg->MessageBox((LPCSTR)temp, "错误!");
		return;
	}
	hFile1 = openUSBfile_0();
	if(hFile1 == INVALID_HANDLE_VALUE)
	{
		strcpy(temp, "不能写数据到pipe_0!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
		return;
	}
	wrBuffer[0] = m_rsec;//Mflash.sec 分区
	wrBuffer[1] = m_rcol;//Mflash.col 列
	wrBuffer[2] = m_rrow;//Mflash.row 行
	wrBuffer[3] = m_rrow>>8;
	wrBuffer[4] = m_rL;//Mflash.WriteL
	wrBuffer[5] = m_rL>>8;
	wrBuffer[6] = m_rL>>16;
	wrBuffer[7] = m_rL>>24;
	int wrLength=8;
	bResult = WriteFile(hFile1,
		&wrBuffer,
		wrLength,
		&nBytes,
		NULL);

	hFile = openUSBfile_3();
	if(hFile == INVALID_HANDLE_VALUE) {
		strcpy(temp, "不能从 pipe_3 读出数据!");
		testDlg->MessageBox((LPCSTR)temp, "打开错误!");
		CloseHandle(hFile1);
		return;
	}
	unsigned char rdBuffer[645];
	int rdLength=640;
	bResult = ReadFile(hFile,
		&rdBuffer,
		rdLength,
		&nBytes,
		NULL);
	if (bResult == TRUE)
	{
		bResult = ReadFile(hFile,
				&rdBuffer,
				rdLength,
				&nBytes,
				NULL);
	}
	
	if (bResult == TRUE)
	{
		if(nBytes>=0)
		{
			rdBuffer[nBytes] = 0;
			m_rdFlash = rdBuffer;
			UpdateData(FALSE);
		}
	}else
	{
		strcpy (cc, "读 pipe_3 失败!");
		testDlg->MessageBox((LPCSTR)cc, "读出错误!");
	}	
	
	CloseHandle(hFile);
	CloseHandle(hFile1);
}

void CAVR_USBDlg::OnTimer(UINT nIDEvent) 
{
    char temp [80],c=0;
	BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	HANDLE hFile=NULL;
	
	long mtemp[10];
	
	mtemp[0] = m_rsec;
	mtemp[1] = m_rcol;

⌨️ 快捷键说明

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