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

📄 skydlg.cpp

📁 下端基于AN2131的USB单片机同PC 通讯的VC 代码
💻 CPP
字号:
// SkyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Sky.h"
#include "SkyDlg.h"

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

#include "winioctl.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSkyDlg dialog

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

void CSkyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSkyDlg)
	DDX_Control(pDX, IDC_SENDDATA, m_SendData);
	DDX_Control(pDX, IDC_RECEDATA, m_ReceData);
	DDX_Text(pDX, IDC_PIPENUM, m_PipeNum);
	DDV_MinMaxUInt(pDX, m_PipeNum, 0, 7);
	DDX_Text(pDX, IDC_VALUE, m_Value);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSkyDlg, CDialog)
	//{{AFX_MSG_MAP(CSkyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(IDC_RESET, OnReset)
	ON_BN_CLICKED(IDC_HOLD, OnHold)
	ON_BN_CLICKED(IDC_LOAD, OnLoad)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_RECE, OnRece)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSkyDlg message handlers

BOOL CSkyDlg::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
	hUSB = NULL;

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

void CSkyDlg::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 CSkyDlg::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 CSkyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void CSkyDlg::OnOpen() 
{
	hUSB = CreateFile("\\\\.\\ezusb-0",
		GENERIC_WRITE,
		FILE_SHARE_WRITE,
		NULL,
		OPEN_EXISTING,
		0,
		NULL);
	if(hUSB==INVALID_HANDLE_VALUE)
	{
		MessageBox("Can't open USB device");
		return;
	}
	ULONG   nBytes = 0;
	SET_INTERFACE_IN	interfaceIn;
	interfaceIn.interfaceNum = 0;
	interfaceIn.alternateSetting =1 ;
	BOOL success = DeviceIoControl(hUSB,IOCTL_Ezusb_SETINTERFACE,
		&interfaceIn,
		sizeof(SET_INTERFACE_IN),
		NULL,0,&nBytes,NULL);
	if(!success)
	{
		MessageBox("Can't set device");
		CloseHandle(hUSB);
	}
}
void CSkyDlg::OnReset() 
{
	BOOL bResult;
	ULONG   nBytes = 0;
	VENDOR_REQUEST_IN	myRequest;
    myRequest.bRequest = 0xA0;
    myRequest.wValue = 0x7F92;
    myRequest.wIndex = 0x00;
    myRequest.wLength = 0x01;
    myRequest.bData = 0;		//reset
    myRequest.direction = 0x00;  
    if (hUSB != NULL)
    {     
         bResult = DeviceIoControl (hUSB,
                   IOCTL_Ezusb_VENDOR_REQUEST,
                   &myRequest,
                   sizeof(VENDOR_REQUEST_IN),
                   NULL,
                   0,
                   &nBytes,
                   NULL);
     
    }/* if valid driver handle */ 
	else
		return;
    if (bResult!=TRUE)
			MessageBox("Can't reset 8051");
}

