www.pudn.com > CYDEMO.zip > CYDEMODlg.cpp, change:2012-06-04,size:98571b


// CYDEMODlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "CYDEMO.h" 
#include "CYDEMODlg.h" 
 
#include <process.h> 
#include <winioctl.h> 
#include <assert.h> 
#include "ezusbsys.h" 
#include "myezusb.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
BOOL Sleep_us(DWORD dwInterval) 
{ 
	BOOL bNormal = TRUE;  
	LARGE_INTEGER frequence, privious, current, interval;  
	 
	if(!QueryPerformanceFrequency( &frequence))  
	{  
		//		::MessageBox(NULL, "Your computer hardware doesn’t support the high-resolution performance counter",  
		//		"Not Support", MB_ICONEXCLAMATION | MB_OK); //或其它的提示信息  
		return FALSE;  
	}  
	 
	interval.QuadPart = frequence.QuadPart * dwInterval / 1000000;  
	 
	bNormal = bNormal && QueryPerformanceCounter( &privious );  
	current = privious;  
	 
	while( current.QuadPart - privious.QuadPart < interval.QuadPart )  
		bNormal = bNormal && QueryPerformanceCounter( ¤t );  
	 
	return bNormal;  
} 
 
 
BOOL bOpenDriver (HANDLE * phDeviceHandle, PCHAR devname) 
{ 
   char completeDeviceName[64] = ""; 
   char pcMsg[64] = ""; 
    
   strcat (completeDeviceName, 
      "\\\\.\\" 
      ); 
    
   strcat (completeDeviceName, 
		    devname); 
    
   *phDeviceHandle = CreateFile(  completeDeviceName, 
      GENERIC_WRITE, 
      FILE_SHARE_WRITE, 
      NULL, 
      OPEN_EXISTING, 
      0,//串口同步传输I/O 
      NULL); 
    
   if (*phDeviceHandle == INVALID_HANDLE_VALUE) { 
      return (FALSE); 
   } else { 
      return (TRUE); 
   }  
} 
 
UINT  TransferThread( 
   void * pParam 
) 
{ 
    PTHREAD_CONTROL threadControl; 
	threadControl=(PTHREAD_CONTROL)pParam; 
	    
 
   // perform the ISO transfer 
   threadControl->status = DeviceIoControl (threadControl->hDevice, 
                                             threadControl->Ioctl, 
                                             threadControl->InBuffer, 
                                             threadControl->InBufferSize, 
                                             threadControl->OutBuffer, 
                                             threadControl->OutBufferSize, 
                                             &threadControl->BytesReturned, 
                                             NULL); 
 
   // if an event exists, set it 
   if (threadControl->completionEvent) 
      SetEvent(threadControl->completionEvent);   //设置事件为有信号状态 
   return 0; 
} 
 
 
UINT TestThread( 
  void * pParam  // thread data 
) 
{  
    CThreadParam*  ThreadParam; 
	CCYDEMODlg* TestDlg; 
 
    HANDLE  hInDevice = NULL; 
    HANDLE  hOutDevice = NULL; 
    PUCHAR inBuffer = NULL; 
    PUCHAR outBuffer = NULL; 
    DWORD MaxTransferSize, CurrentTransferSize; 
    ULONG InPipeNum; 
    ULONG OutPipeNum; 
    DWORD TestPass; 
    DWORD Errors; 
    HANDLE ReadCompleteEvent; 
    HANDLE WriteCompleteEvent; 
 
    ULONG  i; 
    BULK_TRANSFER_CONTROL   inBulkControl,outBulkControl;// 其实就是pipenum 
    THREAD_CONTROL inThreadControl,outThreadControl;//线程句柄,设备号,输入输出端口及请求ioct1,completionevent   status   
 
    char tempbuff[256]; 
    FILE *dumpfile;//保存测试数据 
    FILE *binfile; 
 
	USBDATA Data2A;//databuffer(1024)(512)结构体 
//	USBDATA_CONTROL pData2A; 
	for(Data2A.pDatagrup=0;Data2A.pDatagrup<1024;Data2A.pDatagrup++) 
	{ 
		for (Data2A.pDatabuf=0;Data2A.pDatabuf<512;Data2A.pDatabuf++) 
		{ 
			Data2A.Databuf[Data2A.pDatagrup][Data2A.pDatabuf] = 0x55; 
		} 
	} 
	Data2A.rdDataen = FALSE; 
	Data2A.wrDataen = TRUE; 
 
	ThreadParam=(CThreadParam *)pParam;//将void *转换为CThreadParam * 
	TestDlg=ThreadParam->testDlg; 
 
    if (bOpenDriver (&hOutDevice, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE) 
    { 
		TestDlg->m_strTestDisplay+="打开设备失败!\r\n"; 
		ThreadParam->bUpdate=TRUE; 
		return 0; 
    } 
 
    if (bOpenDriver (&hInDevice, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE) 
    { 
       TestDlg->m_strTestDisplay +="打开设备失败,测试中断!\r\n"; 
	   ThreadParam->bUpdate=TRUE; 
       return 0; 
    } 
 
	OutPipeNum=2; 
    InPipeNum=3; 
 
    sprintf(tempbuff, 
		"------------多线程测试-------\r\n输入端口:%d\r\n输出端口:%d", 
		InPipeNum,OutPipeNum); 
	ThreadParam->bUpdate=TRUE; 
	TestDlg->m_strTestDisplay+=tempbuff; 
   
    // 
    // get the transfer size and allocate the transfer buffers 
    // 
    MaxTransferSize = TestDlg->m_TestBytes; 
    CurrentTransferSize = MaxTransferSize; 
    outBuffer = (unsigned char *)malloc(MaxTransferSize); 
//	for(i=0;i<MaxTransferSize;i++) 
//	outBuffer[i]=; 
    inBuffer  = (unsigned char *)malloc(MaxTransferSize);  
    // 
    // seed the random number generator, and set our starting pattern 
    // 
    srand(TestDlg->m_TestBytes);//srand 函数设置一个随机开始点 
  
    // 
    // initialize the pass and error counters 
    // 
    TestPass=0; 
    Errors=0; 
 
     // 
    // Set up our event objects 
    // 
    WriteCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL); 
    ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);//返回一个句柄 
    // 
    // main test loop 
    //	 
    while (!StopTest)//全局变量来决定是否退出测试线程 
    { 
		// initialize the out buffer 
 
		for (i=0;i<CurrentTransferSize;i++) 
			   outBuffer[i] = rand(); 
		// initialize the in buffer 
		memset(inBuffer, 0, MaxTransferSize);//将字符串中前MaxTransferSize个字符设为0 
  
          // initialize data structures for the read thread 
		outBulkControl.pipeNum = OutPipeNum; 
		outThreadControl.hDevice = hOutDevice; 
		 
		outThreadControl.Ioctl = IOCTL_EZUSB_BULK_WRITE; 
		outThreadControl.InBuffer = (PVOID)&outBulkControl;//将输出端口号赋给InBuffer 
		outThreadControl.InBufferSize = sizeof(BULK_TRANSFER_CONTROL); 
		outThreadControl.OutBuffer = outBuffer; 
		outThreadControl.OutBufferSize = CurrentTransferSize; 
		 
		outThreadControl.completionEvent = WriteCompleteEvent;//CreatEvent 函数返回的句柄 
		outThreadControl.status = FALSE; 
		outThreadControl.BytesReturned = 0;	 
 
		// initialize data structures for the write thread 
		inBulkControl.pipeNum = InPipeNum; 
		inThreadControl.hDevice = hInDevice; 
		 
		inThreadControl.Ioctl = IOCTL_EZUSB_BULK_READ; 
		inThreadControl.InBuffer = (PVOID)&inBulkControl; 
		inThreadControl.InBufferSize = sizeof(BULK_TRANSFER_CONTROL); 
		inThreadControl.OutBuffer = inBuffer; 
		inThreadControl.OutBufferSize = CurrentTransferSize; 
		 
		inThreadControl.completionEvent = ReadCompleteEvent; 
		inThreadControl.status = FALSE; 
		inThreadControl.BytesReturned = 0; 
 
		// start and wait for transfer threads 
		CWinThread * wt = AfxBeginThread(  
			TransferThread, // thread function  
			&outThreadControl); // argument to thread function,creat a worker Thread,param is a class,this class is tread control 
		outThreadControl.hThread = wt->m_hThread;//将创建的线程对象句柄赋值给Thread-control 类的一个对象。 
 
 
		CWinThread * rd = AfxBeginThread(  
			TransferThread, // thread function  
			&inThreadControl); // argument to thread function  
		inThreadControl.hThread = rd->m_hThread; 
 
		WaitForSingleObject(WriteCompleteEvent,INFINITE);//ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);//返回一个句柄 
 
		WaitForSingleObject(ReadCompleteEvent,INFINITE); 
 
		// if either the read or write failed, we want to stop the test 
		if (!inThreadControl.status) 
		{ 
			TestDlg->m_strTestDisplay+= "Error: Read failed\r\n"; 
 
			TestDlg->m_strTestDisplay+="Dumping OUT packet to file\r\n"; 
			dumpfile = fopen("dumpfile.txt","w"); 
			for (i=0;i<CurrentTransferSize;i++) 
			 fprintf(dumpfile,"%X\n",outBuffer[i]); 
			fclose(dumpfile); 
 
			binfile = fopen("binfile.bix","wb"); 
			fwrite(outBuffer,sizeof(BYTE),CurrentTransferSize,binfile); 
			fclose(binfile); 
			StopTest = TRUE; 
		} 
		if( !outThreadControl.status) 
		{ 
			TestDlg->m_strTestDisplay+= "Error: Write failed\r\n"; 
			StopTest = TRUE; 
		} 
 
		// verify the data, unless the user has disabled verify data 
		// or this was a write only test. 
		// make sure the correct amount of data was returned 
		if (inThreadControl.BytesReturned != CurrentTransferSize) 
		{ 
			Errors++; 
		} 
		// copmpare the buffers 
		for (i=0;i<CurrentTransferSize;i++) 
		{ 
			if (inBuffer[i] != outBuffer[i]) 
			{ 
				sprintf(tempbuff, 
					 "Error: Data offset 0x%x expected = 0x%x actual = 0x%x\r\n", 
					 i, 
					 outBuffer[i], 
					 inBuffer[i]); 
				TestDlg->m_strTestDisplay+=tempbuff; 
				Errors++; 
 
		//       StopTest = TRUE; 
			} 
		} 
       TestPass++; 
	   CString strTestPass=""; 
	   strTestPass.Format("-------------正在进行多线程测试--------------\r\n测试通过:  %ld\r\n测试失败:  %ld", 
		   TestPass,Errors);//格式化 
	   TestDlg->m_strTestDisplay=strTestPass; 
	   ThreadParam->bUpdate=TRUE; 
	}//end while(gstoptest) 
    free(inBuffer); 
    free(outBuffer); 
    CloseHandle(hInDevice); 
    CloseHandle(hOutDevice); 
    ThreadParam->bUpdate=TRUE; 
	return 0; 
} 
 
 
 
