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

📄 testdlg.cpp

📁 CYPRESS USB CY7C68013 的速度测试的固件和上位机代码 上位机用c++
💻 CPP
字号:
          // TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"

#include <process.h>
#include <winioctl.h>
#include "ezusbsys.h"

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

char PIPE_TYPE_STRINGS[4][4] = 
{
"CTL",
"ISO",
"BLK",
"INT"
};

char PIPE_DIRECTION[2][4] = 
{
"OUT",
"IN"
};
BOOL gStopTest;

/*
purpose:
     打开设备函数
input:
     devname 设备名	 
output:
	phDeviceHandle 设备句柄
return value:
     BOOLEAN变量  成功或者失败
*/
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,
      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;
	CTestDlg* TestDlg;


    HANDLE  hInDevice = NULL;
    HANDLE  hOutDevice = NULL;
    PUCHAR inBuffer = NULL;
    PUCHAR outBuffer = NULL;
    ULONG MaxTransferSize, CurrentTransferSize;
    ULONG InPipeNum;
    ULONG OutPipeNum;
    ULONG TestPass;
    ULONG Errors;
    HANDLE ReadCompleteEvent;
    HANDLE WriteCompleteEvent;


    ULONG  i;
    BULK_TRANSFER_CONTROL   inBulkControl,outBulkControl;
    THREAD_CONTROL inThreadControl,outThreadControl;
    char tempbuff[256];
    FILE *dumpfile;//保存测试数据
    FILE *binfile;

	ThreadParam=(CThreadParam *)pParam;
	TestDlg=ThreadParam->testDlg;

    if (bOpenDriver (&hInDevice, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE)
    {
       TestDlg->m_strOutput+="打开设备失败,测试线程结束\r\n";
	   ThreadParam->bUpdate=TRUE;
       return 0;
    }
    if (bOpenDriver (&hOutDevice, TestDlg->m_strName.GetBuffer(TestDlg->m_strName.GetLength())) != TRUE)
    {
       TestDlg->m_strOutput+="打开设备失败\r\n";
	   ThreadParam->bUpdate=TRUE;
       return 0;
    }
 
    InPipeNum=2;
	OutPipeNum=0;
    sprintf(tempbuff,"IN PIPE = %d OUT PIPE = %d",InPipeNum,OutPipeNum);
	ThreadParam->bUpdate=TRUE;
	TestDlg->m_strOutput+=tempbuff;
  
    //
    // get the transfer size and allocate the transfer buffers
    //
    MaxTransferSize = TestDlg->m_nSize;
    CurrentTransferSize = MaxTransferSize;
    outBuffer = (unsigned char *)malloc(MaxTransferSize);
    inBuffer  = (unsigned char *)malloc(MaxTransferSize);
 
    //
    // seed the random number generator, and set our starting pattern
    //
    srand(TestDlg->m_nSize);
 
    //
    // initialize the pass and error counters
    //
    TestPass=0;
    Errors=0;

     //
    // Set up our event objects
    //
    ReadCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
    WriteCompleteEvent = CreateEvent(0,FALSE,FALSE,NULL);
    //
    // main test loop
    //
    while (!gStopTest)//全局变量来决定是否退出测试线程
    {
 
		   // initialize the out buffer
		   for (i=0;i<CurrentTransferSize;i++)
			   outBuffer[i] = rand();
		   // initialize the in buffer
		   memset(inBuffer, 0, MaxTransferSize);
 
          // initialize data structures for the read 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;

          // initialize data structures for the write thread
 
          outBulkControl.pipeNum = OutPipeNum;
          outThreadControl.hDevice = hOutDevice;
 
          outThreadControl.Ioctl = IOCTL_EZUSB_BULK_WRITE;
          outThreadControl.InBuffer = (PVOID)&outBulkControl;
          outThreadControl.InBufferSize = sizeof(BULK_TRANSFER_CONTROL);
          outThreadControl.OutBuffer = outBuffer;
          outThreadControl.OutBufferSize = CurrentTransferSize;
 
          outThreadControl.completionEvent = WriteCompleteEvent;
          outThreadControl.status = FALSE;
          outThreadControl.BytesReturned = 0;
    
 
       // start and wait for transfer threads
		 
	      CWinThread * rd = AfxBeginThread( 
		  TransferThread, // thread function 
		  &inThreadControl); // argument to thread function 
	      inThreadControl.hThread = rd->m_hThread;

		  CWinThread * wt = AfxBeginThread( 
		  TransferThread, // thread function 
		  &outThreadControl); // argument to thread function 
	      outThreadControl.hThread = wt->m_hThread;


          WaitForSingleObject(ReadCompleteEvent,INFINITE);	   
          WaitForSingleObject(WriteCompleteEvent,INFINITE);
 
		   // if either the read or write failed, we want to stop the test
		   if (!inThreadControl.status)
		   {
			  TestDlg->m_strOutput+= "Error: Read failed\r\n";

			  TestDlg->m_strOutput+="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);
			  gStopTest = TRUE;
		   }
		   if( !outThreadControl.status)
		   {
			  TestDlg->m_strOutput+= "Error: Write failed\r\n";
			  gStopTest = 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)
		  {
			 sprintf(tempbuff,
					  "Error: Length expected = %d actual = %d\r\n",
					  CurrentTransferSize,
					  inThreadControl.BytesReturned);
			TestDlg->m_strOutput+=tempbuff;

			 Errors++;
		//      gStopTest = TRUE;
		  }
		  // 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_strOutput+=tempbuff;
				Errors++;
       
			//       gStopTest = TRUE;
			 }
		  }
		  //如果有错误发生
		  if (Errors && (gStopTest == TRUE))
		  {
			 TestDlg->m_strOutput+="Dumping Error 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);
		  }

