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

📄 usbhostdlg.cpp

📁 USB2.0原理与工程开发光盘(第一版和第二版)
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	pDC->DrawEdge(TotalRect,EDGE_SUNKEN,BF_RECT);
}

void CUsbhostDlg::DrawLab(CDC *pDC)
{	
    pDC->TextOut(m_InnerLWScreen-30,m_InnerUHScreen-27,"幅度值"); 
	pDC->TextOut(m_InnerLWScreen/2-15 + m_InnerRWScreen/2,m_InnerDHScreen+ 20, "点数值");

	char str[10];
    	
    for(int i=0;i<m_YLineStart.GetSize();i++)
	{   	    
		sprintf(str,"%5.1f",(MaxValue-MinValue)/6.0*i+MinValue);
		pDC->TextOut(m_InnerLWScreen-45,m_YLineStart[i]-10,str); 
    }
	
	for(i=0;i<m_XLineStart.GetSize();i++)
	{   	    
		sprintf(str,"%d",i*32);
		pDC->TextOut(m_XLineStart[i]-5,m_InnerDHScreen+5,str); 
    }

	pDC->TextOut(m_InnerLWScreen+150,m_InnerUHScreen-27,"波形显示"); 
}

///////////////////////////////////////////////////////////////////
//
// 以下是AN2131相关函数。
//
///////////////////////////////////////////////////////////////////
BOOLEAN CUsbhostDlg::UsbOpenDriver (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);   
}

void CUsbhostDlg::OnDeviceButton() 
{
	// TODO: Add your control notification handler code here
    USB_DEVICE_DESCRIPTOR output;
    HANDLE  hDevice = NULL;
	BOOLEAN bResult = FALSE;
    ULONG   nBytes;	
	CString str;

    if (UsbOpenDriver (&hDevice, DeviceName) != TRUE) 
	{
		MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
		hDevice = NULL;
		return;
	}

	if (hDevice != NULL)
    {
        bResult = DeviceIoControl (hDevice,
                                   IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR,
                                   NULL,
                                   0,
                                   &output,
                                   sizeof(USB_DEVICE_DESCRIPTOR),
                                   &nBytes,
                                   NULL);
    }

	if (bResult==TRUE)
    {
		m_UsbList.AddString("USB Device Descriptor");
		str.Format(_T("bLength:0x%x"),output.bLength);  
        m_UsbList.AddString(str);
		str.Format(_T("bDescriptorType:0x%x"),output.bDescriptorType); 
		m_UsbList.AddString(str);
		str.Format(_T("bcdUSB:0x%x"),output.bcdUSB); 
		m_UsbList.AddString(str);
        str.Format(_T("bDeviceClass:0x%x"),output.bDeviceClass); 
		m_UsbList.AddString(str);
		str.Format(_T("bDeviceSubClass:0x%x"),output.bDeviceSubClass);  
		m_UsbList.AddString(str);
        str.Format(_T("bDeviceProtocol:0x%x"),output.bDeviceProtocol);  
		m_UsbList.AddString(str);
        str.Format(_T("bMaxPacketSize0:0x%x"),output.bMaxPacketSize0);  
        m_UsbList.AddString(str);
        str.Format(_T("idVendor:0x%x"),output.idVendor); 
        m_UsbList.AddString(str);
		str.Format(_T("idProduct:0x%x"),output.idProduct); 
		m_UsbList.AddString(str);
		str.Format(_T("bcdDevice:0x%x"),output.bcdDevice); 
        m_UsbList.AddString(str);
		str.Format(_T("iManufacturer:0x%x"),output.iManufacturer); 
        m_UsbList.AddString(str);
		str.Format(_T("iProduct:0x%x"),output.iProduct);
        m_UsbList.AddString(str);
        str.Format(_T("iSerialNumber:0x%x"),output.iSerialNumber); 
        m_UsbList.AddString(str);
		str.Format(_T("bNumConfigurations:0x%x"),output.bNumConfigurations); 
        m_UsbList.AddString(str);		
    }
    else
		MessageBox("读取设备描述符失败!", "错误", MB_ICONERROR | MB_OK);
          
    CloseHandle (hDevice);
}