/*UINT  ReadTransferThread( 
   void * pParam 
) 
{ 
	CThreadParam*  Threadparam; 
	CCYDEMODlg* TestDlg; 
    PTHREAD_CONTROL threadControl; 
	threadControl=(PTHREAD_CONTROL)pParam; 
	    
 
   // perform the ISO transfer 
   threadControl->status = DeviceIoControl (threadControl->hDevice, 
                                             threadControl->Ioctl, 
                                             threadControl->InBuffer, 
                                             threadControl->InBufferSize, 
                                             threadControl->OutBuffer, 
                                             threadControl->OutBufferSize, 
                                             &threadControl->BytesReturned, 
                                             NULL); 
 
   // if an event exists, set it 
   if (threadControl->completionEvent) 
      SetEvent(threadControl->completionEvent);   //设置事件为有信号状态 
   return 0; 
}*/ 
 
 
UINT SpeedTestThread( 
  void * pParam  // thread data 
) 
{  
    CThreadParam*  ThreadParam; 
	CCYDEMODlg* TestDlg; 
//pipe 6 
    BULK_TRANSFER_CONTROL outBulkControl6;//pipenum 
    THREAD_CONTROL outThreadControl6;//线程句柄,设备号,输入输出端口及请求ioct1,completionevent   status   
	ULONG OutPipeNum6; 
    HANDLE hOutDevice6 = NULL; 
    PUCHAR outBuffer6 = NULL; 
    DWORD CurrentTransferSize6; 
    HANDLE WriteCompleteEvent6; 
 
	DWORD MaxTransferSize = 512; 
    DWORD TestPass; 
    DWORD Errors; 
	DWORD m_SpeedCount;//局部变量 
	DWORD m_TestSpeedStart,m_TestSpeedStop; 
	ULONG  i; 
	ULONG framecount = 0; 
//	char tempbuff[256]; 
//	FILE *dumpfile;//保存测试数据 
//	FILE *binfile; 
 
	ThreadParam=(CThreadParam *)pParam; 
	TestDlg=ThreadParam->testDlg; 
 
    if (bOpenDriver (&hOutDevice6, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE) 
    { 
		TestDlg->m_strTestDisplay+="打开设备失败!\r\n"; 
		ThreadParam->bUpdate=TRUE; 
		return 0; 
    } 
 
	OutPipeNum6=2;                                                       //2=ep6????????? 
    // get the transfer size and allocate the transfer buffers 
	CurrentTransferSize6 = MaxTransferSize; 
	outBuffer6 = (unsigned char *)malloc(MaxTransferSize); 
    // seed the random number generator, and set our starting pattern 
    TestPass=0; 
    Errors=0; 
 
	m_SpeedCount = 0;// 
    // Set up our event objects 
	WriteCompleteEvent6 = CreateEvent(0,FALSE,FALSE,NULL); 
    // main test loop 
	//PIPE 6  
	outBulkControl6.pipeNum = OutPipeNum6; 
	outThreadControl6.hDevice = hOutDevice6; 
		   
	outThreadControl6.Ioctl = IOCTL_EZUSB_BULK_WRITE; 
	outThreadControl6.InBuffer = (PVOID)&outBulkControl6;//pipenum 
	outThreadControl6.InBufferSize = sizeof(BULK_TRANSFER_CONTROL); 
	outThreadControl6.OutBuffer = outBuffer6; 
	outThreadControl6.OutBufferSize = CurrentTransferSize6; 
		   
	outThreadControl6.completionEvent = WriteCompleteEvent6; 
	outThreadControl6.status = FALSE; 
	outThreadControl6.BytesReturned = 0; 
	m_TestSpeedStart = GetTickCount();//获得开始时刻 milliseconds 
 
    while (!StopSpeedTest)//全局变量来决定是否退出测试线程 
    { 
		framecount++;//框架循环 
		for (i=0;i<MaxTransferSize;i++)//MaxTransferSize=512 
		{ 
			outBuffer6[i] = usbdata[pusbdataframe[0]][pusbdataline[0]][pusbdatapixel[0]];//usbdata(20)(1024)(512) 全局变量 
		} 
//PIPE 6 
		  CWinThread * wt6 = AfxBeginThread(  
			  TransferThread, // thread function ? 
			  &outThreadControl6); // argument to thread function 传递的参数为一个thread control类对象  
		  outThreadControl6.hThread = wt6->m_hThread;//将线程句柄赋值给threadcontrol;类对象的线程句柄 
		  WaitForSingleObject(WriteCompleteEvent6,INFINITE); 
		   // if either the read or write failed, we want to stop the test 
		  if( !outThreadControl6.status) 
		  { 
			  TestDlg->m_strTestDisplay+= "Error: PIPE 6 Write failed !\r\n"; 
			  StopSpeedTest = TRUE; 
		  } 
		  //如果有错误发生 
		  if (Errors && (StopTest == TRUE)) 
		  { 
			 TestDlg->m_strTestDisplay+="Dumping Error packet to file\r\n"; 
			 ThreadParam->bUpdate=TRUE; 
		  } 
	}//end while(gstoptest) 
	m_TestSpeedStop = GetTickCount();// 获得终止时刻 
 
	DWORD TestTime = m_TestSpeedStop-m_TestSpeedStart; 
	DWORD TestNum = framecount * MaxTransferSize; 
	double TestSpeedval = (TestNum/(TestTime*0.001))/1024.0;// 1024是1k 
	CString strSpeedval=""; 
	strSpeedval.Format("\r\n Test Speed Is : %lf KBytes/s",TestSpeedval); 
	TestDlg->m_strTestDisplay = strSpeedval; 
 
	free(outBuffer6); 
	CloseHandle(hOutDevice6); 
    ThreadParam->bUpdate=TRUE; 
	return 0; 
} 
 
 
UINT BulkTransThread( 
  void * pParam  // thread data 
) 
{  
    CThreadParam*  ThreadParam; 
	CCYDEMODlg* TestDlg; 
//pipe 6 
    BULK_TRANSFER_CONTROL outBulkControl2; 
    THREAD_CONTROL outThreadControl2; 
	ULONG OutPipeNum2; 
    HANDLE hOutDevice2 = NULL; 
    PUCHAR outBuffer2 = NULL; 
    DWORD CurrentTransferSize2; 
    HANDLE WriteCompleteEvent2; 
 
	DWORD MaxTransferSize = 1024;          // 1024? 
    DWORD TestPass; 
    DWORD Errors; 
	DWORD m_SpeedCount; 
	DWORD m_TestSpeedStart,m_TestSpeedStop; 
	ULONG  i; 
	char tempbuff[256];                       //256? 
//	FILE *dumpfile;//保存测试数据 
//	FILE *binfile; 
 
	ThreadParam=(CThreadParam *)pParam; 
	TestDlg=ThreadParam->testDlg; 
 
    if (bOpenDriver (&hOutDevice2, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE) 
    { 
       TestDlg->m_strTestDisplay+="\r\n打开设备失败!\r\n"; 
	   ThreadParam->bUpdate=TRUE; 
       return 0; 
    } 
 
	OutPipeNum2=2;                                //2? 
 
    sprintf(tempbuff,"\r\n----------块传输例程启动----------"); 
	ThreadParam->bUpdate=TRUE; 
	TestDlg->m_strTestDisplay+=tempbuff; 
   
    // 
    // get the transfer size and allocate the transfer buffers 
    // 
//    MaxTransferSize = TestDlg->m_TestBytes; 
    CurrentTransferSize2 = MaxTransferSize; 
    outBuffer2 = (unsigned char *)malloc(MaxTransferSize); 
    // 
    // seed the random number generator, and set our starting pattern 
    // 
//	srand(TestDlg->m_TestBytes);  
    // 
    // initialize the pass and error counters 
    // 
    TestPass=0; 
    Errors=0; 
	m_SpeedCount = 0; 
    WriteCompleteEvent2 = CreateEvent(0,FALSE,FALSE,NULL);  //return a handle 
    // 
    // main test loop 
    // 
	//PIPE 2  
	outBulkControl2.pipeNum = OutPipeNum2; 
	outThreadControl2.hDevice = hOutDevice2; 
	 
	outThreadControl2.Ioctl = IOCTL_EZUSB_BULK_WRITE; 
	outThreadControl2.InBuffer = (PVOID)&outBulkControl2; 
	outThreadControl2.InBufferSize = sizeof(BULK_TRANSFER_CONTROL); 
	outThreadControl2.OutBuffer = outBuffer2; 
	outThreadControl2.OutBufferSize = CurrentTransferSize2; 
	 
	outThreadControl2.completionEvent = WriteCompleteEvent2; 
	outThreadControl2.status = FALSE; 
	outThreadControl2.BytesReturned = 0; 
 
	m_TestSpeedStart = GetTickCount(); 
	for (int j=0;j<1024;j++) 
	{ 
 
 
 
		for (i=0;i<MaxTransferSize;i++) 
		{ 
			outBuffer2[i] = rand();//随机获得数据 
		} 
		 
		CWinThread * wt2 = AfxBeginThread(  
			TransferThread, // thread function  
			&outThreadControl2); // argument to thread function  
		outThreadControl2.hThread = wt2->m_hThread; 
		 
		WaitForSingleObject(WriteCompleteEvent2,INFINITE); 
		 
		// if either the read or write failed, we want to stop the test 
		if( !outThreadControl2.status) 
		{ 
			TestDlg->m_strTestDisplay+= "Error: PIPE 2 Write failed !\r\n"; 
			StopSpeedTest = TRUE; 
		} 
		// verify the data, unless the user has disabled verify data 
		// or this was a write only test. 
		// make sure the correct amount of data was returned 
		// copmpare the buffers 
		 
		//如果有错误发生 
		if (Errors && (StopTest == TRUE)) 
		{ 
			TestDlg->m_strTestDisplay+="\r\nDumping Error packet to file\r\n"; 
		} 
		m_TestSpeedStop = GetTickCount(); 
		CString strtestcount = ""; 
		strtestcount.Format("\r\n Testtmcount=%d,Systime=%ld",j,m_TestSpeedStop); 
		TestDlg->m_strTestDisplay += strtestcount; 
 
 
 
 
 
	} 
//	m_TestSpeedStop = GetTickCount(); 
 
	DWORD TestTime = m_TestSpeedStop-m_TestSpeedStart; 
	DWORD TestNum = MaxTransferSize*1024; 
	double TestSpeedval = (TestNum/(TestTime*0.001))/1024.0;///////////////why? 
	CString strSpeedval=""; 
	strSpeedval.Format("\r\n----------块传输例程结束----------\r\n传输速度为:%lf KBytes/Sec", 
		TestSpeedval); 
	TestDlg->m_strTestDisplay += strSpeedval; 
  
    free(outBuffer2); 
    CloseHandle(hOutDevice2); 
    ThreadParam->bUpdate=TRUE; 
	return 0; 
} 
 
UINT IsoTestThread( 
	void * pParam  // thread data 
) 
{  
    CThreadParam*  ThreadParam; 
	CCYDEMODlg* TestDlg; 
//pipe 2 
//	BULK_TRANSFER_CONTROL outBulkControl2; 
    ISO_TRANSFER_CONTROL outBulkControl2; 
    THREAD_CONTROL outThreadControl2; 
	ULONG OutPipeNum2; 
    HANDLE hOutDevice2 = NULL; 
    PUCHAR outBuffer2 = NULL; 
    DWORD CurrentTransferSize2; 
    HANDLE WriteCompleteEvent2; 
 
	DWORD MaxTransferSize = 512; 
    DWORD TestPass; 
    DWORD Errors; 
	DWORD m_SpeedCount; 
	DWORD m_TestSpeedStart,m_TestSpeedStop; 
	ULONG  i; 
    char tempbuff[256]; 
//	FILE *dumpfile;//保存测试数据 
//	FILE *binfile; 
 
	ThreadParam=(CThreadParam *)pParam; 
	TestDlg=ThreadParam->testDlg; 
 
    if (bOpenDriver (&hOutDevice2, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE) 
    { 
       TestDlg->m_strTestDisplay+="打开设备失败!\r\n"; 
	   ThreadParam->bUpdate=TRUE; 
       return 0; 
    } 
  
	OutPipeNum2=0;//选择EP2 
 
    sprintf(tempbuff,"\r\nout_PIPE_2=%d::\r\n",OutPipeNum2); 
	ThreadParam->bUpdate=TRUE; 
	TestDlg->m_strTestDisplay+=tempbuff; 
   
    // 
    // get the transfer size and allocate the transfer buffers 
    // 
//    MaxTransferSize = TestDlg->m_TestBytes; 
    CurrentTransferSize2 = MaxTransferSize; 
    outBuffer2 = (unsigned char *)malloc(MaxTransferSize); 
 
    // 
    // seed the random number generator, and set our starting pattern 
    // 
//	srand(TestDlg->m_TestBytes); 
  
    // 
    // initialize the pass and error counters 
    // 
    TestPass=0; 
    Errors=0; 
 
	m_SpeedCount = 0; 
 
     // 
    // Set up our event objects 
    // 
    WriteCompleteEvent2 = CreateEvent(0,FALSE,FALSE,NULL); 
    // 
    // main test loop 
    // 
/* 
IsoControl.PipeNum = pTh->pipeNum; 
IsoControl.PacketSize = pTh->length; 
IsoControl.BufferCount = pTh->bufCount; 
IsoControl.FramesPerBuffer = pTh->pakPer; 
IsoControl.PacketCount = pTh->packets; 
*/ 
 
	//PIPE 2  
	outBulkControl2.PipeNum = OutPipeNum2;    ////////outBulkControl is a ISO_TRANSFER_CONTROL  object 
	outBulkControl2.PacketSize = 512; 
	outBulkControl2.BufferCount = 2;              ////////每两个字符组为一行???????? 
	outBulkControl2.FramesPerBuffer = 8;          ///////每8个字符为一组????????? 
	outBulkControl2.PacketCount = 128;///////?????????         512/2 
 
//	outBulkControl2.pipeNum = OutPipeNum2;	 
	outThreadControl2.hDevice = hOutDevice2; 
	 
	outThreadControl2.Ioctl = IOCTL_EZUSB_ISO_WRITE; 
	outThreadControl2.InBuffer = (PVOID)&outBulkControl2; 
	outThreadControl2.InBufferSize = sizeof(ISO_TRANSFER_CONTROL); 
	outThreadControl2.OutBuffer = outBuffer2; 
	outThreadControl2.OutBufferSize = CurrentTransferSize2; 
	 
	outThreadControl2.completionEvent = WriteCompleteEvent2; 
	outThreadControl2.status = FALSE; 
	outThreadControl2.BytesReturned = 0; 
     
	m_TestSpeedStart = GetTickCount(); 
 
    while (!StopIsoTest)//全局变量来决定是否退出测试线程 
    { 
/* 
 
		if (TestDlg->m_ctrlTestDisplay.GetLineCount()>100) 
		{ 
			TestDlg->m_strTestDisplay.Empty(); 
		} 
*/ 
//		Sleep(100); 
  
		   // initialize the out buffer		 
		if (pusbdataline[0]<1024) 
		{ 
			pusbdataline[0]++;			 
		}  
		else 
		{ 
			pusbdataline[0]=0;	 
			 
			if (pusbdataframe[0]<15) 
			{ 
				pusbdataframe[0]++; 
 
				m_SpeedCount++; 
				CString strSpeed = ""; 
				strSpeed.Format(":%ld",m_SpeedCount); 
//				TestDlg->m_strSpeedCount = strSpeed; 
			}  
			else 
			{ 
/* 
				CString strSpeed = ""; 
				strSpeed.Format(":%ld",m_SpeedCount); 
				TestDlg->m_strSpeedCount = strSpeed; 
*/ 
//				pusbdataframe[0] = 0; 
				StopSpeedTest = TRUE; 
				break; 
			} 
		} 
		 
		pusbdatapixel[0] = 0; 
		for (i=0;i<MaxTransferSize;i++)//////MaxTransferSize = 512 
		{ 
			outBuffer2[i] = usbdata[pusbdataframe[0]][pusbdataline[0]][pusbdatapixel[0]]; 
//			pusbdatapixel[0]++;                                   ///////???????????????????????? 
		} 
 
       // start and wait for transfer threads 
//PIPE 2 
		  CWinThread * wt2 = AfxBeginThread(  
		  TransferThread, // thread function  
		  &outThreadControl2); // argument to thread function       IOCTL_EZUSB_ISO_WRITE; 
	      outThreadControl2.hThread = wt2->m_hThread; 
 
          WaitForSingleObject(WriteCompleteEvent2,INFINITE); 
 
		  // if either the read or write failed, we want to stop the test 
		  if( !outThreadControl2.status) 
		  { 
			  TestDlg->m_strTestDisplay+= "Error: PIPE 2 Write failed !\r\n"; 
			  StopIsoTest = TRUE; 
		  } 
 
		   // verify the data, unless the user has disabled verify data 
		   // or this was a write only test. 
		   // make sure the correct amount of data was returned 
		  // copmpare the buffers 
 
		  //如果有错误发生 
		  if (Errors && (StopIsoTest == TRUE)) 
		  { 
			 TestDlg->m_strTestDisplay+="Dumping Error packet to file\r\n"; 
		  } 
	   ThreadParam->bUpdate=TRUE; 
 
 
 
	}//end while(gstoptest) 
 
	m_TestSpeedStop = GetTickCount(); 
 
	DWORD TestTime = m_TestSpeedStop-m_TestSpeedStart; 
	DWORD TestNum = (pusbdataframe[0]*1024*512+(pusbdataline[0]+1)*512)*4;/////////////???????????????? 
	double TestSpeedval = ((TestNum*1000.0)/TestTime)/1024.0; 
	CString strSpeedval=""; 
	strSpeedval.Format("\r\n Test Speed Is : %lf",TestSpeedval); 
	TestDlg->m_strTestDisplay = strSpeedval; 
 
    free(outBuffer2); 
    CloseHandle(hOutDevice2); 
    ThreadParam->bUpdate=TRUE; 
	return 0; 
} 
 
//UART RX 
UINT UsbToUartThread( 
					  void * pParam  // thread data 
					  ) 
{  
    CThreadParam*  ThreadParam; 
	CCYDEMODlg* TestDlg; 
  
	HANDLE  hDevice = NULL; 
    BOOLEAN bResult = FALSE; 
    int     nBytes = 0;	 
	VENDOR_OR_CLASS_REQUEST_CONTROL	myRequest;/////结构体    
	unsigned char *buffer; 
	unsigned char databuffer[256]; 
	ULONG bufferSize = 0; 
//	unsigned char RxBuffer; 
	unsigned char Rxflag; 
	 
	ThreadParam=(CThreadParam *)pParam; 
	TestDlg=ThreadParam->testDlg; 
 
    if (bOpenDriver (&hDevice, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE) 
    { 
		TestDlg->m_strTestDisplay+="打开设备失败!\r\n"; 
		ThreadParam->bUpdate=TRUE; 
		return 0; 
    } 
	else 
	{ 
		TestDlg->m_strTestDisplay="\r\n---------USB转串口启动----------\r\n"; 
		ThreadParam->bUpdate=TRUE; 
	} 
	 
////////////////////initialize request  param 
 
	myRequest.request = 0XB8;	//接收命令   0xB8???????? 
//	myRequest.request = 0XB9;	//UART0 
	 
	myRequest.value = (USHORT) 0x0000; 
	myRequest.index = (USHORT) 0xbeef; 
	myRequest.direction = 0x01;	//in            
	bufferSize = 0x10;                        ///////////16位 
	buffer = databuffer;                     ///////局部变量赋值给buffer	 
	myRequest.requestType=0x02; // vendor specific request type (2) 
	myRequest.recepient=0x00; // recepient is device (0) 
	 
	while(!StopUsbToUart) 
	{ 
		Sleep_us(125);////////////////??????????????? 
		//		Sleep(1); 
		if (hDevice != NULL)  
		{ 
			bResult = DeviceIoControl (hDevice, 
				IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST, 
				&myRequest, 
				sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL), 
				buffer, 
				bufferSize, 
				(unsigned long *)&nBytes, 
				NULL); 
		} 
		 
		if (bResult==TRUE) 
		{ 
			Rxflag = buffer[0];//??????????     0   ?// 
//			RxBuffer = buffer[1]; 
			if (Rxflag==0xaa)	//接收到的是数据 
			{ 
				uart_rxdata = buffer[1];/////??????????     1    ?// 
			}  
		} 
		else 
		{ 
			break; 
		} 
	} 
	CloseHandle(hDevice); 
	return 0; 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
// 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() 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CCYDEMODlg dialog 
 
CCYDEMODlg::CCYDEMODlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CCYDEMODlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CCYDEMODlg) 
	m_strName = _T("EZUSB-0");//设备名文本框文字 
	m_strTestDisplay = _T("");// 
	m_TestBytes = 65535;    //测试字节文本框 
	m_strFileName = _T("cydemo.bix");///////固件代码文件 
	m_UartRx = 0; 
	m_UartTx = 0; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);//AfxGetApp  返回一指向CWinApp对象的指针, 
    ///    LoadIcon  return  A handle to an icon if it is successful; otherwise, it is NULL                               
	////LoadIcon  一个图标 
} 
 
void CCYDEMODlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CCYDEMODlg) 
	DDX_Control(pDX, IDC_COMBO_BAUDRATE, m_ctrlBaudRate); 
	DDX_Control(pDX, IDC_BUTTON_UARTRX, m_ctrlUsbToUart); 
	DDX_Control(pDX, IDC_BUTTON_TESTIO, m_ctrlIoTest); 
	DDX_Control(pDX, IDC_BUTTON_RDSPEEDTEST, m_ctrlIsoTest); 
	DDX_Control(pDX, IDC_EDIT_TESTDIS, m_ctrlTestDisplay); 
	DDX_Control(pDX, IDC_BUTTON_WRSPEEDTEST, m_ctrlSpeedTest); 
	DDX_Control(pDX, IDC_BUTTON_THREADTEST, m_ctrlThreadTest); 
	DDX_Text(pDX, IDC_EDIT_STRNAME, m_strName); 
	DDX_Text(pDX, IDC_EDIT_TESTDIS, m_strTestDisplay); 
	DDX_Text(pDX, IDC_EDIT_TESTBYTE, m_TestBytes); 
	DDX_Text(pDX, IDC_STATIC_PROGRAMEPATH, m_strFileName); 
	DDX_Text(pDX, IDC_EDIT_UARTRX, m_UartRx); 
	DDX_Text(pDX, IDC_EDIT_UARTTX, m_UartTx); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CCYDEMODlg, CDialog) 
	//{{AFX_MSG_MAP(CCYDEMODlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON_THREADTEST, OnButtonThreadtest) 
	ON_WM_TIMER() 
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear) 
	ON_BN_CLICKED(IDC_BUTTON_WRSPEEDTEST, OnButtonWrSpeedtest) 
	ON_BN_CLICKED(IDC_BUTTON_RDSPEEDTEST, OnButtonRdSpeedtest) 
	ON_BN_CLICKED(IDC_BUTTON_PORT, OnButtonPort) 
	ON_BN_CLICKED(IDC_BUTTON_MCURESET, OnButtonMcureset) 
	ON_BN_CLICKED(IDC_BUTTON_MCULOCK, OnButtonMculock) 
	ON_BN_CLICKED(IDC_BUTTON_DOWNLOAD, OnButtonDownload) 
	ON_BN_CLICKED(IDC_BUTTON_FRAME, OnButtonFrame) 
	ON_BN_CLICKED(IDC_BUTTON_GETSTRING, OnButtonGetstring) 
	ON_BN_CLICKED(IDC_BUTTON_GETPORT, OnButtonGetport) 
	ON_BN_CLICKED(IDC_BUTTON_GETINTERFACE, OnButtonGetinterface) 
	ON_BN_CLICKED(IDC_BUTTON_GETCONFIG, OnButtonGetconfig) 
	ON_BN_CLICKED(IDC_BUTTON_GETDEVICE, OnButtonGetdevice) 
	ON_BN_CLICKED(IDC_BUTTON_OPENDEVICE, OnButtonOpendevice) 
	ON_BN_CLICKED(IDC_BUTTON_TESTIO, OnButtonTestio) 
	ON_BN_CLICKED(IDC_BUTTON_SELECTFILE, OnButtonSelectfile) 
	ON_BN_CLICKED(IDC_BUTTON_CLOSEPRO, OnButtonClosepro) 
	ON_BN_CLICKED(IDC_BUTTON_I2CTEST, OnButtonI2ctest) 
	ON_BN_CLICKED(IDC_BUTTON_CTRLTRANS, OnButtonCtrltrans) 
	ON_BN_CLICKED(IDC_BUTTON_INTTRANS, OnButtonInttrans) 
	ON_BN_CLICKED(IDC_BUTTON_ISOTRANS, OnButtonIsotrans) 
	ON_BN_CLICKED(IDC_BUTTON_BULKTRANS, OnButtonBulktrans) 
	ON_BN_CLICKED(IDC_BUTTON_FIFO8WR, OnButtonFifo8wr) 
	ON_BN_CLICKED(IDC_BUTTON_FIFO8RD, OnButtonFifo8rd) 
	ON_BN_CLICKED(IDC_BUTTON_FIFO16WR, OnButtonFifo16wr) 
	ON_BN_CLICKED(IDC_BUTTON_FIFO16RD, OnButtonFifo16rd) 
	ON_BN_CLICKED(IDC_BUTTON_GPIFSNGWR, OnButtonGpifsngwr) 
	ON_BN_CLICKED(IDC_BUTTON_GPIFSNGRD, OnButtonGpifsngrd) 
	ON_BN_CLICKED(IDC_BUTTON_GPIFFIFOWR, OnButtonGpiffifowr) 
	ON_BN_CLICKED(IDC_BUTTON_GPIFFIFORD, OnButtonGpiffiford) 
	ON_BN_CLICKED(IDC_BUTTON_UARTRX, OnButtonUartRx) 
	ON_BN_CLICKED(IDC_BUTTON_UARTTX, OnButtonUarttx) 
	ON_CBN_SELENDOK(IDC_COMBO_BAUDRATE, OnSelendokComboBaudrate) 
	ON_BN_CLICKED(IDC_BUTTON_SINGLESHOW, OnButtonSingleshow) 
	ON_BN_CLICKED(IDC_BUTTON_THANNELSELECT, OnButtonThannelselect) 
	ON_BN_CLICKED(IDC_BUTTON_DATADOWM, OnButtonDatadowm) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCYDEMODlg message handlers 
 