//		  dumpfile = fopen("dumpfile.txt","w");
//		  for (i=0;i<CurrentTransferSize;i++)
//             fprintf(dumpfile,"%X\n",outBuffer[i]);
//           fclose(dumpfile);
       // update pass counter
       TestPass++;
	   TestDlg->m_nError=Errors;
	   TestDlg->m_nTestPass=TestPass;
	   ThreadParam->bUpdate=TRUE;
	}//end while(gstoptest)


	TestDlg->m_nError=Errors;
	TestDlg->m_nTestPass=TestPass;
 
    free(inBuffer);
    free(outBuffer);
    CloseHandle(hInDevice);
    CloseHandle(hOutDevice);
    ThreadParam->bUpdate=TRUE;
	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog




CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDlg)
	m_strName = _T("");
	m_strOutput = _T("");
	m_nSize = 0;
	m_nTestPass = 0;
	m_nError = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	DDX_Text(pDX, IDC_EDTNAME, m_strName);
	DDX_Text(pDX, IDC_EDTOUTPUT, m_strOutput);
	DDX_Text(pDX, IDC_EDTSIZE, m_nSize);
	DDX_Text(pDX, IDC_EDTPASS, m_nTestPass);
	DDX_Text(pDX, IDC_EDTERROR, m_nError);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNLIST, OnBtnlist)
	ON_BN_CLICKED(IDC_BTNCLEAR, OnBtnclear)

	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTNSPEED, OnBtnspeed)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	m_strName = _T("EZUSB-0");
	m_strOutput = _T("");
	m_nSize =60000;

	HANDLE hDevice;//设备句柄
	UpdateData(FALSE);

	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength())))
	{
		m_strOutput+="打开设备失败,请检查设备名是正确\r\n";
	}
	else
	{
		m_strOutput+="打开设备成功\r\n";
	}
	CloseHandle(hDevice);
	
	

	gStopTest=FALSE;

	SetTimer(1, 1000, 0);





    UpdateData(FALSE);	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CTestDlg::OnBtnlist() 
{
   HANDLE  hDevice = NULL;
   ULONG   nBytes;
   char tempbuff[256];
   PUSBD_INTERFACE_INFORMATION pInterfaceInfo;
   PUSBD_PIPE_INFORMATION pPipeInfo;
   ULONG i;
	UCHAR gInterfaceInfo [1024];
	UpdateData(FALSE);

	if(!bOpenDriver(&hDevice,m_strName.GetBuffer(m_strName.GetLength())))
	{
		m_strOutput+="打开设备失败,请检查设备名是正确\r\n";
	}

   DeviceIoControl (hDevice,
		IOCTL_Ezusb_GET_PIPE_INFO,
		NULL,
		0,
		&gInterfaceInfo,
		sizeof(gInterfaceInfo),
		&nBytes,
		NULL);

   CloseHandle(hDevice);
	  
   pInterfaceInfo = (PUSBD_INTERFACE_INFORMATION) &gInterfaceInfo;


   m_strOutput+="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,
               pPipeInfo->EndpointAddress & 0x80 ? "IN " : "OUT",
               PIPE_TYPE_STRINGS[pPipeInfo->PipeType],
               pPipeInfo->MaximumPacketSize);
      m_strOutput+=tempbuff;
	  m_strOutput+="\r\n";

   }
   UpdateData(FALSE);
		
}

void CTestDlg::OnBtnclear() 
{
	m_strOutput="";
	UpdateData(FALSE);
	
}




void CTestDlg::OnTimer(UINT nIDEvent) 
{
	if(m_BulkTest.bUpdate)
	{
		m_BulkTest.bUpdate=FALSE;
		UpdateData(FALSE);   
	}
 	
	CDialog::OnTimer(nIDEvent);
}

void CTestDlg::OnBtnspeed() 
{
	  UpdateData(TRUE);
	  PUCHAR outBuffer = NULL;
	  unsigned long mstep,mlength;
	  double speed;	
      BULK_TRANSFER_CONTROL   outBulkControl;//inBulkControl,
 
      HANDLE hOutDevice=NULL,hInDevice=NULL;
	 
	  char output[256];
	  double mTotal;
	  if (bOpenDriver (&hOutDevice, m_strName.GetBuffer(m_strName.GetLength())) != TRUE)
	  {
		   m_strOutput+="打开设备失败\r\n";
		   UpdateData(FALSE);
		   return ;
	  }
	  outBuffer=(PUCHAR) malloc(m_nSize);
	  srand(m_nSize);
	  for(ULONG i=0;i<m_nSize;i++)
		  outBuffer[i]=rand();
      BOOLEAN status=FALSE;
	  ULONG BytesReturned=0;
      ULONG nTestCount=100;//共循环测试100次
	  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_nSize,//字节数,在对话框中可以设置,60000为较佳值
                                             &BytesReturned,//返回字节数据
											 //这里为了测试速度,没有测试返回字节数
                                             NULL);	  
	  }
      mlength=GetTickCount();//结束时间

	  if(status==TRUE)
	  {
		mTotal=m_nSize*nTestCount;  
		m_strOutput+="测试成功\r\n";
		mlength-=mstep;
		if ( mlength !=0 ) speed=mTotal/mlength;
		else speed=9999999;
		sprintf( output,"***speed = %10.2f KBytes/Sec, total=%10.2f bytes, time=%ld mS\r\n", speed, mTotal, mlength);
		m_strOutput+=output;

	  }
	  else
	  {
		  m_strOutput+="测试失败\r\n";
	  }

	  //关闭设备
	  free(outBuffer);
	  CloseHandle(hOutDevice);
	  UpdateData(FALSE);
}

⌨️ 快捷键说明

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