void CUsbhostDlg::OnConfigButton() 
{
	// TODO: Add your control notification handler code here
    USB_CONFIGURATION_DESCRIPTOR output;
    HANDLE  hDevice = NULL;
	BOOLEAN bResult = FALSE;
    ULONG   nBytes;
	CString str;

    if (UsbOpenDriver (&hDevice, DeviceName) != TRUE) 
	{
		MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
		hDevice = NULL;
		return;
	}

	if (hDevice != NULL)
    {
        bResult = DeviceIoControl (hDevice,
                                   IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR,
                                   NULL,
                                   0,
                                   &output,
                                   sizeof(USB_CONFIGURATION_DESCRIPTOR),
                                   &nBytes,
                                   NULL);
    }

	if (bResult==TRUE)
    {
		m_UsbList.AddString("USB Configuration Descriptor");
		str.Format(_T("bLength:0x%x"),output.bLength);  
        m_UsbList.AddString(str);
		str.Format(_T("bDescriptorType:0x%x"),output.bDescriptorType); 
		m_UsbList.AddString(str);
		str.Format(_T("wTotalLength:0x%x"),output.wTotalLength); 
		m_UsbList.AddString(str);
        str.Format(_T("bNumInterfaces:0x%x"),output.bNumInterfaces); 
		m_UsbList.AddString(str);
		str.Format(_T("bConfigurationValue:0x%x"),output.bConfigurationValue); 
		m_UsbList.AddString(str);
        str.Format(_T("iConfiguration:0x%x"),output.iConfiguration); 
		m_UsbList.AddString(str);
        str.Format(_T("bmAttributes:0x%x"),output.bmAttributes); 
        m_UsbList.AddString(str);
        str.Format(_T("MaxPower:0x%x"),output.MaxPower); 
        m_UsbList.AddString(str);
    }
    else
		MessageBox("读取配置描述符失败!", "错误", MB_ICONERROR | MB_OK);
          
    CloseHandle (hDevice);
}

void CUsbhostDlg::OnStartButton() 
{
	// TODO: Add your control notification handler code here
	HANDLE  hDevice = NULL;
	BOOLEAN bResult = FALSE;
	ULONG   nBytes;
	unsigned char outValue;   
	BULK_TRANSFER_CONTROL bulkControl;
	unsigned long input=8;

    if(g_KeepGoing)
	{
		MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
		return;
	}

	if (UsbOpenDriver (&hDevice, DeviceName) != TRUE) 
	{    	 
		MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
		return;
	}

	outValue=0x1A;
	bulkControl.pipeNum = 6;  
	bResult = DeviceIoControl (hDevice,
                              IOCTL_EZUSB_BULK_WRITE,
                              &bulkControl,
                              sizeof(BULK_TRANSFER_CONTROL),
                              &outValue,
                              1,
                              &nBytes,
                              NULL);
    if (bResult != TRUE) 
	{
        AfxMessageBox("启动AD1674失败!");
        CloseHandle(hDevice);	
		return;
	}
    
	bResult = DeviceIoControl (hDevice,
                           IOCTL_Ezusb_RESETPIPE,
                           &input,
                           sizeof(unsigned long),
                           NULL,
                           0,
                           &nBytes,
                           NULL);

    if (bResult != TRUE) 
	{
    	AfxMessageBox("复位管道8失败!");
        CloseHandle(hDevice);	  
	    return;
	}

    g_KeepGoing = true;
    if(_beginthread(ReceiveThreadFunction, 0, hDevice) < 0)
    {
		AfxMessageBox("启动接收数据线程失败!");
    }
}

void __cdecl ReceiveThreadFunction(HANDLE hDevice)
{
	BOOLEAN bResult = FALSE;
	ULONG   nBytes;
	BULK_TRANSFER_CONTROL bulkControl;
    unsigned char  InBuffer[512];
	unsigned short temp,TempMax,TempMin;
	CString str;

    while(g_KeepGoing)
	{
        nBytes = 0;
        bulkControl.pipeNum = 8;
        g_Transfering= FALSE;
		bResult = DeviceIoControl (hDevice,
	            			   IOCTL_EZUSB_BULK_READ,
							   &bulkControl,
							   sizeof(BULK_TRANSFER_CONTROL),
							   InBuffer,
							   512,
							   &nBytes,
							   NULL);
           
		if (bResult != TRUE)
		{
			 AfxMessageBox("接收数据失败!");
			 goto exit;
		}

		if (nBytes==512) 
		{
		    for (int i=0;i<256;i++)
			{
				
                temp=InBuffer[2*i]*16+InBuffer[2*i+1];
				if(temp>4096) temp=2048; 
				m_PointList[i].y = temp;
			}
			
			TempMax=TempMin=m_PointList[0].y;
            for (i=1;i<256;i++)
			{				
                if(m_PointList[i].y>TempMax)
					TempMax=m_PointList[i].y;
                else if(m_PointList[i].y<TempMin)
					TempMin=m_PointList[i].y;
			}
			
			MaxValue=TempMax+100;
			MinValue=TempMin-100;

			CRect r;
	        r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
	        InvalidateRect(hWindow,r,TRUE); 
		}
	}
exit:
	CloseHandle(hDevice);
    g_KeepGoing=false;
	_endthread();
}