BOOL CCYDEMODlg::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 
/* 
	CBitmap Bitmap; 
	Bitmap.LoadBitmap(IDB_BITMAP1); 
	HBITMAP hBitmap = (HBITMAP)Bitmap.Detach(); 
	CButton *pButton = (CButton*)GetDlgItem(IDC_BUTTON_CLOSEPRO); 
	pButton->SetBitmap(hBitmap); 
*/ 
	char PathName[100]; 
	//	::GetModuleFileName(NULL,PathName,100);	//获取当前应用程序位置 
	::GetCurrentDirectory(100,PathName); 
	 
	m_strFileName.Format("%s",PathName); 
 
	m_strName = _T("EZUSB-0"); 
	m_strTestDisplay = _T(""); 
	m_TestBytes = 65535; 
 
	for (int i=0;i<20;i++) 
	{ 
		for (int j=0;j<1024;j++) 
		{ 
			for (int k=0;k<512;k++) 
			{ 
				usbdata[i][j][k] = 0xaa; 
			} 
		} 
	} 
 
	for (int h=0;h<5;h++) 
	{ 
		pusbdataframe[h] = 0; 
		pusbdataline[h] = 0; 
		pusbdatapixel[h] = 0; 
		rdusbdataen[h] = FALSE; 
		wrusbdataen[h] = TRUE; 
		usbdatabusy[h] = FALSE; 
	} 
 
	HANDLE hDevice; 
	 
	// TODO: Add extra initialization here 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="打开设备失败,请检查连接及设备名是否正确!\r\n"; 
	} 
	else 
	{ 
		m_strTestDisplay+="打开设备成功!\r\n"; 
	} 
	CloseHandle(hDevice); 
 
	StopTest = FALSE; 
	StopSpeedTest = FALSE; 
	StopIsoTest = FALSE; 
	StopIoTest = FALSE; 
	StopUsbToUart = FALSE; 
	 
	SetTimer(1, 100, 0); 
	 