void CSkyDlg::OnHold() 
{
	BOOL bResult;
	ULONG     nBytes = 0;
	VENDOR_REQUEST_IN	myRequest;
    myRequest.bRequest = 0xA0;
    myRequest.wValue = 0x7F92;
    myRequest.wIndex = 0x00;
    myRequest.wLength = 0x01;
    myRequest.bData = 1;		//Hold
    myRequest.direction = 0x00;  
    if (hUSB != NULL)
    {     
         bResult = DeviceIoControl (hUSB,
                   IOCTL_Ezusb_VENDOR_REQUEST,
                   &myRequest,
                   sizeof(VENDOR_REQUEST_IN),
                   NULL,
                   0,
                   &nBytes,
                   NULL);
     
    }/* if valid driver handle */  
	else
		return;
    if (bResult!=TRUE)
			MessageBox("Can't Hold 8051");
}
void CSkyDlg::OnLoad() 
{
	///////////
	FILE * fp;
	CFile file;
	CFileException fe;
	CString pathName;
	CFileDialog dlg(TRUE);
	dlg.m_ofn.lpstrTitle = "请选择您的资料文件";
	dlg.m_ofn.lpstrFilter = "BIN (*.bin)\0*.bin\0";    
	if(dlg.DoModal()!=IDOK)
		return;
	pathName = dlg.GetPathName();
	/////////////////////	
	if (!file.Open(pathName, CFile::modeRead | CFile::shareExclusive, &fe))
	{
		AfxMessageBox("对不起!!!\r\n\r\n文件出错\r\n 请重新选择文件!!!",MB_ICONSTOP);
		file.Close();
		return;
	}
	file.Close();
	///////////
	#define MAX_FILE_SIZE (1024*7)
    BYTE buffer[MAX_FILE_SIZE];
    int numread = 0;
    if ((fp = fopen(pathName,"rb")) != NULL)
    {
       numread = fread(buffer,sizeof(BYTE),MAX_FILE_SIZE,fp);    
	   fclose(fp);
    }  
	OnHold();
    BOOL bResult;
	ULONG nBytes = 0;
	ANCHOR_DOWNLOAD_CONTROL downloadControl;
	downloadControl.Offset = 0x0;
    if (hUSB != NULL) {   	
		bResult = DeviceIoControl (hUSB,
					IOCTL_EZUSB_ANCHOR_DOWNLOAD,
					&downloadControl,
					sizeof(ANCHOR_DOWNLOAD_CONTROL),
					buffer,
					numread,
					(unsigned long *)&nBytes,
					NULL);							
     }
	else
		return;	
	OnReset();
}
void CSkyDlg::SendData(CByteArray& data, ULONG pipename)
{
	char buffer[64];
	for(int i=0;i<64;i++)
		buffer[i]=data.GetAt(i);
	BULK_TRANSFER_CONTROL bulkControl;    
    ULONG length = data.GetSize();
    bulkControl.pipeNum = pipename;    
	ULONG nBytes = 0;
	BOOL bResult;
    if (hUSB != NULL)
    {		
		bResult = DeviceIoControl (hUSB,
			IOCTL_EZUSB_BULK_WRITE, 
			&bulkControl,
			sizeof (BULK_TRANSFER_CONTROL),
			buffer,
			length,
			(unsigned long *)&nBytes,
			NULL);				
	}
    if (bResult != TRUE)
		MessageBox("Can't send data");	
}

void CSkyDlg::OnSend() 
{
	UpdateData(TRUE);
	CByteArray data;
	data.SetSize(64);
	for(int i=0;i<64;i++)
		data.SetAt(i,m_Value);	
	SendData(data,m_PipeNum);

}
void CSkyDlg::OnRece() 
{
	if(hUSB==NULL)
		return;
	int i=0;
	char buffer[64];	
	for(i=0;i<64;i++)
		buffer[i]=0;
	BULK_TRANSFER_CONTROL bulkControl;    
    WORD length = 64;
    bulkControl.pipeNum = 1;    
	ULONG nBytes = 0;
	BOOL bResult;
    bResult = DeviceIoControl (hUSB,
		IOCTL_EZUSB_BULK_READ, 
		&bulkControl,
		sizeof (BULK_TRANSFER_CONTROL),
		&buffer[0],
		length,
		&nBytes,
		NULL);	
	CString x = "";
	char c[10];
	for(i=0;i<length;i++)
	{
		sprintf(c,"%c ;",(char)buffer[i]);
		x += c;
		if(i%8==0)
			x+="\r\n";
	}	
	bResult = DeviceIoControl (hUSB,
				IOCTL_Ezusb_RESETPIPE,
				&bulkControl,
				sizeof(BULK_TRANSFER_CONTROL),
				NULL,
				0,
				(unsigned long *)&nBytes,
				NULL);	
	m_ReceData.SetWindowText(x);
	CDialog::UpdateData(TRUE);
}

⌨️ 快捷键说明

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