void CUsbhostDlg::OnStopButton() 
{
	// TODO: Add your control notification handler code here
    HANDLE  hDevice = NULL;
	BOOLEAN bResult = FALSE;
	ULONG   nBytes;
	unsigned char outValue;   
	BULK_TRANSFER_CONTROL bulkControl;    
	unsigned long input=8;
   
	g_KeepGoing=false;
    
	if (UsbOpenDriver (&hDevice, DeviceName) != TRUE)
	{
		MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
	    return;
	}
    
	bResult = DeviceIoControl (hDevice,
                           IOCTL_Ezusb_ABORTPIPE,
                           &input,
                           sizeof(unsigned long),
                           NULL,
                           0,
                           &nBytes,
                           NULL);

    if (bResult != TRUE) 
	{
    	AfxMessageBox("撤销管道8失败!");
        CloseHandle(hDevice);
	    return;
	}

	outValue=0x2B;
	bulkControl.pipeNum = 5;   
    bResult = DeviceIoControl (hDevice,
                              IOCTL_EZUSB_BULK_WRITE,
                              &bulkControl,
                              sizeof(BULK_TRANSFER_CONTROL),
                              &outValue,
                              1,
                              &nBytes,
                              NULL);
    if (bResult != TRUE) 
	{
    	AfxMessageBox("停止AD1674失败!");
        CloseHandle(hDevice);	 
	    return;
	}
    CloseHandle(hDevice);
}

///////////////////////////////////////////////////////////////////
//
// 以下是系统运行相关函数。
//
///////////////////////////////////////////////////////////////////
void CUsbhostDlg::OnAboutButton() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

void CUsbhostDlg::OnClearlistButton() 
{
	// TODO: Add your control notification handler code here
	int count=m_UsbList.GetCount();
	
	for (int i=0;i < count;i++)
	{
		m_UsbList.DeleteString( 0 );
	}
}

void CUsbhostDlg::OnSaveButton() 
{
	// TODO: Add your control notification handler code here
    if(g_KeepGoing)
	{
		MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
		return;
	}

   	CFileDialog SaveDlg(FALSE,"uad","*.uad");
	if (SaveDlg.DoModal()==IDOK)
	{
		CFile SaveFile;
		if(!SaveFile.Open(SaveDlg.GetPathName(),CFile::modeCreate|CFile::modeWrite))
		{
			AfxMessageBox("打开文件失败!");
			return;
		}
		else
		{
			unsigned short* pBuf = new unsigned short[256];
			CString str;
			
			for(int i = 0; i < 256; i++)
			{
                pBuf[i] = m_PointList[i].y;
			    str.Format(_T("[%d]:0x%x"),i,pBuf[i]);
				m_UsbList.AddString(str);
			}

	        SaveFile.Write( pBuf, 512); 
			SaveFile.Close();
			delete pBuf;
		}
	}
}

void CUsbhostDlg::OnCleargraphButton() 
{
	// TODO: Add your control notification handler code here
    if(g_KeepGoing)
	{
		MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
		return;
	}
	
	for(int i = 0; i < 256; i++)
            m_PointList[i].y=0;

	MinValue=0;     
	MaxValue=255;

    CRect r;
	r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
	InvalidateRect(r,TRUE); 
}

void CUsbhostDlg::OnReplayButton() 
{
	// TODO: Add your control notification handler code here
    if(g_KeepGoing)
	{
		MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
		return;
	}
	
	CFileDialog OpenDlg(TRUE,"uad","*.uad");
	if (OpenDlg.DoModal()==IDOK)
	{
		CFile OpenFile;
		if(!OpenFile.Open(OpenDlg.GetPathName(),CFile::modeRead))
		{
			AfxMessageBox("打开文件失败!");
			return;
		}
		else
		{			
			unsigned short* pBuf = new unsigned short[256];
			CString str;
			
			UINT nBytesRead = OpenFile.Read( pBuf, 512); 

			for(int i = 0; i < 256; i++)
            {
				m_PointList[i].y=pBuf[i];
                str.Format(_T("[%d]:0x%x"),i,pBuf[i]);
				m_UsbList.AddString(str);
			}
            
			unsigned short TempMax,TempMin;
			TempMax=TempMin=m_PointList[0].y;
            for (i=1;i<256;i++)
			{				
                if(m_PointList[i].y>TempMax)
					TempMax=m_PointList[i].y;
                else if(m_PointList[i].y<TempMin)
					TempMin=m_PointList[i].y;
			}
			
			MaxValue=TempMax+100;
			MinValue=TempMin-100;

			CRect r;
	        r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
	        InvalidateRect(r,TRUE);  
			
			OpenFile.Close();
			delete pBuf;
		}
	}
}

void CUsbhostDlg::OnExitButton() 
{
	// TODO: Add your control notification handler code here
    if(g_KeepGoing)
	{
		MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
		return;
	}	
	OnOK();
}

⌨️ 快捷键说明

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