//	StayOnTop(); 
//设置默认波特率9600 
	m_ctrlBaudRate.SetCurSel(2);//////设置默认波特率CComboBox中选择9600对应的index  2 
 
	m_FifoWrRdNum = 0;//设置FIFO读写控制变量 
 
	UpdateData(FALSE);	 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CCYDEMODlg::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 CCYDEMODlg::OnPaint()  
{ 
	if (IsIconic())//The IsIconic function determines whether the specified window is minimized .  
 
	{ 
		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 
	{ 
		this->DrawView(); //此句为自添加 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CCYDEMODlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
/////////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////// 
//////////								/////////////////////////////////// 
///////////			画图相关函数		/////////////////////////////////// 
//////////								/////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////// 
void CCYDEMODlg::OnDraw()  
{ 
	// TODO: Add your control notification handler code here 
	CRect rect; 
 
	CDC *pDC; 
	CDC memDC; 
 
	// 获取绘制坐标的文本框 
	CWnd* pWnd = GetDlgItem(IDC_DRAW); 
	//获得对话框上的picture的窗口句柄 
	pWnd->GetClientRect(&rect); 
	// 指针 
	pDC = pWnd->GetDC();	//得到显示设备的句柄 
 
	pWnd->Invalidate();		//使整个客户区无效 
	pWnd->UpdateWindow();	//使整个窗口客户区有效 
	 
    //内存绘图 
    CBitmap memBitmap; 
	CBitmap* pOldBmp = NULL; 
	//创建内存绘图设备 
//	memDC.CreateCompatibleDC(pDC);	//memDC为CDC类的一个对象,可以传递给DrawWave 
	memDC.CreateCompatibleDC(NULL);	//这时还不能绘图,因为没有地方画 
 
	//CreateCompatibleBitmap()图和显示设备相匹配,pDC 为显示设备句柄 
	memBitmap.CreateCompatibleBitmap(pDC,rect.right,rect.bottom); 
	//SelectObject()为显示设备选择一个对象 
	pOldBmp = memDC.SelectObject(&memBitmap); 
 
 
//文本背景色 
	memDC.FillSolidRect(0,0,100,100,RGB(192,192,192));  
 
	DrawWave(&memDC);	//绘图函数 
 
	//把内存绘图拷贝到屏幕 
	pDC->BitBlt(rect.left,rect.top,rect.right,rect.bottom,&memDC,0,0,SRCCOPY); 
 
	memDC.SelectObject(pOldBmp); 
	//删除对象 
	memDC.DeleteDC(); 
	memBitmap.DeleteObject(); 
	pDC->DeleteDC(); 
} 
 
void CCYDEMODlg::DrawWave(CDC *pDC) 
{ 
	CRect rect;   //位置对象 
	 
	// 获取绘制坐标的文本框 
	CWnd* pWnd = GetDlgItem(IDC_DRAW); //IDC_Draw 为Picture 控件的ID 
	 
	pWnd->GetClientRect(&rect);	//用rect得到绘图区域的范围 
	pDC->Rectangle(&rect);	//用得到的绘图区域定义绘图的矩形区 
	 
	DrawBasic(pDC,rect); 
	DrawInner(pDC,rect); 
	DrawGrid(pDC,rect); 
	DrawData(pDC,rect); 
} 
 
 
/////////////////////////////////////////////////// 
void CCYDEMODlg::DrawBasic(CDC * pDC,CRect &rect) 
{ 
	CRect BasicRect; 
    BasicRect.left = rect.left; 
	BasicRect.top = rect.top; 
	BasicRect.right = rect.right; 
	BasicRect.bottom = rect.bottom; 
	CBrush brushctlBkColor(RGB(192,192,192)); 
//	CBrush brushctlBkColor(RGB(255,255,255)); 
	pDC->FillRect(BasicRect,&brushctlBkColor); 
} 
 
/////////////////////////////////////////////////// 
void CCYDEMODlg::DrawInner(CDC * pDC,CRect &rect) 
{ 
	CRect InnerRect; 
 
	Inner_Left = rect.left+30; 
	Inner_Right = rect.right; 
	Inner_Top = rect.top; 
	Inner_Bottom = rect.bottom-15; 
 
    InnerRect.left = Inner_Left; 
	InnerRect.right = Inner_Right; 
	InnerRect.top = Inner_Top; 
	InnerRect.bottom = Inner_Bottom; 
 
	CBrush brush(RGB(0,0,0)); 
	pDC->FillRect(InnerRect,&brush); 
} 
 
void CCYDEMODlg::DrawGrid(CDC * pDC,CRect &rect) 
{ 
	CRect InnerRect; 
    InnerRect.left = Inner_Left+5; 
	InnerRect.right = Inner_Right-1; 
	InnerRect.top = Inner_Top+5; 
	InnerRect.bottom = Inner_Bottom-5; 
 
	// 创建画笔对象---------------------------------------- 
	CPen* pPenGrid = new CPen; 
	// GRID画笔 
	pPenGrid->CreatePen(PS_SOLID,1,RGB(192,192,192)); 
	// 选中当前黑色画笔,并保存以前的画笔 
	CGdiObject* pOldPen = pDC->SelectObject(pPenGrid); 
 
 
	LONG x_div,y_div;  
	int i=0; 
	x_div = (LONG)((InnerRect.right-InnerRect.left)/10.0); 
	y_div = (LONG)((InnerRect.bottom-InnerRect.top)/10.0); 
	pDC->SelectObject(pPenGrid); 
 
	CString str; 
	for (i=0;i<=10;i++) 
	{ 
		pDC->MoveTo(InnerRect.left+i*x_div,InnerRect.bottom); 
		pDC->LineTo(InnerRect.left+i*x_div,InnerRect.top); 
		str.Format("%02d ",i); 
		pDC->TextOut(InnerRect.left+i*x_div-10,InnerRect.bottom+5,str);	//X轴标 
	} 
	for (i=0;i<=10;i++) 
	{ 
		pDC->MoveTo(InnerRect.left,InnerRect.top+i*y_div); 
		pDC->LineTo(InnerRect.right,InnerRect.top+i*y_div); 
		str.Format("%03d ",i*10); 
		pDC->TextOut(InnerRect.left-35,InnerRect.bottom-i*y_div-10,str);	//Y轴标 
	} 
 
	// 恢复以前的画笔 
	pDC->SelectObject(pOldPen);	 
	 
	delete pPenGrid; 
} 
 
void CCYDEMODlg::DrawData(CDC * pDC,CRect &rect) 
{ 
	CRect InnerRect; 
    InnerRect.left = Inner_Left+5; 
	InnerRect.right = Inner_Right-1; 
	InnerRect.top = Inner_Top+5; 
	InnerRect.bottom = Inner_Bottom-5; 
	 
	// 创建画笔对象---------------------------------------- 
	CPen* pPenGrid = new CPen; 
	// GRID画笔 
	pPenGrid->CreatePen(PS_SOLID,1,RGB(255,0,0)); 
	// 选中当前黑色画笔,并保存以前的画笔 
	CGdiObject* pOldPen = pDC->SelectObject(pPenGrid); 
	 
	 
	LONG x_div,y_div;  
	int i=0,j=0; 
	x_div = (LONG)((InnerRect.right-InnerRect.left)/10.0); 
	y_div = (LONG)((InnerRect.bottom-InnerRect.top)/10.0); 
	pDC->SelectObject(pPenGrid); 
	 
	UCHAR datadis; 
	pDC->MoveTo(InnerRect.left,InnerRect.bottom); 
	for (i=0;i<400;i++) 
	{ 
//		Drawdatabuf[i] = rand(); 
//		datadis = (BYTE) rand(); 
/* 
		if (j<50) 
		{ 
			j++; 
			datadis = (BYTE) j*5; 
		}  
		else 
		{ 
			j=0; 
			datadis = (BYTE) j*5; 
		} 
*/ 
		datadis = (UCHAR)Drawdatabuf[i]; 
		 
		pDC->LineTo(InnerRect.left+i,InnerRect.bottom-datadis); 
	} 
	 
	// 恢复以前的画笔 
	pDC->SelectObject(pOldPen);	 
	 
	delete pPenGrid; 
} 
/////////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////////////////////// 
 
 
void CCYDEMODlg::OnButtonThreadtest()  
{ 
	// TODO: Add your control notification handler code here 
	static BOOL startorstopthreadtest = FALSE; 
	CString strtemp = ""; 
	startorstopthreadtest = !startorstopthreadtest; 
	if (startorstopthreadtest) 
	{ 
		UpdateData(TRUE); 
		strtemp = "关闭多线程\r\n测试"; 
		m_ctrlThreadTest.SetWindowText(strtemp);//m_ctr1ThreadTest 为CCYDEMODlg类中一个CButton类对象, 
		StopTest=FALSE; 
		m_BulkTest.testDlg=this;////m_BulkTest为CCYDEMODlg类中一个CThreadparam 结构体的数据成员 
               ////////////CThreadparam中包括 hThread,testdlg ,bInThread ,bUpData 
 
 
		m_BulkTest.bUpdate=FALSE; 
		CWinThread * wt = AfxBeginThread(  
			TestThread, // thread function  
			&m_BulkTest); // argument to thread function  
		m_BulkTest.hThread = wt->m_hThread; 
		UpdateData(FALSE); 
	}  
	else 
	{ 
		StopTest = TRUE;/////控制是否为多线程,以及多线程的结束线程标记 
		strtemp = "启动多线程\r\n测试"; 
		m_ctrlThreadTest.SetWindowText(strtemp); 
	} 
} 
 
void CCYDEMODlg::OnTimer(UINT nIDEvent) ///????????????????????nIDEvent为一UNIT 
{ 
	// TODO: Add your message handler code here and/or call default 
	if(m_BulkTest.bUpdate) 
	{ 
		m_BulkTest.bUpdate=FALSE; 
		UpdateData(FALSE);    
	}	 
	CDialog::OnTimer(nIDEvent); 
} 
 
void CCYDEMODlg::OnButtonClear() //////清空显示框 
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	m_strTestDisplay = ""; 
	UpdateData(FALSE); 
} 
 
void CCYDEMODlg::OnButtonWrSpeedtest()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	PUCHAR outBuffer = NULL; 
	unsigned long mstep,mlength; 
	double speed;	 
	BULK_TRANSFER_CONTROL   outBulkControl;//inBulkControl,   结构体   pipenum 
	 
	HANDLE hOutDevice=NULL; 
	char output[256];    
	double mTotal; 
 
	if (!OnSetOutDir())///////OnSetOutDir() 设置输出端口,请求等    返回值为true表示设置成功 
    { 
		free(outBuffer); 
		CloseHandle(hOutDevice); 
		return ; 
    } 
 
	if (bOpenDriver (&hOutDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		CloseHandle(hOutDevice); 
		return ; 
    } 
    UCHAR  wo=0,sss=127,sss2=-128; 
	outBuffer=(PUCHAR) malloc(m_TestBytes);//m_TestBytes 为对话框中一个DWORD 的数据成员  
	srand(m_TestBytes);/////随机设置起点为m_TestBytes 
	for(ULONG i=0;i<m_TestBytes;i++)//////	m_TestBytes = 65535;初始化时设置 
//	{ 
		outBuffer[i]=rand();//=wo++;//  
	   // if(wo>=sss) 
	    // wo-=sss2; 
//	} 
 
	BOOLEAN status=FALSE; 
	ULONG BytesReturned=0; 
	ULONG nTestCount=1000;//共循环测试1000次 
	outBulkControl.pipeNum=0;//端点选择EP2 
 
	mstep=GetTickCount(); //起始时间	 
	for( i=0;i<nTestCount;i++)//多次循环测试 
	{ 
		 
		status =  DeviceIoControl (hOutDevice, 
			IOCTL_EZUSB_BULK_WRITE, 
			(PVOID)&outBulkControl, 
			sizeof(BULK_TRANSFER_CONTROL), 
			outBuffer,//输出缓冲区 
			m_TestBytes,//字节数,在对话框中可以设置,65535为较佳值 
			&BytesReturned,//返回字节数据 
			//这里为了测试速度,没有测试返回字节数 
			NULL);	  
		 
	}	 
	mlength=GetTickCount();//结束时间	 
	if(status==TRUE) 
	{ 
		mTotal=m_TestBytes * nTestCount;   
		mlength-=mstep; 
		if ( mlength !=0 ) speed=(mTotal/mlength)/1000.0;///////1000??????     millisecond  转换为second 
 
		else speed=9999999; 
		sprintf( output, 
			"写数据速度为:%3.3f MBytes/Sec\r\n共传输:%d×%d=%10.2f字节\r\n耗时:%ldmS\r\n",  
			speed,m_TestBytes, nTestCount,mTotal, mlength); 
		m_strTestDisplay=output;		 
	} 
	else 
	{ 
		m_strTestDisplay="测试失败!\r\n"; 
	} 
	 
	//关闭设备 
	free(outBuffer); 
	CloseHandle(hOutDevice); 
	UpdateData(FALSE); 
} 
 
void CCYDEMODlg::OnButtonRdSpeedtest()  
{ 
	// TODO: Add your control notification handler code here 
	CFile file; 
	CFileException e;////接收错误操作的status 
	file.Open("E:\\error.txt", CFile::modeWrite, &e );//将接收错误操作的status保存在CFileException定义的文件中 
	                               // CFile::modeWrite  Opens the file for writing only. 
	int i; 
		 
	UpdateData(TRUE); 
	PUCHAR outBuffer = NULL; 
	unsigned long mstep,mlength; 
	double speed;	 
	int nTestCount=100;//共循环测试100次 
 
	BULK_TRANSFER_CONTROL   inBulkControl;//,outBulkControl     pipenum 
	THREAD_CONTROL  inThreadControl;//,outThreadControl      
	HANDLE hOutDevice=NULL; 
 
	HANDLE ReadCompleteEvent; 
//	ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);/////???????????????为甚不用这一句 
 
	char output[256]; 
	double mTotal; 
 
	if (!OnSetInDir()) 
    { 
		free(outBuffer); 
		CloseHandle(hOutDevice); 
		return ; 
    } 
	 
	if (bOpenDriver (&hOutDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		CloseHandle(hOutDevice); 
		return ; 
    } 
 
	outBuffer=(unsigned char*)malloc(nTestCount*512*127);     /////512为缓存器大小,,,127  ????、 
     /////   初始化输入线程参数 
	inBulkControl.pipeNum=1;  //端点选择EP6 
	inThreadControl.hDevice=hOutDevice; 
	inThreadControl.Ioctl=IOCTL_EZUSB_BULK_READ; 
	inThreadControl.InBuffer=(void*)&inBulkControl;   ///  pipenum  
	inThreadControl.InBufferSize=sizeof(BULK_TRANSFER_CONTROL); 
	inThreadControl.OutBuffer=outBuffer; 
	inThreadControl.OutBufferSize= 512*127;                      ///////127 
	inThreadControl.status=FALSE; 
	inThreadControl.BytesReturned=0; 
	inThreadControl.completionEvent=ReadCompleteEvent;     
 
	DWORD s; 
 
	mstep=GetTickCount(); //起始时间 
	for( i=0;i<nTestCount;i++)//多次循环测试100次 
	{ 
		inThreadControl.OutBuffer=outBuffer+i*512*127;/////outBuffer is a PCHAR  
		WaitForSingleObject( CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)TransferThread, (LPVOID)&inThreadControl,(DWORD)0,&s),INFINITE);	 
	} 
 
	mlength=GetTickCount();//结束时间 
 
	file.Write(outBuffer,nTestCount*512*127);////将数据写入file中 
 
	if(inThreadControl.status)         //// THREAD_CONTROL inThreadControl  
	{ 
		mTotal=nTestCount*512*127;   
		mlength-=mstep; 
		if ( mlength !=0 ) speed=(mTotal/mlength)/1000.0; 
		else speed=9999999; 
		sprintf( output, 
			"读数据速度为:%3.3f MBytes/Sec\r\n共传输:%10.2f字节\r\n耗时:%ldmS\r\n",  
			speed, mTotal, mlength); 
		m_strTestDisplay=output;		 
	} 
	else 
	{ 
		m_strTestDisplay="测试失败\r\n"; 
	} 
 
	//关闭设备 
 
	file.Close(); 
	free(outBuffer); 
	CloseHandle(hOutDevice); 
	UpdateData(FALSE); 
} 
 
void CCYDEMODlg::OnButtonPort() ////端点列表控件 
{ 
	// TODO: Add your control notification handler code here 
	HANDLE  hDevice = NULL; 
	ULONG   nBytes; 
	char tempbuff[256]; 
	PUSBD_INTERFACE_INFORMATION pInterfaceInfo; 
	PUSBD_PIPE_INFORMATION pPipeInfo; 
	ULONG i; 
	UCHAR gInterfaceInfo [1024]; 
 
	UpdateData(TRUE); 
	 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
	 
	DeviceIoControl (hDevice, 
		IOCTL_Ezusb_GET_PIPE_INFO, 
		NULL, 
		0, 
		&gInterfaceInfo,   ////   lpOutBuffer 
		sizeof(gInterfaceInfo), 
		&nBytes, 
		NULL); 
	 
	CloseHandle(hDevice); 
	 
	pInterfaceInfo = (PUSBD_INTERFACE_INFORMATION) &gInterfaceInfo; 
	 
	 
	m_strTestDisplay+="Pipe  Endpoint  Direction  Type  Size\r\n"; 
	 
	for (i=0;i<pInterfaceInfo->NumberOfPipes;i++) 
	{ 
		pPipeInfo = &pInterfaceInfo->Pipes[i]; 
		sprintf(tempbuff,"%2d     %2d          %s     %s  %4d", 
			i, 
			pPipeInfo->EndpointAddress & 0x0F,/////////0x0F   高四位清0,后四位保留,后四位表示endpoint编号2,4,6,8 
			pPipeInfo->EndpointAddress & 0x80 ? "IN " : "OUT",/////////0x80  =  二进制 10000000   判断最高位是否为1, 1  表示传输方向 
			PIPE_TYPE_STRINGS[pPipeInfo->PipeType], 
			pPipeInfo->MaximumPacketSize); 
		m_strTestDisplay+=tempbuff; 
		m_strTestDisplay+="\r\n"; 
		 
	} 
   UpdateData(FALSE);	 
} 
 
void CCYDEMODlg::OnButtonMcureset()     /////    mcu复位 
{ 
	// TODO: Add your control notification handler code here 
	HANDLE hDevice=NULL; 
	BOOL   bResult = FALSE; 
	ULONG  nBytes; 
//	char   tempbuff[256]; 
	VENDOR_REQUEST_IN	myRequest; 
 
	UpdateData(TRUE); 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
 
	myRequest.bRequest = 0xA0;//////?????????????////?????// 
	myRequest.wValue = 0xE600; 
	myRequest.wIndex = 0x00; 
	myRequest.wLength = 0x01; 
	myRequest.bData = 0;	//复位 
	myRequest.direction = 0x00; 
 
	bResult = DeviceIoControl (hDevice, 
		IOCTL_Ezusb_VENDOR_REQUEST, 
		&myRequest, 
		sizeof(VENDOR_REQUEST_IN), 
		NULL, 
		0, 
		&nBytes, 
		NULL); 
 
	if (bResult!=TRUE)  
		m_strTestDisplay+="复位8051失败!\r\n"; 
	else { 
		m_strTestDisplay+="8051已经复位!\r\n"; 
	} 
	CloseHandle(hDevice); 
	UpdateData(FALSE); 
	return; 
} 
 
void CCYDEMODlg::OnButtonMculock()                  ////mcu 锁定 
{ 
	// TODO: Add your control notification handler code here 
	HANDLE hDevice=NULL; 
	BOOL   bResult = FALSE; 
	ULONG  nBytes; 
//	char   tempbuff[256]; 
	VENDOR_REQUEST_IN	myRequest; 
 
	UpdateData(TRUE); 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
 
	myRequest.bRequest = 0xA0; 
	myRequest.wValue = 0xE600; 
	myRequest.wIndex = 0x00; 
	myRequest.wLength = 0x01; 
	myRequest.bData = 1;	//锁定 
	myRequest.direction = 0x00; 
 
	bResult = DeviceIoControl (hDevice, 
		IOCTL_Ezusb_VENDOR_REQUEST, 
		&myRequest, 
		sizeof(VENDOR_REQUEST_IN), 
		NULL, 
		0, 
		&nBytes, 
		NULL); 
 
	if (bResult!=TRUE)  
		m_strTestDisplay+="锁定8051失败!\r\n"; 
	else { 
		m_strTestDisplay+="8051已经锁定!\r\n"; 
	} 
	CloseHandle(hDevice); 
	UpdateData(FALSE); 
	return;	 
} 
 
void CCYDEMODlg::OnButtonDownload() ////下载固件代码  
{ 
	// TODO: Add your control notification handler code here 
	LPTSTR DownloadFile = m_strFileName.GetBuffer(100);////LPTSTR  一个32bit的字符串指针	 
                                                  	/* OnInitDialog() 中已经初始化  m_strFileName    固件代码文件名 
                                                      	::GetCurrentDirectory(100,PathName); 
	  
                                                     	m_strFileName.Format("%s",PathName);*/ 
	 
	if (OnDownloadFirmware(DownloadFile,TRUE))/////下载固件代码 
	{ 
		m_strTestDisplay+="程序下载完毕!\r\n"; 
		UpdateData(FALSE); 
	} 
} 
 
void CCYDEMODlg::OnButtonFrame() //   传送帧 
{ 
	// TODO: Add your control notification handler code here 
	HANDLE hDevice=NULL; 
	BOOL   bResult = FALSE; 
	ULONG  nBytes; 
	char   tempbuff[256]; 
	unsigned long  output; 
 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
 
	bResult = DeviceIoControl (hDevice, 
		IOCTL_EZUSB_GET_CURRENT_FRAME_NUMBER, 
		NULL, 
		0, 
		&output, 
		sizeof(unsigned long), 
		&nBytes, 
		NULL); 
	if (bResult!=TRUE)  
	{ 
		m_strTestDisplay+="读取传输帧字节数失败!\r\n"; 
		UpdateData(FALSE); 
	} 
    else  
	{ 
		sprintf(tempbuff,"已传输帧字节数:%d\r\n",output); 
		m_strTestDisplay+=tempbuff; 
		UpdateData(FALSE);		 
    } 
	CloseHandle(hDevice); 
	return;	 
} 
 
void CCYDEMODlg::OnButtonGetstring() ////字符描述符  
{ 
	// TODO: Add your control notification handler code here 
	HANDLE hDevice=NULL; 
	BOOL   bResult = FALSE; 
	ULONG  nBytes; 
	char   tempbuff[18][256];/// 
	int    i; 
	char output0[50]; 
	GET_STRING_DESCRIPTOR_IN input;//    index   languageId 
	USB_STRING_DESCRIPTOR    output;//     blength   bDescriptorType   bString[1] 
 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
 
	input.Index=0; 
	input.LanguageId=0;	 
	bResult = DeviceIoControl (hDevice, 
                               IOCTL_Ezusb_GET_STRING_DESCRIPTOR, 
                               &input, 
                               sizeof(GET_STRING_DESCRIPTOR_IN), 
                               &output,//    output  is a stuct       	USB_STRING_DESCRIPTOR 
                               4, 
                               &nBytes, 
                               NULL); 
	if (bResult!=TRUE)  
	{ 
		m_strTestDisplay+="读取字符串描述符失败!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
    else  
	{ 
		bResult = DeviceIoControl (hDevice, 
                                   IOCTL_Ezusb_GET_STRING_DESCRIPTOR, 
                                   &input, 
                                   sizeof(GET_STRING_DESCRIPTOR_IN), 
                                   &output0,/////////////////////?????????????????????? 
                                   output.bLength,  
                                   &nBytes, 
                                   NULL); 
		if (bResult!=TRUE)  
		{ 
			m_strTestDisplay+="读取字符串描述符失败!\r\n"; 
			UpdateData(FALSE); 
			return; 
		} 
		else  
		{ 
			m_strTestDisplay+="测试-字符串描述符(String Descrioptor):\r\n"; 
			UpdateData(FALSE); 
 
			sprintf(tempbuff[0],"字符串描述符[0]:"); 
			sprintf(tempbuff[1],"[0]描述符长度(bLength): 0x%xH",output0[0]); 
            sprintf(tempbuff[2],"[1]描述符类型(bDescriptorType):0x0%xH",output0[1]); 
		    sprintf(tempbuff[3],"[2]字符串(bString):"); 
		    for (i=0;i<output.bLength-2;i++)  
			{ 
				sprintf(tempbuff[4],"0%d",output0[i+2]);///////////////            i+2      ?????????? 
				strcat(tempbuff[3],tempbuff[4]); 
			} 
			for(i=0;i<4;i++)  
			{ 
				m_strTestDisplay+=tempbuff[i]; 
				m_strTestDisplay+="\r\n"; 
				UpdateData(FALSE); 
			} 
		}         
    } 
 
	input.Index=1; 
	input.LanguageId=0; 
	bResult = DeviceIoControl (hDevice, 
                               IOCTL_Ezusb_GET_STRING_DESCRIPTOR, 
                               &input, 
                               sizeof(GET_STRING_DESCRIPTOR_IN), 
                               &output, 
                               4,  
                               &nBytes, 
                               NULL); 
	if (bResult!=TRUE)  
	{ 
		m_strTestDisplay+="读取字符串描述符失败!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
    else { 
		bResult = DeviceIoControl (hDevice, 
                                   IOCTL_Ezusb_GET_STRING_DESCRIPTOR, 
                                   &input, 
                                   sizeof(GET_STRING_DESCRIPTOR_IN), 
                                   &output0, 
                                   output.bLength,  
                                   &nBytes, 
                                   NULL); 
		if (bResult!=TRUE)  
		{ 
			m_strTestDisplay+="读取字符串描述符失败!\r\n"; 
			UpdateData(FALSE); 
			return; 
		} 
		else  
		{ 
			sprintf(tempbuff[0],"字符串描述符[1]:"); 
			sprintf(tempbuff[1],"[0]描述符长度(bLength): 0x%xH",output0[0]); 
            sprintf(tempbuff[2],"[1]描述符类型(bDescriptorType):0x0%xH",output0[1]); 
		    sprintf(tempbuff[3],"[2]字符串(bString):"); 
		    for (i=0;i<output.bLength-2;i++)  
			{ 
				sprintf(tempbuff[4],"%c",output0[i+2]); 
				strcat(tempbuff[3],tempbuff[4]); 
			} 
			for(i=0;i<4;i++)  
			{ 
				m_strTestDisplay+=tempbuff[i]; 
				m_strTestDisplay+="\r\n"; 
				UpdateData(FALSE); 
			} 
		}         
    } 
 
	input.Index=2; 
	input.LanguageId=0; 
	bResult = DeviceIoControl (hDevice, 
                               IOCTL_Ezusb_GET_STRING_DESCRIPTOR, 
                               &input, 
                               sizeof(GET_STRING_DESCRIPTOR_IN), 
                               &output, 
                               4,  
                               &nBytes, 
                               NULL); 
	if (bResult!=TRUE)  
	{ 
		m_strTestDisplay+="读取字符串描述符失败!\r\n"; 
		UpdateData(FALSE); 
	} 
    else { 
		bResult = DeviceIoControl (hDevice, 
                                   IOCTL_Ezusb_GET_STRING_DESCRIPTOR, 
                                   &input, 
                                   sizeof(GET_STRING_DESCRIPTOR_IN), 
                                   &output0, 
                                   output.bLength,  
                                   &nBytes, 
                                   NULL); 
		if (bResult!=TRUE)  
		{ 
			m_strTestDisplay+="读取字符串描述符失败!\r\n"; 
			UpdateData(FALSE); 
		} 
		else { 
			sprintf(tempbuff[0],"字符串描述符[2]:"); 
			sprintf(tempbuff[1],"[0]描述符长度(bLength): 0x%xH",output0[0]); 
            sprintf(tempbuff[2],"[1]描述符类型(bDescriptorType):0x0%xH",output0[1]); 
		    sprintf(tempbuff[3],"[2]字符串(bString):"); 
		    for (i=0;i<output.bLength-2;i++)  
			{ 
				sprintf(tempbuff[4],"%c",output0[i+2]); 
				strcat(tempbuff[3],tempbuff[4]); 
			} 
			for(i=0;i<4;i++)  
			{ 
				m_strTestDisplay+=tempbuff[i]; 
				m_strTestDisplay+="\r\n"; 
				UpdateData(FALSE); 
			} 
		}         
    } 
	CloseHandle(hDevice); 
	return;	 
} 
 
void CCYDEMODlg::OnButtonGetport() /////////////端点描述 
{ 
	// TODO: Add your control notification handler code here 
	HANDLE hDevice=NULL; 
	BOOL   bResult = FALSE; 
	ULONG  nBytes; 
	char   tempbuff[18][256]; 
	unsigned char output2[200]; 
	int    i,j; 
	Usb_Configuration_Descriptor output; 
	Usb_Endpoint_Descriptor *output0; 
 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
 
	bResult = DeviceIoControl (hDevice, 
		IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR, 
		NULL, 
		0, 
		&output, 
		sizeof(Usb_Configuration_Descriptor), 
		&nBytes, 
		NULL); 
	if (bResult!=TRUE)  
	{ 
		m_strTestDisplay+="读取端点描述符失败!\r\n"; 
		UpdateData(FALSE); 
	} 
	else  
	{ 
		bResult = DeviceIoControl (hDevice, 
			IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR, 
			NULL, 
			0, 
			&output2, 
			output.wTotalLength, 
			&nBytes, 
			NULL); 
		if (bResult!=TRUE)  
		{ 
			m_strTestDisplay+="读取端点描述符失败!\r\n"; 
			UpdateData(FALSE); 
		} 
		else { 
			m_strTestDisplay+="测试-端点描述符(Endpoint Descrioptor):\r\n"; 
			UpdateData(FALSE); 
			 
			for (i=0;i<4;i++) //四个端口 
			{ 
				output0=(Usb_Endpoint_Descriptor *)&output2[18+i*7];//让output0指向output2的地址空间,output2中存放配置描述??? 
				sprintf(tempbuff[0],"端点[%d]:",i+1);//tempbuff[18][200] 
				sprintf(tempbuff[1],"[0]描述符长度(bLength): 0x0%xH",output0->bLength); 
                sprintf(tempbuff[2],"[1]描述符类型(bDescriptorType):0x0%xH",output0->bDescriptorType); 
				sprintf(tempbuff[3],"[2]端点号及方向(bEndpointAddress): 0x%0xH",output0->bEndpointAddress); 
				sprintf(tempbuff[4],"[3]传输类型(bmAttributes):0x0%xH",output0->bmAttributes); 
				sprintf(tempbuff[5],"[4]最大包的大小(wMaxPacketSize):0x0%xH",output0->wMaxPacketSize); 
                sprintf(tempbuff[6],"[5]查询间隔(bInterval):0x0%xH",output0->bInterval); 
				for(j=0;j<7;j++)  
				{ 
					m_strTestDisplay+=tempbuff[j]; 
					m_strTestDisplay+="\r\n"; 
					UpdateData(FALSE); 
				} 
			} 
			sprintf(tempbuff[0],"**在测试框中显示了该配置所有字节的信息**\r\n");			 
			m_strTestDisplay+=tempbuff[0]; 
			UpdateData(FALSE); 
			 
			sprintf(tempbuff[0],""); 
			for (i=0;i<output.wTotalLength;i++)  
			{ 
				sprintf(tempbuff[1],"[%d]%xH  ",i,output2[i]);//output2 中存储内容输出,存储的内容为??????? 
				strcat(tempbuff[0],tempbuff[1]); 
				if(i%5==0 && i!=0)  
				{ 
					m_strTestDisplay+=tempbuff[0]; 
					m_strTestDisplay+="\r\n"; 
					UpdateData(FALSE); 
					sprintf(tempbuff[0],""); 
				} 
			} 
		} 
	} 
    CloseHandle(hDevice); 
	return;	 
} 
 
void CCYDEMODlg::OnButtonGetinterface() //接口描述 
{ 
	// TODO: Add your control notification handler code here 
	HANDLE hDevice=NULL; 
	BOOL   bResult = FALSE; 
	ULONG  nBytes; 
	char   tempbuff[18][256]; 
	unsigned char output2[200]; 
	int    i; 
	Usb_Configuration_Descriptor output; 
    Usb_Interface_Descriptor *output0; 
 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
	 
	bResult = DeviceIoControl (hDevice, 
		IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR, 
		NULL, 
		0, 
		&output, 
		sizeof(Usb_Configuration_Descriptor), 
		&nBytes, 
		NULL); 
	if (bResult!=TRUE)  
	{ 
		m_strTestDisplay+="读取接口描述符失败!\r\n"; 
		UpdateData(FALSE); 
	} 
	else { 
		bResult = DeviceIoControl (hDevice, 
			IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR, 
			NULL, 
			0, 
			&output2,                        ///	unsigned char output2[200] 
			output.wTotalLength, 
			&nBytes, 
			NULL); 
		if (bResult!=TRUE)  
		{ 
			m_strTestDisplay+="读取接口描述符失败!\r\n"; 
			UpdateData(FALSE); 
		} 
		else  
		{ 
			m_strTestDisplay+="测试-接口描述符(Interface Descrioptor):\r\n"; 
			UpdateData(FALSE); 
			 
			output0=(Usb_Interface_Descriptor *)&output2[9];              //////9  is  class  data members 
			sprintf(tempbuff[0],"[0]描述符长度(bLength): 0x0%xH",output0->bLength); 
            sprintf(tempbuff[1],"[1]描述符类型(bDescriptorType):0x0%xH",output0->bDescriptorType); 
			sprintf(tempbuff[2],"[2]接口号(bInterfaceNumber): 0x%0xH",output0->bInterfaceNumber); 
			sprintf(tempbuff[3],"[3]替换设置值(bAlternateSetting):0x0%xH",output0->bAlternateSetting); 
			sprintf(tempbuff[4],"[4]支持的端点数(bNumEndpoints):0x0%xH",output0->bNumEndpoints); 
            sprintf(tempbuff[5],"[5]类代码(bInterfaceClass):0x0%xH",output0->bInterfaceClass); 
            sprintf(tempbuff[6],"[6]子类代码(bInterfaceSubClass):0x%xH",output0->bInterfaceSubClass); 
			sprintf(tempbuff[7],"[7]协议代码(bInterfaceProtocol):0x0%xH",output0->bInterfaceProtocol); 
            sprintf(tempbuff[8],"[8]该接口的字符串描述符(iInterface):0x0%xH",output0->iInterface); 
			sprintf(tempbuff[9],"**在测试框中显示了该配置所有字节的信息**");         
			for(i=0;i<10;i++)  
			{ 
				m_strTestDisplay+=tempbuff[i]; 
				m_strTestDisplay+="\r\n"; 
			} 
			UpdateData(FALSE); 
			 
			sprintf(tempbuff[0],""); 
			for (i=0;i<output.wTotalLength;i++) { 
				sprintf(tempbuff[1],"[%d]%xH  ",i,output2[i]);////output2中数据为Usb_Interface_Descriptor    			IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR, 
 
				strcat(tempbuff[0],tempbuff[1]); 
				if(i%5==0 && i!=0)  
				{ 
					m_strTestDisplay+=tempbuff[0]; 
					m_strTestDisplay+="\r\n"; 
					sprintf(tempbuff[0],""); 
				} 
				UpdateData(FALSE); 
			} 
		}//////////////////为何要两次输出output   output2 
	} 
    CloseHandle(hDevice); 
	return;	 
} 
 
void CCYDEMODlg::OnButtonGetconfig() ////配置描述 
{ 
	// TODO: Add your control notification handler code here 
	HANDLE hDevice=NULL; 
	BOOL   bResult = FALSE; 
	ULONG  nBytes; 
	char   tempbuff[18][256]; 
	unsigned char output2[200]; 
	int    i; 
	Usb_Configuration_Descriptor output; 
	 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
	 
	bResult = DeviceIoControl (hDevice, 
		IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR, 
		NULL, 
		0, 
		&output, 
		sizeof(Usb_Configuration_Descriptor), 
		&nBytes, 
		NULL); 
	if (bResult!=TRUE)  
	{ 
		m_strTestDisplay+="读取配置描述符失败!\r\n"; 
		UpdateData(FALSE); 
	} 
    else  
	{ 
		m_strTestDisplay+="测试-配置描述符(ConfigurationDescrioptor):\r\n"; 
		UpdateData(FALSE); 
		 
		sprintf(tempbuff[0],"[0]描述符长度(bLength): 0x0%xH",output.bLength); 
        sprintf(tempbuff[1],"[1]描述符类型(bDescriptorType):0x0%xH",output.bDescriptorType); 
		sprintf(tempbuff[2],"[2]该配置所有数据的大小(wTotalLength): 0x%0xH",output.wTotalLength); 
		sprintf(tempbuff[3],"[4]该配置支持的接口数(bNumInterfaces):0x0%xH",output.bNumInterfaces); 
		sprintf(tempbuff[4],"[5]SetGet请求标识(bConfigurationValue):0x0%xH",output.bConfigurationValue); 
        sprintf(tempbuff[5],"[6]该配置的字符串描述符(iConfiguration):0x0%xH",output.iConfiguration); 
        sprintf(tempbuff[6],"[7]供电和唤醒设置(bmAttributes):0x%xH",output.bmAttributes); 
		sprintf(tempbuff[7],"[8]需要的总线电流(MaxPower):0x0%xH",output.MaxPower*2); 
        sprintf(tempbuff[8],"**在测试框中显示了该配置所有字节的信息**");         
		for(i=0;i<9;i++)  
		{ 
			m_strTestDisplay+=tempbuff[i]; 
			m_strTestDisplay+="\r\n"; 
		} 
		UpdateData(FALSE); 
		 
		bResult = DeviceIoControl (hDevice, 
			IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR, 
			NULL, 
			0, 
			&output2,      //////why   output2   when it had output 
			output.wTotalLength, 
			&nBytes, 
			NULL); 
		if (bResult!=TRUE)  
		{ 
			m_strTestDisplay+="读取配置描述符失败!\r\n"; 
			UpdateData(FALSE); 
		} 
		else{  
			sprintf(tempbuff[0],""); 
			for (i=0;i<output.wTotalLength;i++) { 
				sprintf(tempbuff[1],"[%d]%xH  ",i,output2[i]); 
				strcat(tempbuff[0],tempbuff[1]); 
				if(i%5==0 && i!=0)  
				{ 
					m_strTestDisplay+=tempbuff[0]; 
					m_strTestDisplay+="\r\n"; 
					sprintf(tempbuff[0],""); 
				} 
				UpdateData(FALSE); 
			} 
		} 
	} 
	CloseHandle(hDevice); 
	return;	 
} 
 
void CCYDEMODlg::OnButtonGetdevice() //设备描述 
{ 
	// TODO: Add your control notification handler code here 
	HANDLE hDevice=NULL; 
	BOOL   bResult = FALSE; 
	ULONG  nBytes; 
	char   tempbuff[18][256]; 
	int    i; 
	Usb_Device_Descriptor output; 
	 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
		UpdateData(FALSE); 
		return; 
	} 
 
	bResult = DeviceIoControl (hDevice, 
		IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR, 
		NULL, 
		0, 
		&output, 
		sizeof(Usb_Device_Descriptor), 
		&nBytes, 
		NULL); 
	if (bResult!=TRUE)  
	{ 
		m_strTestDisplay+="读取配置描述符失败!\r\n"; 
		UpdateData(FALSE); 
	} 
    else  
	{ 
		m_strTestDisplay+="测试-设备描述符(Device Descrioptor):\r\n"; 
		UpdateData(FALSE); 
		 
		sprintf(tempbuff[0],"[0]描述符长度(bLength): 0x%xH",output.bLength); 
        sprintf(tempbuff[1],"[1]描述符类型(bDescriptorType):0x0%xH",output.bDescriptorType); 
		sprintf(tempbuff[2],"[2]USB规范发布号(bcdUSB): 0x0%0xH",output.bcdUSB); 
		sprintf(tempbuff[3],"[4]类代码(bDeviceClass):0x0%xH",output.bDeviceClass); 
		sprintf(tempbuff[4],"[5]子类代码(bDeviceSubClass):0x0%xH",output.bDeviceSubClass); 
        sprintf(tempbuff[5],"[6]协议代码(bDeviceProtocol):0x0%xH",output.bDeviceProtocol); 
        sprintf(tempbuff[6],"[7]端点0最大包大小(bMaxPacketSize0):0x%xH",output.bMaxPacketSize0); 
		sprintf(tempbuff[7],"[8]销售商ID(idVendor):0x0%xH",output.idVendor); 
        sprintf(tempbuff[8],"[10]产品ID(idProduct):0x%xH",output.idProduct); 
		sprintf(tempbuff[9],"[12]设备发布号(bcdDevice):0x0%0xH",output.bcdDevice); 
		sprintf(tempbuff[10],"[14]制造商描述符(iManufacturer):0x0%xH",output.iManufacturer); 
		sprintf(tempbuff[11],"[15]产品描述符(iProduct):0x0%xH",output.iProduct); 
		sprintf(tempbuff[12],"[16]序列号描述符(iSerialNumber):0x0%xH",output.iSerialNumber); 
        sprintf(tempbuff[13],"[17]可能的配置号码(bNumConfigurations):0x0%xH",output.bNumConfigurations); 
         
		for(i=0;i<14;i++)  
		{ 
			m_strTestDisplay+=tempbuff[i]; 
			m_strTestDisplay+="\r\n"; 
		} 
		UpdateData(FALSE);		 
	} 
	CloseHandle(hDevice); 
	return;	 
} 
 
void CCYDEMODlg::OnButtonOpendevice() //  连接设备 
{ 
	// TODO: Add your control notification handler code here 
	HANDLE hDevice=NULL; 
 
	UpdateData(TRUE);                                ////重画 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
	} 
	else 
	{ 
		m_strTestDisplay+="连接设备成功!\r\n"; 
		CloseHandle(hDevice); 
	} 
	UpdateData(FALSE); 
	 
	return;	 
} 
 
void CCYDEMODlg::OnButtonTestio() /// 启动 IO  Test 
{ 
	// TODO: Add your control notification handler code here 
 
    HANDLE  hDevice = NULL; 
    BOOLEAN bResult = FALSE; 
    int		nBytes = 0;	 
	VENDOR_OR_CLASS_REQUEST_CONTROL	myRequest; 
	unsigned char *buffer; 
	unsigned char databuffer[256]; 
	ULONG bufferSize = 0; 
 
	static UCHAR disLED = 1; 
	UCHAR LEDval = 0; 
	 
	UpdateData(TRUE); 
	if (bOpenDriver (&hDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		m_strTestDisplay +="打开设备失败,测试中断!\r\n"; 
		UpdateData(FALSE); 
		CloseHandle(hDevice); 
		return ; 
    } 
	else 
	{ 
		m_strTestDisplay+="\r\n---------测试中----------\r\n"; 
		UpdateData(FALSE); 
	} 
 
	switch(disLED++)///static变量  全局控制 
	{ 
	case 1: 
		LEDval =~0x01;	//为0时点亮LED 
		break; 
	case 2: 
		LEDval =~0x02; 
		break; 
	case 3: 
		LEDval =~0x04; 
	    break; 
	case 4: 
		disLED = 1; 
		LEDval =~0x08; 
	    break; 
	default: 
		disLED = 1; 
		LEDval =~0x01; 
	    break; 
	} 
	myRequest.request = 0XB5;	//发送数据命令,与固件中BOOL DR_VendorCmnd(void)的命令配对 
	myRequest.value = (USHORT) LEDval;	//发送数据 
	myRequest.index = (USHORT) 0xbeef; 
	myRequest.direction = 0x00;	//out 
	bufferSize = 0x10;//16  
	buffer = databuffer;	//   buffer指向databuffer 数组空间 
	myRequest.requestType=0x02;/////02=vendor 
	myRequest.recepient=0x00;/////00=device 
	 
	if (hDevice != NULL)  
	{ 
		bResult = DeviceIoControl (hDevice, 
			IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST, 
			&myRequest, 
			sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL), 
			buffer, 
			bufferSize, 
			(unsigned long *)&nBytes, 
			NULL); 
	}	 
	 
	CloseHandle(hDevice); 
}//////////////////////////////////////////////////////    IOTest   仅仅发出请求而不输出任何内容 
 
//选择要发送的文件 
void CCYDEMODlg::OnButtonSelectfile() //选择固件文件 
{ 
	// TODO: Add your control notification handler code here 
	LPCSTR	lpszPath = "c:\\comdata"; 
	char PathName[100]; 
	::GetCurrentDirectory(100,PathName);	///获得当前程序文件路径赋给PathName 
	lpszPath = PathName; 
 
	SetCurrentDirectory( lpszPath );//设置  
	static char BASED_CODE szFilter[] = "BIX文件(*.bix)|*.bix|HEX文件(*.hex)|*.hex|所有文件(*.*)|*.*||"; 
	 
	CFileDialog FileDlg( TRUE,  NULL,  NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
		szFilter ); 
	/*  TRUE  表示construct a File Open dialog box   ,NULL  表示no file extension is appended   NULL表示 no filename initially appears. 
     OFN_HIDEREADONLY 表示Hides the Read Only check box.   |  
	 OFN_OVERWRITEPROMPT表示Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file. 
	 szFilter表示A pointer to the file dialog-box object’s parent or owner window. 
*/ 
	FileDlg.m_ofn.lpstrInitialDir = lpszPath ; 
	/*lpstrInitialDir  表示Pointer to a null terminated string that can specify the initial directory. The algorithm for selecting the initial directory varies on different platforms.  
	m_ofn表示The Windows OPENFILENAME structure. Provides access to basic file dialog box parameters.*/ 
	if( FileDlg.DoModal() ==IDOK ) 
	{ 
		 
		CString strFileName = FileDlg.GetFileName( );//Returns the filename of the selected file 
		CString strFileExt = FileDlg.GetFileExt( );//Returns the file extension of the selected file. 
		CString lpstrName =  FileDlg.GetPathName( );//Returns the full path of the selected file. 
		m_strFileName=lpstrName;// 改变对话框的数据成员m_strFileName为当前选择文件路径 
		UpdateData(FALSE); 
	} 
} 
 
/////////////////////////////////////////////////////////// 
/////////	跟皮肤绘制有关的函数	////////////////////// 
/////////////////////////////////////////////////////////// 
void CCYDEMODlg::DrawView() 
{ 
		CPaintDC dc(this); 
  
		CRect rect; 
		CString strText="USB2.0 + FPGA 开发板 VC 示例程序"; //文本内容 
		GetClientRect(rect); 
		rect.bottom=600; //自绘区域宽度 
 
		COLORREF	m_clrSideBarStart; //自绘区域起始处颜色 
		COLORREF	m_clrSideBarEnd;  //自绘区域结束处颜色 
 
		m_clrSideBarStart=RGB(190,190,190); //灰色 
		m_clrSideBarEnd=RGB(240,240,240); //灰色 
 
		//创建图及绘制区域 
		HBITMAP	bmpBar = CreateGradientBMP( 
			dc.m_hDC, m_clrSideBarStart, m_clrSideBarEnd, 
			rect.Width(), rect.Height(), 
			4, 256); 
 
		if (bmpBar) 
		{ 
			CDC memDC; 
			memDC.CreateCompatibleDC(&dc); 
			HBITMAP hOldBmp = (HBITMAP)::SelectObject(memDC.m_hDC, bmpBar); 
			dc.BitBlt(rect.left, rect.top, 
				rect.Width(), rect.Height(), 
				&memDC, 0, 0, SRCCOPY); 
			::SelectObject(memDC, hOldBmp); 
			::DeleteObject(bmpBar); 
		} 
		//绘制文本 
		CFont	vertFont; 
		//字体 
		vertFont.CreateFont( 
			30,							// nHeight 
			0,                         // nWidth,纵横比由最接近的决定 
			0,                         // nEscapement 
			0,                         // nOrientation 
			FW_NORMAL,                 // nWeight 
			FALSE,                     // bItalic,西方斜体 
			FALSE,                     // bUnderline,下划线 
			0,                         // cStrikeOut 
//			ANSI_CHARSET,              // nCharSet 
			DEFAULT_CHARSET, 
			OUT_DEFAULT_PRECIS,        // nOutPrecision 
			CLIP_DEFAULT_PRECIS,       // nClipPrecision 
			DEFAULT_QUALITY,           // nQuality 
			DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily 
			"宋体");					// lpszFacename,"华文行楷" 选择字体类型 
 
		CFont *pOldFont = dc.SelectObject(&vertFont); 
		COLORREF oldColor = dc.GetTextColor(); 
		dc.SetTextColor(RGB(50,50,250));  //文本颜色 
		dc.SetBkMode(TRANSPARENT); 
		dc.TextOut(rect.left+30, rect.top+5, strText); //文本起始位置 
		dc.SetTextColor(oldColor);	 
		dc.SelectObject(pOldFont); 
		vertFont.DeleteObject(); 
} 
 
//创建图 
HBITMAP CCYDEMODlg::CreateGradientBMP(HDC hDC, COLORREF cl1, COLORREF cl2, int nWidth, int nHeight, int nDir, int nNumColors) 
{ 
	int myval = 0; 
	int mycolval = 0; 
	int colchangflag = 0; 
 
	if(nNumColors > 256) 
		nNumColors = 256; 
 
	COLORREF PalVal[256]; 
	memset(PalVal, 0, sizeof(COLORREF)*256); 
 
	int nIndex; 
	BYTE peRed=0,peGreen=0,peBlue=0; 
 
	int r1=GetRValue(cl1); 
	int r2=GetRValue(cl2); 
	int g1=GetGValue(cl1); 
	int g2=GetGValue(cl2); 
	int b1=GetBValue(cl1); 
	int b2=GetBValue(cl2); 
 
    for (nIndex = 0; nIndex < nNumColors; nIndex++) 
    { 
        peRed = (BYTE) (r1 + MulDiv((r2-r1),nIndex,nNumColors-1)); 
        peGreen = (BYTE) (g1 + MulDiv((g2-g1),nIndex,nNumColors-1)); 
        peBlue = (BYTE) (b1 + MulDiv((b2-b1),nIndex,nNumColors-1)); 
 
		PalVal[nIndex]=(peRed << 16) | (peGreen << 8) | (peBlue); 
	} 
 
	int x,y,w,h; 
	w=nWidth; 
	h=nHeight; 
	 
	LPDWORD			pGradBits; 
	BITMAPINFO		GradBitInfo; 
 
	pGradBits=(DWORD*) malloc(w*h*sizeof(DWORD)); 
	memset(&GradBitInfo,0,sizeof(BITMAPINFO)); 
 
	GradBitInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); 
	GradBitInfo.bmiHeader.biWidth=w; 
	GradBitInfo.bmiHeader.biHeight=h; 
	GradBitInfo.bmiHeader.biPlanes=1; 
	GradBitInfo.bmiHeader.biBitCount=32; 
	GradBitInfo.bmiHeader.biCompression=BI_RGB; 
	 
	if(nDir==0)  
	{ 
		for(y=0;y<h;y++)  
		{ 
			for(x=0;x<w;x++)  
			{ 
				*(pGradBits+(y*w)+x)=PalVal[MulDiv(nNumColors,y,h)]; 
			} 
		} 
	} 
	else if(nDir==1)  
	{ 
		for(y=0;y<h;y++) 
		{ 
			int l,r; 
			l=MulDiv((nNumColors/2),y,h); 
			r=l+(nNumColors/2)-1; 
			for(x=0;x<w;x++) 
			{ 
				*(pGradBits+(y*w)+x)=PalVal[l+MulDiv((r-l),x,w)]; 
			} 
		} 
	} 
	else if(nDir==2) 
	{ 
		for(x=0;x<w;x++) 
		{ 
			for(y=0;y<h;y++) 
			{ 
				*(pGradBits+(y*w)+x)=PalVal[MulDiv(nNumColors,x,w)]; 
			} 
		} 
	} 
	else if(nDir==3) 
	{ 
		for(y=0;y<h;y++) 
		{ 
			int l,r; 
			r=MulDiv((nNumColors/2),y,h); 
			l=r+(nNumColors/2)-1; 
			for(x=0;x<w;x++) 
			{ 
				*(pGradBits+(y*w)+x)=PalVal[l+MulDiv((r-l),x,w)]; 
			} 
		} 
	} 
	else if(nDir==4) 
	{ 
		myval = 50; 
		for(y=0;y<h-42;y++) 
		{ 
			myval = 50; 
			mycolval = 5; 
			for(x=0;x<w;x++)  
			{ 
				if (x<10) 
				{ 
					mycolval += 24; 
					*(pGradBits+(y*w)+x)=PalVal[mycolval]; 
				}  
				else if (x<15) 
				{ 
					mycolval -= 40; 
					*(pGradBits+(y*w)+x)=PalVal[mycolval]; 
				}  
				else if (x<145) 
				{ 
					*(pGradBits+(y*w)+x)=PalVal[50]; 
				}  
				else if(x<w-15) 
				{ 
					*(pGradBits+(y*w)+x)=PalVal[myval]; 
				} 
				else if(x<w-5) 
				{ 
					mycolval += 20; 
					*(pGradBits+(y*w)+x)=PalVal[mycolval]; 
				} 
				else 
				{ 
					mycolval -= 30; 
					*(pGradBits+(y*w)+x)=PalVal[mycolval]; 
				} 
			} 
		} 
		for(y=h-42;y<h-12;y++)  
		{ 
			myval += 6; 
			for(x=0;x<w;x++) 
			{ 
				*(pGradBits+(y*w)+x)=PalVal[myval]; 
			} 
		} 
 
		for(y=h-12;y<h;y++)  
		{ 
			myval -= 15; 
			for(x=0;x<w;x++) 
			{ 
				*(pGradBits+(y*w)+x)=PalVal[myval]; 
			} 
		} 
	} 
 
	HBITMAP hBmp = CreateDIBitmap(hDC,&GradBitInfo.bmiHeader,CBM_INIT, 
						pGradBits,&GradBitInfo,DIB_RGB_COLORS); 
 
	free(pGradBits); 
	return hBmp; 
} 
/////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////// 
/////////////////////////////////////////////////////////// 
 
