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

📄 testdlg.cpp

📁 可以采集数据 usb通道专用 适合初学者修改
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNSTOP, OnBtnstop)
	ON_BN_CLICKED(IDC_BTNSTART, OnBtnstart)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTNIN, OnBtnin)
	ON_BN_CLICKED(IDC_BTNOUT, OnBtnout)
	ON_BN_CLICKED(IDC_BTNBLKS, OnBtnblks)
	ON_BN_CLICKED(IDC_BTNBLKSO, OnBtnblkso)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTNCLR, OnBtnclr)
	//}}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
    //初始化设备名
	strDevname="EZUSB-0";

	m_strOutput = _T("");
	m_nSize = 1024*1024; //每次1MByte
	m_iMaxFileSize = 100;

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

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

	CloseHandle(hDevice);
	
	gStopRead=FALSE;
	gStopBulk=FALSE;

	m_ReadTest.bUpdate=FALSE;
	m_BulkTest.bUpdate=FALSE;


	SetTimer(1, 2000, 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::OnBtnstop() 
{
	m_btnStart.EnableWindow(TRUE);
	m_btnStop.EnableWindow(FALSE);
	gStopRead=TRUE;
	Sleep(1000);//等待上一个请求完成
	m_strOutput+="结束数据采集线程\r\n";
	UpdateData(FALSE);
}

void CTestDlg::OnBtnstart() 
{  
	UpdateData(TRUE);

	m_nTestPass=0;
	mTmp=m_nTestPass;

	if (!Reset_Device()) {
	   m_strOutput+="复位设备失败\r\n";
	   UpdateData(FALSE);
	   return ;
	}

	//读取数据线程
	ReadEvent = CreateEvent(0,FALSE,FALSE,NULL);//用户等待
	gStopRead=FALSE;
	m_ReadTest.testDlg=this;
    m_ReadTest.bUpdate=FALSE;
	CWinThread * testrd = AfxBeginThread( 
		ReadThread, // 线程函数 
		&m_ReadTest,
		THREAD_PRIORITY_ABOVE_NORMAL); // 线程函数变量
	m_ReadTest.hThread = testrd->m_hThread;
	m_btnStart.EnableWindow(FALSE);
	m_btnStop.EnableWindow(TRUE);

	UpdateData(FALSE);	
}

void CTestDlg::OnTimer(UINT nIDEvent) 
{
	//UpdateData(TRUE);
	double speed;
	double mtotal;
	char output[100];
	if (0) //if(m_BulkTest.bUpdate)//批量循环读写传输测试速度
	{
	    mtotal=m_nSize*2.0;
		speed;
		if ( m_ntime !=0 ) speed=mtotal/m_ntime;
		else speed=9999999.0;
		sprintf( output,"***speed = %10.2f KBytes/Sec, total=%10.2f bytes, time=%10.3f mS\r\n", speed, mtotal, m_ntime);
		m_strOutput+=output;
		m_BulkTest.bUpdate=FALSE;
		UpdateData(FALSE);   
	}
	if(m_ReadTest.bUpdate)//批量读传输速度测试
	{
		mtotal=(double)(m_nTestPass-mTmp)*(double)m_nSize;
		mTmp=m_nTestPass;
		m_nRt=GetTickCount()-nT1;
		nT1=GetTickCount();
		if ( m_nRt !=0 ) speed=mtotal/m_nRt;
		else speed=9999999.0;
		sprintf( output,"***speed = %10.2f KBytes/Sec, total=%10.2f bytes, time=%10.3f mS\r\n", speed, mtotal, m_nRt);
		m_strOutput+=output;	
		m_ReadTest.bUpdate=FALSE;
		UpdateData(FALSE);   
	}
	
//	UpdateData(FALSE);

 	
	CDialog::OnTimer(nIDEvent);
}

//写测试
void CTestDlg::OnBtnout() 
{
	UpdateData(TRUE);
	PUCHAR outBuffer = NULL;
	double speed;	
	BULK_TRANSFER_CONTROL   outBulkControl;
	HANDLE hOutDevice=NULL,hInDevice=NULL;
	char output[256];
	double mTotal;
	if (!Reset_Device()) {
		m_strOutput+="复位设备失败\r\n";
		UpdateData(FALSE);
		return;
	}
	if (bOpenDriver (&hOutDevice, strDevname.GetBuffer(strDevname.GetLength())) != TRUE)
	{
		m_strOutput+="打开设备失败\r\n";
		UpdateData(FALSE);
		return ;
	}

	outBuffer=(PUCHAR) malloc(m_nSize);
	for(ULONG i=0;i<m_nSize/2;i++)
	{
		outBuffer[2*i]=0x55;
		outBuffer[2*i+1]=0xaa;
	}

	BOOLEAN status=FALSE;
	ULONG BytesReturned=0;
	ULONG nTestCount=100;//共循环测试100次
	outBulkControl.pipeNum=0;//端点选择EP2

	LARGE_INTEGER lTime;
	LONGLONG lT1,lT2;
	double dfFreq,dfMinus, dfTime;
    //开始时间
	QueryPerformanceFrequency(&lTime);
	dfFreq=(double)lTime.QuadPart;
	QueryPerformanceCounter(&lTime);
	lT1=lTime.QuadPart;

	for(i=0;i<nTestCount;i++)//多次循环测试
	{
		status =  DeviceIoControl (hOutDevice,
						 IOCTL_EZUSB_BULK_WRITE,
						 (PVOID)&outBulkControl,
						 sizeof(BULK_TRANSFER_CONTROL),
						 outBuffer,//输出缓冲区
						 m_nSize,//字节数,在对话框中可以设置
						 &BytesReturned,//返回字节数据
						 //这里为了测试速度,没有测试返回字节数
						 NULL);	  
	}
	//结束时间
	QueryPerformanceCounter(&lTime);
	lT2=lTime.QuadPart;
	dfMinus=(double)(lT2-lT1);
	dfTime=1000.0*dfMinus/dfFreq;

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

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

//读测试
void CTestDlg::OnBtnin() 
{

	UpdateData(TRUE);

	if (!gStopRead)
	{
		m_nTestPass=0;
		mTmp=m_nTestPass;

		if (!Reset_Device()) {
		   m_strOutput+="复位设备失败\r\n";
		   UpdateData(FALSE);
		   return ;
		}

		gStopRead=TRUE;
		m_btnIn.SetWindowText("停止读测试");
		m_editMaxFileSizeCtrl.EnableWindow(FALSE);
		
		CWinThread * testrd = AfxBeginThread( 
			ContinueReadThread, // 线程函数 
			this,
			THREAD_PRIORITY_ABOVE_NORMAL); // 线程函数变量

	}
	else
	{

		gStopRead=FALSE;
	}
	UpdateData(FALSE);	


}

//启动批量测试

void CTestDlg::OnBtnblks() 
{	
	UpdateData(TRUE);
	//发送厂商请求复位FPGA
	if (!Reset_Device()) {
		 m_strOutput+="复位设备成功\r\n";
		 UpdateData(FALSE);
		 return;
	}
	gStopBulk=FALSE;
	m_BulkTest.testDlg=this;
	m_BulkTest.bUpdate=FALSE;
	CWinThread * wt = AfxBeginThread( 
	BulkThread, // thread function 
	&m_BulkTest); // argument to thread function 
	m_BulkTest.hThread = wt->m_hThread;
	m_btnBlkStart.EnableWindow(FALSE);
	m_btnBlkStop.EnableWindow(TRUE);
	m_strOutput+="启动批量测试(First Bulk OUT,Then Bulk IN)\r\n";
	UpdateData(FALSE);	

}
//	结束批量测试
void CTestDlg::OnBtnblkso() 
{
	m_btnBlkStart.EnableWindow(TRUE);
	m_btnBlkStop.EnableWindow(FALSE);
	gStopBulk=TRUE;
	m_strOutput+="结束批量测试\r\n";
	UpdateData(FALSE);
}

void CTestDlg::OnClose() 
{
	CDialog::OnClose();
}




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


//连续读数据线程
UINT ContinueReadThread(
void * pParam  // thread data
)
{

    CThreadParam*  ThreadParam;
	CTestDlg* TestDlg;
    HANDLE  hInDevice = NULL;
    PUCHAR inBuffer = NULL;
    ULONG CurrentTransferSize;
    ULONG InPipeNum;
    ULONG TestPass=0;
    ULONG Errors=0;
    //ULONG  i;
	ULONG  nCount=0; 
    BULK_TRANSFER_CONTROL   inBulkControl;
    THREAD_CONTROL inThreadControl;

	char FileName[100];//the name of saving file
	CFile DataFile;
	CTime t = CTime::GetCurrentTime();
	sprintf(FileName,"%2d-%2d-%2d-%2d-%2d.bin",
		t.GetMonth(),t.GetDay(),t.GetHour(),t.GetMinute(),t.GetSecond());

	try
	{
		DataFile.Open( FileName, CFile::modeCreate | CFile::modeWrite );
	}
	catch(...)
	{
		AfxMessageBox("Open File Error!", MB_OK);
	}

	
    //得到线程参数
	TestDlg=(CTestDlg*)pParam;

    //打开设备
    if (bOpenDriver (&hInDevice, strDevname.GetBuffer(strDevname.GetLength())) != TRUE)
    {
       TestDlg->m_strOutput+="打开设备失败,测试线程结束\r\n";
	   ThreadParam->bUpdate=TRUE;
       return 0;
    }
    //进行数据传输
    InPipeNum=1;//端点6,EP6IN
    CurrentTransferSize = m_nSize;
	//CurrentTransferSize=1024000;
    inBuffer  = (unsigned char *)malloc(CurrentTransferSize);
    // initialize the pass and error counters
    TestPass=0;
    Errors=0;

	BOOL bFirst = TRUE;
	WORD wLast = 0;

	nT1 = GetTickCount();//程序启动初始化时间

	ULONG nTestCount = 0;

    while (gStopRead&&nTestCount<TestDlg->m_iMaxFileSize)//全局变量来决定是否退出测试线程
    {
		  // initialize the in buffer
		  //memset(inBuffer, 0, CurrentTransferSize);
          // 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.status = FALSE;
          inThreadControl.BytesReturned = 0;

		  
		  inThreadControl.status = DeviceIoControl (inThreadControl.hDevice,
			  inThreadControl.Ioctl,
			  inThreadControl.InBuffer,
			  inThreadControl.InBufferSize,
			  inThreadControl.OutBuffer,
			  inThreadControl.OutBufferSize,
			  &inThreadControl.BytesReturned,
			  NULL);
	   
		   // 如果发生错误,停止数据传输
		   if ((!inThreadControl.status)||(inThreadControl.BytesReturned != CurrentTransferSize))
		   {
			  TestDlg->m_strOutput+= "Error: Read failed\r\n";
			  Errors++;
			  gStopRead = TRUE;
		   }
	
			CopyMemory(InUserRegion, inBuffer, m_nSize);
			//拷贝数据缓冲,拷贝数据将花费一段时间,可注释该语句,测试传输速度

			// 写文件
			DataFile.Write(InUserRegion, m_nSize);
			
			nTestCount ++;
			CString str;
			str.Format("%d\r\n",nTestCount);
			TestDlg->m_strOutput += str;
			TestDlg->m_ctrlOutput.SetWindowText(TestDlg->m_strOutput); 
		
			//Sleep(200);
       
	}//end while(gStopRead)

	DataFile.Close();
	//gStopRead=FALSE;
	TestDlg->m_btnIn.SetWindowText("开始读测试");
	TestDlg->m_editMaxFileSizeCtrl.EnableWindow(TRUE);

	
	TestDlg->m_nError=Errors;
	TestDlg->m_nTestPass=TestPass;

    free(inBuffer);
    CloseHandle(hInDevice);
    
	
	return 0;
	

}

⌨️ 快捷键说明

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