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

📄 bulkxferdlg.cpp

📁 基于VC的EZ-USB的开发 EZ-USB含有标准内核
💻 CPP
字号:
// BulkXferDlg.cpp : implementation file
//


#include "stdafx.h"
#include "BulkXfer.h"
#include "BulkXferDlg.h"
#include "devioctl.h"
#include "ezusbsys.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBulkXferDlg dialog

CBulkXferDlg::CBulkXferDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBulkXferDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBulkXferDlg)
	m_BlkSize = 1;
	m_InData = _T("");
	m_Outdata = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBulkXferDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBulkXferDlg)
	DDX_Control(pDX, IDC_COMBO1, m_DriveName);
	DDX_Text(pDX, IDC_BLKSIZE, m_BlkSize);
	DDV_MinMaxInt(pDX, m_BlkSize, 1, 64);
	DDX_Text(pDX, IDC_INDATA, m_InData);
	DDX_Text(pDX, IDC_OUTDATA, m_Outdata);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBulkXferDlg, CDialog)
	//{{AFX_MSG_MAP(CBulkXferDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnClear_InData)
	ON_BN_CLICKED(IDC_BUTTON2, OnClear_OutData)
	ON_EN_CHANGE(IDC_INDATA, OnChangeIndata)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBulkXferDlg message handlers

BOOL CBulkXferDlg::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
	
	SetupDriverComboBox(); // look for attached EZ-USB devices

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

void CBulkXferDlg::OnClear_InData() 
{
	m_InData = "";
	UpdateData(FALSE);
}

void CBulkXferDlg::OnClear_OutData() 
{
	m_Outdata = "";
	UpdateData(FALSE);
}



void CBulkXferDlg::OnChangeIndata() 
{

	BULK_TRANSFER_CONTROL btc;
	ULONG nBytes;
	BOOLEAN bResult;
	HANDLE hDevice;
	CString sDriverName;
	int n;

	CComboBox* pCBox = GetCComboBox(); // inline defined in BulkXFERDlg.h

	// get the driver name from the combobox
    n = pCBox->GetLBTextLen( pCBox->GetCurSel() );
    pCBox->GetLBText( pCBox->GetCurSel(), sDriverName.GetBuffer(n));
    sDriverName.ReleaseBuffer();


	// update member variables from text box controls
	UpdateData(TRUE);

	// right-most char represents last keypress
	// append right-most char to "trigger" buffer
	m_StrBuffer = m_StrBuffer + m_InData.Right (1);

	// if we have collected a "block", do the bulk xfer
	if (m_StrBuffer.GetLength() == m_BlkSize)
	{
		// extract the individual characters and put them 
		// into the byte buffer
		for (int i = 0; i< m_StrBuffer.GetLength(); i++)
		{
			m_Buffer[i] = m_StrBuffer.GetAt(i);
		}

		// open driver
		if (OpenDriver(&hDevice,sDriverName))
		{

			// we use pipes 1 and 0 because that's what "ep-pair.hex" sets up

			btc.pipeNum = 1;

			// write data OUT to EZUSB device
			bResult = DeviceIoControl (hDevice,
						IOCTL_EZUSB_BULK_WRITE, 
						&btc,
						sizeof (BULK_TRANSFER_CONTROL),
						m_Buffer,
						m_StrBuffer.GetLength(),
						(unsigned long *)&nBytes,
						NULL);

			if (!bResult)
				AfxMessageBox("Correct Pipe not found. Perhaps \"Ep-pair.hex\" was not downloaded to a development board.", MB_OK|MB_ICONSTOP);
			else
			{
				// no rabbits up our sleeve
				for (i = 0; i < 63; i++)
					m_Buffer[i] = 0;

				btc.pipeNum = 0;

				// read data IN from EZUSB device
				bResult = DeviceIoControl (hDevice,
						IOCTL_EZUSB_BULK_READ, 
						&btc,
						sizeof (BULK_TRANSFER_CONTROL),
						m_Buffer,
						m_StrBuffer.GetLength(),
						(unsigned long *)&nBytes,
						NULL);

				if (!bResult)
					AfxMessageBox("Correct Pipe not found. Perhaps \"Ep-pair.hex\" was not downloaded to a development board.", MB_OK|MB_ICONSTOP);
				else
				{
					// move data from buffer to output member variable
					for (i = 0; i < m_StrBuffer.GetLength(); i++)
						m_Outdata += m_Buffer[i];

					// clear out "trigger" buffer
					m_StrBuffer.Empty();

					// transfer data from output member variable to output textbox 
					UpdateData(FALSE);

				}

			}

		}

	}
}

void CBulkXferDlg::SetupDriverComboBox() {
	
CString sDriverName, sCompleteName;

char num[3] = {0,0,0};

HANDLE hDriver;

	CComboBox* pCBox = GetCComboBox(); // inline defined in BulkXFERDlg.h

	pCBox->ResetContent ();

	// Assume 32 EZ-USB boards are attached to the PC.
	// Try to open each driver.  If successful, add driver name to combobox

	for (int i = 0; i < MAX_USB_DEV_NUMBER; i++)
	{
		_itoa( i, num, 10 );
   
		sDriverName = (CString)"Ezusb-" + num;

		if (OpenDriver(&hDriver, sDriverName))
			pCBox->AddString ((LPCTSTR) sDriverName);
		else
		{
			if (i==0)
			{
				AfxMessageBox("No EZ-USB device drivers were found.  Perhaps no device is connected", MB_OK|MB_ICONSTOP);
				exit(0);
			}
		}

	}

	pCBox->SetCurSel(0);
}


BOOLEAN
CBulkXferDlg::OpenDriver (HANDLE* phDriver, CString devname)
{
	SECURITY_ATTRIBUTES security;

	security.nLength = sizeof(security);
	security.lpSecurityDescriptor = NULL;
	security.bInheritHandle = false;

    CString completeDeviceName = (CString)"\\\\.\\" + devname;

    *phDriver = CreateFile(   completeDeviceName,
		                            GENERIC_WRITE,
		                            FILE_SHARE_WRITE,
		                            &security,
		                            OPEN_EXISTING,
		                            0,
		                            NULL);

    return (*phDriver != INVALID_HANDLE_VALUE);

}

⌨️ 快捷键说明

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