void CCYDEMODlg::OnButtonClosepro()  
{ 
	// TODO: Add your control notification handler code here 
	CCYDEMODlg::OnOK(); 
} 
 
 
 
 
void CCYDEMODlg::OnButtonI2ctest()  
{ 
    	// TODO: Add your control notification handler code here 
     //程序路径根据开发包的安装路径来定。 
     //C:\\Cypress\\USB\\Bin\\EzMr.exe 
	::ShellExecute(this->GetSafeHwnd(),NULL,"C:\\Cypress\\USB\\Bin\\EzMr.exe",NULL,NULL,SW_SHOW);	//启动FX2的控制面板 
	////ShellExecute表示Performs an operation on a specified file.  
    ///Activates the window and displays it in its current size and position.(SW_SHOW) 
    ///GetSafeHwnd获得窗口句柄 
} 
 
 
 
 
BOOL CCYDEMODlg::OnDownloadFirmware(LPTSTR strdownloadfile,BOOL displaycharen)  
{ 
	// TODO: Add your control notification handler code here 
	HANDLE hDevice=NULL; 
 	BOOL   bResult = FALSE; 
	ULONG  nBytes; 
	char   DeviceName[256] = ""; 
	char   tempbuff[256]; 
	char   temp[256]; 
//	char   DownloadFilename[16];	//DownloadFilename[16] 
//	FILE   *fp; 
    unsigned char buffer[MAX_BIX_SIZE];//////#define	MAX_BIX_SIZE 8192	//8K = 8192 OR 16K = 16384 
 
    int    numreadfile  = 0; 
	int i; 
 
	HWND hdlg = NULL; 
	VENDOR_REQUEST_IN	myRequest; 
 
	BOOL Downloadresult = FALSE; 
	 
	m_strTestDisplay+="\r\n"; 
 
	// Open the driver 
	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength()))) 
	{ 
		m_strTestDisplay+="无效设备,请重试!\r\n"; 
		UpdateData(FALSE); 
		return Downloadresult; 
	}	 
	 
	myRequest.bRequest = 0xA0; 
	myRequest.wValue = 0xE600; 
	myRequest.wIndex = 0x00; 
	myRequest.wLength = 0x01; 
	myRequest.bData = 1;	//锁定 
	myRequest.direction = 0x00; 
	 
	bResult = DeviceIoControl (hDevice, 
		IOCTL_Ezusb_VENDOR_REQUEST, 
		&myRequest, 
		sizeof(VENDOR_REQUEST_IN), 
		NULL, 
		0, 
		&nBytes, 
		NULL); 
	 
	if (bResult!=TRUE)  
	{ 
		m_strTestDisplay+="锁定8051失败!\r\n"; 
		CloseHandle(hDevice); 
		return Downloadresult; 
	}		 
	else { 
		m_strTestDisplay+="8051已经锁定!\r\n"; 
	} 
	UpdateData(FALSE); 
 
	CFile MyFile; 
	if (!MyFile.Open(strdownloadfile,CFile::modeRead,NULL)) 
	{ 
		MessageBox("打开文件失败!","信息提示",MB_OK); 
		CloseHandle(hDevice); 
		return Downloadresult; 
	}  
	else 
	{ 
		MyFile.Read(buffer,MAX_BIX_SIZE);	//读取文件 
		numreadfile = MAX_BIX_SIZE; 
		CString strfilelength; 
		strfilelength.Format("文件长度为:%d 字节\r\n",numreadfile); 
		m_strTestDisplay+=strfilelength; 
		UpdateData(FALSE); 
	} 
	MyFile.Close(); 
 
	if (displaycharen) 
	{ 
		sprintf (tempbuff, ""); 
		for (i=0;i<numreadfile;i++) { 
			sprintf(temp,"%02X ",buffer[i]);////////buffer     局部变量 
			strcat(tempbuff,temp); 
			m_strTestDisplay+=tempbuff; 
			sprintf(tempbuff,""); 
			if((i+1)%20==0 && i!=0) {	 
				m_strTestDisplay+="\r\n"; 
				if(i%1024==0 && i!=0) UpdateData(FALSE); 
			}	 
		} 
		m_strTestDisplay+="\r\n"; 
		UpdateData(FALSE); 
	} 
	 
    bResult = DeviceIoControl (hDevice, 
		IOCTL_Ezusb_ANCHOR_DOWNLOAD,////////////////// ???????????????????????? 
		buffer, 
		numreadfile, 
		NULL, 
		0, 
		&nBytes, 
		NULL); 
	if (bResult!=TRUE)  
	{ 
		m_strTestDisplay+="下载8051程序失败!\r\n"; 
	} 
    else  
	{ 
		m_strTestDisplay+="8051程序已经下载!\r\n"; 
	} 
	UpdateData(FALSE); 
 
	myRequest.bRequest = 0xA0; 
	myRequest.wValue = 0xE600; 
	myRequest.wIndex = 0x00; 
	myRequest.wLength = 0x01; 
	myRequest.bData = 0;	//复位 
	myRequest.direction = 0x00; 
 
	bResult = DeviceIoControl (hDevice, 
		IOCTL_Ezusb_VENDOR_REQUEST,////////////////////////????????????????????? 
		&myRequest, 
		sizeof(VENDOR_REQUEST_IN), 
		NULL, 
		0, 
		&nBytes, 
		NULL); 
 
	if (bResult!=TRUE)  
		m_strTestDisplay+="复位8051失败!\r\n"; 
	else { 
		m_strTestDisplay+="8051已经复位!\r\n"; 
		Downloadresult = TRUE; 
	} 
	UpdateData(FALSE); 
 
	CloseHandle(hDevice); 
	return Downloadresult;	 
} 
 
 
 
