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

📄 usbdll.cpp

📁 USBDLL实用源代码.包括一些BULK IN OUT的.使用用.
💻 CPP
字号:
// usbdll.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "usbdll.h"
#include "cyapi.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//++++++++++++++++++++++++++++++++++++++++++++++++++++
CCyUSBDevice *USBDevice=new CCyUSBDevice();
CCyControlEndPoint *ept0=USBDevice->ControlEndPt;

unsigned char cUSBMode=1;
//++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CUsbdllApp

BEGIN_MESSAGE_MAP(CUsbdllApp, CWinApp)
	//{{AFX_MSG_MAP(CUsbdllApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUsbdllApp construction

CUsbdllApp::CUsbdllApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CUsbdllApp object

CUsbdllApp theApp;

//+++++++++++++++++++add my function start++++++++++++++++++
int TestUSBDevice() 
{
	// TODO: Add your control notification handler code here
	CString s1,s2;
	s1="USB online,thanks for your using!";
	s2="USB offline,Please turn off it and link it again!";
//	unsigned char buf[2];
	// TODO: Add your control notification handler code here
//------------reset usb device proc-----------------           
	if(USBDevice ->DeviceCount() && !USBDevice->Open(0))
	{
		USBDevice ->Reset();
		Sleep(500);
		USBDevice ->Open(0);
	}
	if(!USBDevice ->IsOpen())
	{
		AfxMessageBox(s2);
		return -1;
	}
//------------reset usb device end-----------------           
//----------------Test USB vid&pid ------------------
	unsigned long n=0;
	ept0=USBDevice->ControlEndPt;
	int  devices= USBDevice->DeviceCount();
	int  vID,pID;
	int  d=0;
	while(1)
	{
		USBDevice->Open(d); // open automatically calls close() if necessary
		vID=USBDevice->VendorID;
		pID=USBDevice->ProductID;
		d++;
		n++;
		if((d>devices)||(n>60000)||((vID==0X04B4)&&(pID==0X8613)))break;
	}
	if((vID==0X04B4)&&(pID==0X8613))
	{
		AfxMessageBox(s1);
		return 0;
	}
	else 
	{
		AfxMessageBox(s2);
		return 1;
	}
//-----------------Test USB VID&PID End------------------	
//	//delete USBDevice;
}

int SingleRead(long len,unsigned char *buff) 
{
	// TODO: Add your control notification handler code here
	long i,buflen=0;
	unsigned char buf[5];
	for(i=0;i<len;i+=2)
	{
		ept0 ->Target =TGT_DEVICE;
		ept0 ->ReqType =REQ_VENDOR;
		ept0 ->Direction =DIR_FROM_DEVICE;
		ept0 ->ReqCode =0xDC;
		ept0 ->Value =0;   //set address word
		ept0 ->Index =0;
		buflen=4;
		ept0->XferData(buf,buflen);  //XferData(buf,buflen)
		buff[i]=buf[1];
		buff[i+1]=buf[2];
	}
	return 0;
}

int BlukWrite(long len,unsigned char *buff)
{
	long buflen=0;
	buflen=len;
	USBDevice->EndPoints[5]->XferData(buff,buflen);  //XferData(buf,buflen)
	return 0;
}
int BlukRead(long len,unsigned char *buff)
{
	long buflen=0;
	ept0 ->Target =TGT_DEVICE;
	ept0 ->ReqType =REQ_VENDOR;
	ept0 ->Direction =DIR_TO_DEVICE;
	ept0 ->ReqCode =0xE0;
	ept0 ->Value =0;   //set address word
	if(cUSBMode==2)ept0 ->Index =512;
	else ept0 ->Index =64;
	buflen=len;
	USBDevice->EndPoints[5]->XferData(buff,buflen);  //XferData(buf,buflen)
	return 0;
}

int TestUSBMode() 
{
	// TODO: Add your control notification handler code here
	int tt=USBDevice->EndPoints[5]->MaxPktSize;
	if(tt>64) 
	{
		cUSBMode=2;
		return 2;
	}
	if(tt==64)
	{
		cUSBMode=1;
		return 1;
	}
	else return -1;

}
//+++++++++++++++++++add my function end++++++++++++++++++++

⌨️ 快捷键说明

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