void CCYDEMODlg::OnButtonCtrltrans() ////控制传输 
{ 
	// TODO: Add your control notification handler code here 
	CString strmyfile = "\\VEND_AX.bix"; 
	CString strmyfilepath = ""; 
	CString strdownloadfile = ""; 
 
	char PathName[100]; 
	::GetCurrentDirectory(100,PathName); 
 
	strmyfilepath.Format("%s",PathName); 
 
	strdownloadfile = strmyfilepath + strmyfile; 
 
	LPTSTR DownloadFile = strdownloadfile.GetBuffer(100); 
 
	if (OnDownloadFirmware(DownloadFile,FALSE)) 
	{ 
		m_strTestDisplay+="\r\n--------可以进行控制传输例程测试了!!--------\r\n"; 
		UpdateData(FALSE); 
	} 
	return;		 
} 
 
void CCYDEMODlg::OnButtonInttrans() //中断传输 
{ 
	// TODO: Add your control notification handler code here 
	CString strmyfile = "\\IntTest.bix"; 
	CString strmyfilepath = ""; 
	CString strdownloadfile = ""; 
 
	char PathName[100]; 
	::GetCurrentDirectory(100,PathName); 
 
	strmyfilepath.Format("%s",PathName); 
 
	strdownloadfile = strmyfilepath + strmyfile; 
 
	LPTSTR DownloadFile = strdownloadfile.GetBuffer(100); 
 
	if (OnDownloadFirmware(DownloadFile,FALSE)) 
	{ 
		m_strTestDisplay+="\r\n--------可以进行中断传输例程测试了!!--------\r\n"; 
		UpdateData(FALSE); 
	} 
	return;	 
} 
 
void CCYDEMODlg::OnButtonIsotrans()     //Iso传输  同步传输写 
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	PUCHAR outBuffer = NULL; 
	DWORD m_nSize = 1000*1000*2; 
	unsigned long mstep,mlength; 
	double speed; 
	ISO_TRANSFER_CONTROL   outBulkControl;//inBulkControl, 
//	THREAD_CONTROL  outThreadControl;//inThreadControl, 
	HANDLE hOutDevice=NULL,hInDevice=NULL; 
//	ULONG OutPipeNum;//,InPipeNum 
	char output[256]; 
	double mTotal; 
	 
	BOOLEAN status=FALSE;//BYTE   	BOOLEAN 
	ULONG BytesReturned=0; 
	int nBytes = 0; 
	ULONG nTestCount=1; 
	 
	if (bOpenDriver (&hOutDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		m_strTestDisplay +="打开设备失败,测试中断!\r\n"; 
		UpdateData(FALSE); 
		free(outBuffer); 
		CloseHandle(hOutDevice); 
		return ; 
    } 
	else 
	{ 
		m_strTestDisplay +="\r\n-----同步传输例程-----\r\n"; 
		UpdateData(FALSE); 
	} 
	 
	outBuffer=(PUCHAR) malloc(m_nSize); 
	srand(m_nSize); 
	for(ULONG i=0;i<m_nSize;i++) 
		outBuffer[i]=rand(); 
	 
	outBulkControl.PipeNum=0;//端点选择EP2 
	outBulkControl.PacketSize =2048; 
	outBulkControl.BufferCount = 16; 
	outBulkControl.FramesPerBuffer = 8; 
	outBulkControl.PacketCount = 128;////////////////////???????????????????????????????????????????? 
	 
	mstep=GetTickCount(); //起始时间 
	status = DeviceIoControl (hOutDevice, 
		IOCTL_EZUSB_ISO_WRITE, 
		&outBulkControl, 
		sizeof(ISO_TRANSFER_CONTROL), 
		outBuffer, 
		m_nSize, 
		(unsigned long *)&nBytes, 
		NULL); 
	mlength=GetTickCount();//结束时间 
	 
	if(status==TRUE) 
	{ 
		mTotal=m_nSize;   
		mlength-=mstep; 
		if ( mlength !=0 ) speed=(mTotal/mlength)/1000.0; 
		else speed=9999999; 
		sprintf( output,"ISOspeed= %3.3fMBytes/Sec\r\nTotal= %10.2fbytes\r\nTime= %ldmS\r\n", speed, mTotal, mlength); 
		m_strTestDisplay+=output;		 
	} 
	else 
	{ 
		m_strTestDisplay+="测试失败!\r\n"; 
	} 
	 
	//关闭设备 
	free(outBuffer); 
	CloseHandle(hOutDevice); 
	UpdateData(FALSE); 
} 
 
void CCYDEMODlg::OnButtonBulktrans() //块传输 写 
{ 
	// TODO: Add your control notification handler code here 
	m_BulkTest.testDlg=this;//	m_BulkTest为CThreadParam类型 
	m_BulkTest.bUpdate=FALSE; 
	CWinThread * speedwr = AfxBeginThread(  
		BulkTransThread, // thread function  
		&m_BulkTest); // argument to thread function  
	m_BulkTest.hThread = speedwr->m_hThread; 
} 
 
 
void CCYDEMODlg::OnWrtest(LONG nTestCount)  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	PUCHAR outBuffer = NULL; 
	BULK_TRANSFER_CONTROL   outBulkControl;//inBulkControl, 
	 
	HANDLE hOutDevice=NULL; 
	char output[256]; 
 
	LONG mTotal=512;	//写的字节数,一个数据包大小为512字节 
 
	UpdateData(TRUE); 
 
	if (!OnSetOutDir())//设置输出描述 
    { 
		free(outBuffer); 
		CloseHandle(hOutDevice); 
		return ; 
    } 
 
	if (bOpenDriver (&hOutDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		CloseHandle(hOutDevice); 
		return ; 
    } 
 
	outBuffer=(PUCHAR) malloc(mTotal); 
     
	srand(mTotal); 
	for(LONG i=0;i<mTotal;i++) 
		outBuffer[i]=(BYTE)i; 
 
	BOOLEAN status=FALSE; 
	ULONG BytesReturned=0; 
	outBulkControl.pipeNum=0;//端点选择EP2 
 
	if (nTestCount>2) nTestCount=2;	//FPGA开的FIFO只有1024×16BITS 
 
	for( i=0;i<nTestCount;i++)//多次循环测试 
	{ 
		 
		status =  DeviceIoControl (hOutDevice, 
			IOCTL_EZUSB_BULK_WRITE, 
			(PVOID)&outBulkControl, 
			sizeof(BULK_TRANSFER_CONTROL), 
			outBuffer,//输出缓冲区 
			mTotal,//字节数 
			&BytesReturned,//返回字节数据 
			NULL);	 		 
	}	 
	if(status==TRUE) 
	{ 
		sprintf( output,"测试成功!\r\n共发送%03d字节", mTotal ); 
		m_strTestDisplay=output;		 
//如果FPGA中的读地址和写地址一样,还进行读操作,程序会死掉! 
		if (m_FifoWrRdNum>=1000) 
		{ 
			m_FifoWrRdNum = 1000; 
		} 
		else 
		{ 
			m_FifoWrRdNum++;//	m_FifoWrRdNum = 0;/初始化时 
 
		} 
	} 
	else 
	{ 
		m_strTestDisplay+="测试失败!\r\n"; 
	} 
	 
	//关闭设备 
	free(outBuffer); 
	CloseHandle(hOutDevice); 
	UpdateData(FALSE); 
} 
 
void CCYDEMODlg::OnRdtest(LONG nTestCount)  
{ 
	// TODO: Add your control notification handler code here 
	PUCHAR outBuffer = NULL; 
	BULK_TRANSFER_CONTROL   inBulkControl;//,outBulkControl 
	THREAD_CONTROL  inThreadControl;//,outThreadControl 
	HANDLE hInDevice=NULL; 
 
	HANDLE ReadCompleteEvent; 
//	ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL); 
 
	char output[256]; 
 
//如果FPGA中的读地址和写地址一样,还进行读操作,程序会死掉! 
	if (m_FifoWrRdNum<1) 
	{ 
		m_FifoWrRdNum=0; 
		AfxMessageBox("          还没写入数据\r\n\r\n或者写入的数据已被读走,不能再被读出!"); 
		free(outBuffer); 
		CloseHandle(hInDevice); 
		return ; 
	} 
 
	UpdateData(TRUE); 
 
	if (!OnSetInDir()) 
    { 
		free(outBuffer); 
		CloseHandle(hInDevice); 
		return ; 
    } 
 
	if (bOpenDriver (&hInDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		CloseHandle(hInDevice); 
		return ; 
    } 
 
	outBuffer=(unsigned char*)malloc(nTestCount*512*1); 
 
	inBulkControl.pipeNum=1;  //端点选择EP6 
	inThreadControl.hDevice=hInDevice; 
	inThreadControl.Ioctl=IOCTL_EZUSB_BULK_READ; 
	inThreadControl.InBuffer=(void*)&inBulkControl; 
	inThreadControl.InBufferSize=sizeof(BULK_TRANSFER_CONTROL); 
	inThreadControl.OutBuffer=outBuffer; 
	inThreadControl.OutBufferSize= 512*1; 
	inThreadControl.status=FALSE; 
	inThreadControl.BytesReturned=0; 
	inThreadControl.completionEvent=ReadCompleteEvent; 
 
	DWORD s;  
 
	for( LONG i=0;i<nTestCount;i++)//多次循环测试 
	{ 
		inThreadControl.OutBuffer=outBuffer+i*512*1; 
		WaitForSingleObject( CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)TransferThread, (LPVOID)&inThreadControl,(DWORD)0,&s),INFINITE);	 
		 
		for (int j=0;j<512;j++) 
		{ 
			Drawdatabuf[j] = outBuffer[i*512*1+j]; 
		} 
		OnDraw(); 
		Sleep(100); 
//如果FPGA中的读地址和写地址一样,还进行读操作,程序会死掉! 
		m_FifoWrRdNum--; 
	} 
 
	if(inThreadControl.status) 
	{ 
		sprintf( output,"测试成功!\r\n共接收%03d字节", nTestCount*512*1 ); 
		m_strTestDisplay=output;		 
	} 
	else 
	{ 
		m_strTestDisplay+="测试失败\r\n"; 
	} 
 
	//关闭设备 
	free(outBuffer); 
	CloseHandle(hInDevice); 
	UpdateData(FALSE); 
} 
 
 
void CCYDEMODlg::OnButtonFifo8wr()  
{ 
	// TODO: Add your control notification handler code here 
	OnWrtest(1); 
} 
 
void CCYDEMODlg::OnButtonFifo8rd()  
{ 
	// TODO: Add your control notification handler code here 
	OnRdtest(1); 
} 
 
void CCYDEMODlg::OnButtonFifo16wr()  
{ 
	// TODO: Add your control notification handler code here 
	OnWrtest(1); 
} 
 
void CCYDEMODlg::OnButtonFifo16rd()  
{ 
	// TODO: Add your control notification handler code here 
	OnRdtest(1); 
} 
 
void CCYDEMODlg::OnButtonGpifsngwr()  
{ 
	// TODO: Add your control notification handler code here 
	OnWrtest(1); 
} 
 
void CCYDEMODlg::OnButtonGpifsngrd()  
{ 
	// TODO: Add your control notification handler code here 
	OnRdtest(1); 
} 
 
void CCYDEMODlg::OnButtonGpiffifowr()  
{ 
	// TODO: Add your control notification handler code here 
	OnWrtest(1); 
} 
 
void CCYDEMODlg::OnButtonGpiffiford()  
{ 
	// TODO: Add your control notification handler code here 
	OnRdtest(1); 
} 
 
void CCYDEMODlg::OnButtonUartRx() //usb转串口接收 
{ 
	// TODO: Add your control notification handler code here 
    HANDLE  hDevice = NULL; 
    BOOLEAN bResult = FALSE; 
    int     nBytes = 0;	 
	VENDOR_OR_CLASS_REQUEST_CONTROL	myRequest; 
	unsigned char *buffer; 
	unsigned char databuffer[256]; 
	ULONG bufferSize = 0; 
	unsigned char Rxflag; 
	 
	if (bOpenDriver (&hDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		m_strTestDisplay +="打开设备失败,测试中断!\r\n"; 
		UpdateData(FALSE); 
		CloseHandle(hDevice); 
		return ; 
    } 
	else 
	{ 
		m_strTestDisplay+="\r\n---------测试中----------\r\n"; 
		UpdateData(FALSE); 
	} 
	 
	myRequest.request = 0XB4;	//接收数据命令,与固件中BOOL DR_VendorCmnd(void)的命令配对 
	myRequest.value = (USHORT) 0x0000; 
	myRequest.index = (USHORT) 0xbeef; 
	myRequest.direction = 0x01;	//in 
	bufferSize = 0x10; 
	buffer = databuffer;	 
	myRequest.requestType=0x02; // vendor specific request type (2) 
	myRequest.recepient=0x00; // recepient is device (0) 
	 
	if (hDevice != NULL)  
	{ 
		bResult = DeviceIoControl (hDevice, 
			IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST, 
			&myRequest, 
			sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL), 
			buffer, 
			bufferSize, 
			(unsigned long *)&nBytes, 
			NULL); 
	} 
	 
	if (bResult==TRUE)//?????????????????????????????????? 
	{ 
		Rxflag = buffer[0]; 
		if (Rxflag==0xaa) 
		{ 
			m_UartRx = buffer[1]; 
			UpdateData(FALSE); 
		}  
	} 
	CloseHandle(hDevice); 
} 
 
void CCYDEMODlg::OnButtonUarttx() //发送 
{ 
	// TODO: Add your control notification handler code here 
    HANDLE  hDevice = NULL; 
    BOOLEAN bResult = FALSE; 
    int		nBytes = 0;	 
	VENDOR_OR_CLASS_REQUEST_CONTROL	myRequest; 
	unsigned char *buffer; 
	unsigned char databuffer[256]; 
	ULONG bufferSize = 0; 
	BOOL	bReturnVal = FALSE; 
	 
	UpdateData(TRUE); 
	if (bOpenDriver (&hDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		m_strTestDisplay +="打开设备失败,测试中断!\r\n"; 
		UpdateData(FALSE); 
		CloseHandle(hDevice); 
		return ; 
    } 
	else 
	{ 
		m_strTestDisplay+="\r\n---------测试中----------\r\n"; 
		UpdateData(FALSE); 
	} 
	 
	myRequest.request = 0XB3;	//发送数据命令,与固件中BOOL DR_VendorCmnd(void)的命令配对 
	myRequest.value = (USHORT) m_UartTx;	//发送数据 
	myRequest.index = (USHORT) 0xbeef; 
	myRequest.direction = 0x00;	//out 
	bufferSize = 0x10; 
	buffer = databuffer;	 
	myRequest.requestType=0x02; 
	myRequest.recepient=0x00; 
	 
	if (hDevice != NULL)  
	{ 
		bResult = DeviceIoControl (hDevice, 
			IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST, 
			&myRequest, 
			sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL), 
			buffer, 
			bufferSize, 
			(unsigned long *)&nBytes, 
			NULL); 
	}	 
	if (bResult==TRUE) 
	{ 
		bReturnVal = TRUE; 
	} 
	else 
	{ 
		bReturnVal = FALSE; 
	} 
	 
	CloseHandle(hDevice); 
} 
 
void CCYDEMODlg::OnSelendokComboBaudrate() //设置波特率 
{ 
	// TODO: Add your control notification handler code here 
    HANDLE  hDevice = NULL; 
    BOOLEAN bResult = FALSE; 
    int		nBytes = 0;	 
	VENDOR_OR_CLASS_REQUEST_CONTROL	myRequest; 
	unsigned char *buffer; 
	unsigned char databuffer[256]; 
	ULONG bufferSize = 0; 
	BOOL	bReturnVal = FALSE; 
	 
	UpdateData(TRUE); 
	if (bOpenDriver (&hDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		m_strTestDisplay +="打开设备失败,测试中断!\r\n"; 
		UpdateData(FALSE); 
		CloseHandle(hDevice); 
		return ; 
    } 
	else 
	{ 
		m_strTestDisplay+="\r\n---------设置波特率----------\r\n"; 
		UpdateData(FALSE); 
	} 
	 
	int baudcs; 
	DWORD baudrateval = 9600; 
	double baudratefloat = 9600; 
	baudcs = (int)m_ctrlBaudRate.GetCurSel();//获得当前所选择的list 
	switch(baudcs) 
	{ 
	case 0: 
		baudrateval=2400; 
		break; 
	case 1: 
		baudrateval = 4800; 
		break; 
	case 2: 
		baudrateval = 9600; 
	    break; 
	case 3: 
		baudrateval = 19200; 
	    break; 
	case 4: 
		baudrateval = 38400; 
		break; 
	default: 
	    break; 
	}	 
	baudratefloat = (double)((48000000*1.0)/(32.0*baudrateval));//	 baudrateval = 9600; 
	baudrateval = (DWORD) baudratefloat; 
	baudrateval = 65536-baudrateval; 
 
	myRequest.request = 0XB2;	//发送数据命令,与固件中BOOL DR_VendorCmnd(void)的命令配对 
	myRequest.value = (USHORT) baudrateval;	//发送波特率值 
	myRequest.index = (USHORT) 0xbeef; 
	myRequest.direction = 0x00;	//out 
	bufferSize = 0x10; 
	buffer = databuffer;	 
	myRequest.requestType=0x02; 
	myRequest.recepient=0x00; 
	 
	if (hDevice != NULL)  
	{ 
		bResult = DeviceIoControl (hDevice, 
			IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST, 
			&myRequest, 
			sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL), 
			buffer, 
			bufferSize, 
			(unsigned long *)&nBytes, 
			NULL); 
	}	 
	if (bResult==TRUE) 
	{ 
		bReturnVal = TRUE; 
	} 
	else 
	{ 
		bReturnVal = FALSE; 
	} 
	 
	CloseHandle(hDevice); 
} 
 
BOOL CCYDEMODlg::OnSetInDir() 
{ 
	HANDLE  hDevice = NULL; 
    BOOLEAN bResult = FALSE; 
    int		nBytes = 0;	 
	VENDOR_OR_CLASS_REQUEST_CONTROL	myRequest; 
	unsigned char *buffer; 
	unsigned char databuffer[256]; 
	ULONG bufferSize = 0; 
	BOOL	bReturnVal = FALSE; 
	 
	UpdateData(TRUE); 
	if (bOpenDriver (&hDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		m_strTestDisplay +="打开设备失败,测试中断!\r\n"; 
		UpdateData(FALSE); 
		CloseHandle(hDevice); 
		return bReturnVal; 
    } 
	else 
	{ 
		m_strTestDisplay+="\r\n---------测试中----------\r\n"; 
		UpdateData(FALSE); 
	} 
	 
	myRequest.request = 0XBB;	//发送数据命令,与固件中BOOL DR_VendorCmnd(void)的命令配对 
	myRequest.value = (USHORT) 0x0000;	//发送数据 
	myRequest.index = (USHORT) 0xbeef; 
	myRequest.direction = 0x00;	//out 
	bufferSize = 0x10; 
	buffer = databuffer;	 
	myRequest.requestType=0x02;//vender 
	myRequest.recepient=0x00;//device 
	 
	if (hDevice != NULL)  
	{ 
		bResult = DeviceIoControl (hDevice, 
			IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST, 
			&myRequest, 
			sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL), 
			buffer, 
			bufferSize, 
			(unsigned long *)&nBytes, 
			NULL); 
	}	 
	if (bResult==TRUE) 
	{ 
		bReturnVal = TRUE; 
	} 
	else 
	{ 
		bReturnVal = FALSE; 
	} 
	 
	CloseHandle(hDevice); 
	return bReturnVal; 
} 
 
BOOL CCYDEMODlg::OnSetOutDir() 
{ 
	HANDLE  hDevice = NULL; 
    BOOLEAN bResult = FALSE; 
    int		nBytes = 0;	 
	VENDOR_OR_CLASS_REQUEST_CONTROL	myRequest; 
	unsigned char *buffer; 
	unsigned char databuffer[256]; 
	ULONG bufferSize = 0; 
	BOOL	bReturnVal = FALSE; 
	 
	UpdateData(TRUE); 
	if (bOpenDriver (&hDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
    { 
		m_strTestDisplay +="打开设备失败,测试中断!\r\n"; 
		UpdateData(FALSE); 
		CloseHandle(hDevice); 
		return bReturnVal; 
    } 
	else 
	{ 
		m_strTestDisplay+="\r\n---------测试中----------\r\n"; 
		UpdateData(FALSE); 
	} 
	 
	myRequest.request = 0XBA;	//发送数据命令,与固件中BOOL DR_VendorCmnd(void)的命令配对 
	myRequest.value = (USHORT) 0x0000;	//发送数据 
	myRequest.index = (USHORT) 0xbeef; 
	myRequest.direction = 0x00;	//out 
	bufferSize = 0x10;//16 
	buffer = databuffer;	 
	myRequest.requestType=0x02; 
	myRequest.recepient=0x00; 
	 
	if (hDevice != NULL)  
	{ 
		bResult = DeviceIoControl (hDevice, 
			IOCTL_EZUSB_VENDOR_OR_CLASS_REQUEST, 
			&myRequest, 
			sizeof(VENDOR_OR_CLASS_REQUEST_CONTROL), 
			buffer, 
			bufferSize, 
			(unsigned long *)&nBytes, 
			NULL); 
	}	 
	if (bResult==TRUE) 
	{ 
		bReturnVal = TRUE; 
	} 
	else 
	{ 
		bReturnVal = FALSE; 
	} 
	 
	CloseHandle(hDevice); 
	return bReturnVal; 
} 
 
 
void CCYDEMODlg::OnButtonDatadowm()  
{ 
	// TODO: Add your control notification handler code here 
//创建一个文件用于存放数据 
    CFileDialog FileDlg(FALSE); 
    FileDlg.m_ofn.lpstrTitle ="数据保存对话框"; 
    FileDlg.m_ofn.lpstrFilter = "DATE Files(*.dat)\0*.dat\0TEXT Files(*.txt)\0*.txt\0ALL Files(*.*)\0*.*\0\0 "; 
	FileDlg.m_ofn.lpstrDefExt="dat"; 
    if(IDOK==FileDlg.DoModal()) 
	{    
		CFile DateFile(FileDlg.GetFileName(),CFile::modeCreate|CFile::modeWrite); 
        CString  TempBuff  = "数据文件创建成功\r\n"; 
	 
        MessageBox("数据文件创建成功\n"); 
		DateFile.Write(TempBuff,strlen(TempBuff)); 
 
		 
	//开始进行数据接收 
 
		PUCHAR outBuffer = NULL; 
		BULK_TRANSFER_CONTROL   inBulkControl;//,outBulkControl 
		THREAD_CONTROL  inThreadControl;//,outThreadControl 
		HANDLE hInDevice=NULL; 
		LONG nTestCount=1; 
		HANDLE ReadCompleteEvent; 
		ULONG	nBytes=0;	 
	//	ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL); 
		g_KeepGoing=TRUE; 
		BOOL ResultReset=FALSE; 
 
		char output[256]; 
 
		UpdateData(TRUE); 
 
		if (!OnSetInDir()) 
		{ 
			free(outBuffer); 
			CloseHandle(hInDevice); 
			return ; 
		} 
 
 
		if (bOpenDriver (&hInDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE) 
		{ 
			CloseHandle(hInDevice); 
			return ; 
		} 
 
 
		outBuffer=(unsigned char*)malloc(512*1); 
     
/*		inBulkControl.pipeNum=1;//端点选择EP6 
 
		ResultReset= DeviceIoControl (hInDevice, 
									  IOCTL_Ezusb_RESETPIPE, 
									  &(inBulkControl.pipeNum), 
									  sizeof(ULONG), 
									  NULL, 
									  0, 
									  &nBytes, 
									  NULL); 
 
		if(ResultReset==TRUE) 
		{ 
        	m_strTestDisplay+="END POINT RESET\r\n"; 
		} 
		else 
		{ 
			m_strTestDisplay+="END POINT DON'T RESET\r\n"; 
			free(outBuffer); 
			CloseHandle(hInDevice); 
			return ; 
		 
		}*/ 
 
		inBulkControl.pipeNum=1; 
		inThreadControl.hDevice=hInDevice; 
		inThreadControl.Ioctl=IOCTL_EZUSB_BULK_READ; 
		inThreadControl.InBuffer=(void*)&inBulkControl; 
		inThreadControl.InBufferSize=sizeof(BULK_TRANSFER_CONTROL); 
		inThreadControl.OutBuffer=outBuffer; 
		inThreadControl.OutBufferSize=512*1; 
		inThreadControl.status=FALSE; 
		inThreadControl.BytesReturned=0; 
		inThreadControl.completionEvent=ReadCompleteEvent; 
 
		DWORD s; 
     
		int i=0; 
     	/*for( LONG i=0;i<nTestCount;i++)//多次循环测试 
		{ 
			inThreadControl.OutBuffer=outBuffer+i*512*1; 
			WaitForSingleObject( CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)TransferThread, (LPVOID)&inThreadControl,(DWORD)0,&s),INFINITE);	 
			 
			for (int j=0;j<512;j++) 
			{ 
				Drawdatabuf[j] = outBuffer[i*512*1+j]; 
			} 
			OnDraw(); 
			Sleep(100); 
			//如果FPGA中的读地址和写地址一样,还进行读操作,程序会死掉! 
		//	m_FifoWrRdNum--; 
		}*/ 
        	while(g_KeepGoing) 
		//	for( LONG i=0;i<nTestCount;i++) 
			{ 
 
			inThreadControl.OutBuffer=outBuffer+i*512*1; 
			WaitForSingleObject( CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)TransferThread, (LPVOID)&inThreadControl,(DWORD)0,&s),INFINITE); 
	 		if (inThreadControl.BytesReturned==512) 
		 
				 
			{ 
				for (int j=0;j<512;j++)  
				{ 
					Drawdatabuf[j] = outBuffer[i*512*1+j]; 
				} 
				i++; 
                nTestCount++; 
				OnDraw(); 
				Sleep(100); 
				sprintf( output,"\r\n共接收%03d字节11", inThreadControl.BytesReturned); 
				m_strTestDisplay+=output; 
				if (nTestCount<=2) 
					break; 
			} 
			 
				else 
				{ 
					i=1; 
					 
					m_strTestDisplay+="测试失败11\r\n"; 
					break; 
				} 
				 
 
				//nTestCount++; 
				//DateFile.Write(inThreadControl.OutBuffer,i*512); 
			//} 
		//	else 
		//	{  
			//	g_KeepGoing=FALSE; 
			//	break; 
		//	} 
		 
		    //	if(nTestCount>2) 
	    	//		break; 
			} 
			DateFile.Close(); 
			if(inThreadControl.status) 
				{ 
					sprintf( output,"测试成功!\r\n共接收%03d字节22222", nTestCount*512*1); 
					m_strTestDisplay+=output;		 
				} 
			else 
				{ 
     	 			m_strTestDisplay+="测试失败2222\r\n"; 
				} 
 
 
		//关闭设备 
		free(outBuffer); 
		CloseHandle(hInDevice); 
		UpdateData(FALSE); 
		 
	} 
    else 
	{ 
		MessageBox("创建文件失败!","信息提示",MB_OK); 
		return; 
	} 
} 
 
 
void CCYDEMODlg::OnButtonThannelselect()  
{ 
     
	 
} 
void CCYDEMODlg::OnButtonSingleshow()  
{ 
 
}