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

📄 usbhostwavedlg.cpp

📁 USB2.0原理与工程开发光盘(第二版)
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		pDC->TextOut(m_InnerLWScreen-25,(int)m_yLineStart[i]-10,str); 
    }
	
	for(i=0;i<m_xn+1;i++)
	{   	    
		sprintf(str,"%d",i*128);
		pDC->TextOut((int)m_xLineStart[i]-5,m_InnerDHScreen+5,str); 
    }

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

	pDC->SelectObject(pOldFont);  
}

void CUSBHOSTWAVEDlg::DrawCurve (CDC *pDC)
{
	int x1,y1; 		
 
    CPen* npOldPen=pDC->SelectObject (&CurvePen);
  
    pDC->SetROP2(R2_COPYPEN);   
 
    x1=(int)(((m_PointList[0].x-m_xStart)/m_XOriginScale+(float)0.5)+m_InnerLWScreen);
    y1=(int)(m_InnerDHScreen-((m_PointList[0].y-m_yStart)/m_YOriginScale+(float)0.5));

    pDC->MoveTo (x1,y1);

    for(int i=1;i<1024;i++)
    {
	   x1=(int)(((m_PointList[i].x-m_xStart)/m_XOriginScale+(float)0.5)+m_InnerLWScreen);
       y1=(int)(m_InnerDHScreen-((m_PointList[i].y-m_yStart)/m_YOriginScale+(float)0.5));
 	   pDC->LineTo (x1,y1);
	}
    pDC->SelectObject (npOldPen);
}
///////////////////////////////////////////////////////////////////
//
// 以下是AN2131相关函数。
//
///////////////////////////////////////////////////////////////////
BOOLEAN CUSBHOSTWAVEDlg::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 CUSBHOSTWAVEDlg::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 CUSBHOSTWAVEDlg::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 CUSBHOSTWAVEDlg::OnInterfaceButton() 
{
	// TODO: Add your control notification handler code here
	USB_CONFIGURATION_DESCRIPTOR output;
	unsigned char output2[200];
    USB_INTERFACE_DESCRIPTOR *output0;
    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) 		
	{
	    bResult = DeviceIoControl (hDevice,
                                   IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR,
                                   NULL,
                                   0,
                                   &output2,
                                   output.wTotalLength,
                                   &nBytes,
                                   NULL);

        if (bResult!=TRUE) 
			MessageBox("读取接口描述符失败!", "错误", MB_ICONERROR | MB_OK);
    	else
		{
			m_UsbList.AddString("USB Interface Descriptor");
			output0=(USB_INTERFACE_DESCRIPTOR *)&output2[9];
			str.Format(_T("bLength:0x%x"),output0->bLength); 
            m_UsbList.AddString(str);
		    str.Format(_T("bDescriptorType:0x%x"),output0->bDescriptorType); 
		    m_UsbList.AddString(str);
		    str.Format(_T("bInterfaceNumber:0x%x"),output0->bInterfaceNumber); 
			m_UsbList.AddString(str);
            str.Format(_T("bAlternateSetting:0x%x"),output0->bAlternateSetting);  
		    m_UsbList.AddString(str);
		    str.Format(_T("bNumEndpoints:0x%x"),output0->bNumEndpoints); 
			m_UsbList.AddString(str);
            str.Format(_T("bInterfaceClass:0x%x"),output0->bInterfaceClass);  
		    m_UsbList.AddString(str);
            str.Format(_T("bInterfaceSubClass:0x%x"),output0->bInterfaceSubClass); 
            m_UsbList.AddString(str);
            str.Format(_T("bInterfaceProtocol:0x%x"),output0->bInterfaceProtocol); 
            m_UsbList.AddString(str);
			str.Format(_T("iInterface:0x%x"),output0->iInterface); 
            m_UsbList.AddString(str);
		}
	}
	else
		MessageBox("读取接口描述符失败!", "错误", MB_ICONERROR | MB_OK);

    CloseHandle (hDevice);	
}

void CUSBHOSTWAVEDlg::OnEndpointButton() 
{
	// TODO: Add your control notification handler code here
	USB_CONFIGURATION_DESCRIPTOR output;
	unsigned char output2[200];
    USB_ENDPOINT_DESCRIPTOR *output0;
    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) 		
	{
	    bResult = DeviceIoControl (hDevice,
                                   IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR,
                                   NULL,
                                   0,
                                   &output2,
                                   output.wTotalLength,
                                   &nBytes,
                                   NULL);

        if (bResult!=TRUE) 
			MessageBox("读取端点描述符失败!", "错误", MB_ICONERROR | MB_OK);
    	else
		{
			m_UsbList.AddString("USB Endpoint Descriptor");
			for (int i=0;i<6;i++)
			{
				output0=(USB_ENDPOINT_DESCRIPTOR *)&output2[18+i*7];
				str.Format(_T("Endpoint:%d"),i+1);
				m_UsbList.AddString(str);
				str.Format(_T("bLength:0x%x"),output0->bLength);  
                m_UsbList.AddString(str);
		        str.Format(_T("bDescriptorType:0x%x"),output0->bDescriptorType);  
		        m_UsbList.AddString(str);			    
		        str.Format(_T("bEndpointAddress:0x%x"),output0->bEndpointAddress);  
			    m_UsbList.AddString(str);
                str.Format(_T("bmAttributes:0x%x"),output0->bmAttributes);  
		        m_UsbList.AddString(str);
                str.Format(_T("wMaxPacketSize:0x%x"),output0->wMaxPacketSize); 
		        m_UsbList.AddString(str);
                str.Format(_T("bInterval:0x%x"),output0->bInterval); 
		        m_UsbList.AddString(str);
				m_UsbList.AddString("");
			}
		}
	}
	else
		MessageBox("读取端点描述符失败!", "错误", MB_ICONERROR | MB_OK);

    CloseHandle (hDevice);	
}

void CUSBHOSTWAVEDlg::OnStringButton() 
{
	// TODO: Add your control notification handler code here
	HANDLE hDevice=NULL;	
	BOOL   bResult = FALSE;
	ULONG  nBytes;
	char output0[50];
	GET_STRING_DESCRIPTOR_IN input;
	USB_STRING_DESCRIPTOR    output;	
	char   tempbuff[2][256];
	CString str;

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

	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) 
	{
		MessageBox("读取字符串描述符失败!", "错误", MB_ICONERROR | MB_OK);
        CloseHandle (hDevice);
		return;
	}
    else
	{
		bResult = DeviceIoControl (hDevice,
                                   IOCTL_Ezusb_GET_STRING_DESCRIPTOR,
                                   &input,
                                   sizeof(GET_STRING_DESCRIPTOR_IN),
                                   &output0,
                                   output.bLength, 
                                   &nBytes,
                                   NULL);
		if (bResult!=TRUE) 
		{
			MessageBox("读取字符串描述符失败!", "错误", MB_ICONERROR | MB_OK);
            CloseHandle (hDevice);
		    return;
		}
		else 
		{
			m_UsbList.AddString("USB String Descriptor");			
			m_UsbList.AddString("String Descriptor 1");
			str.Format(_T("bLength:0x%x"),output0[0]); 
            m_UsbList.AddString(str);
		    str.Format(_T("bDescriptorType:0x%x"),output0[1]);  
		    m_UsbList.AddString(str);		    
	        sprintf(tempbuff[0],"bString:");
		    for (int i=0;i<output.bLength-2;i++) 
			{
				sprintf(tempbuff[1],"%c",output0[i+2]);
				strcat(tempbuff[0],tempbuff[1]);
			}
			m_UsbList.AddString((CString)tempbuff[0]);
            m_UsbList.AddString("");
		}
	}

	input.Index=2;
	input.LanguageId=0;
	bResult = DeviceIoControl (hDevice,
                               IOCTL_Ezusb_GET_STRING_DESCRIPTOR,
                               &input,
                               sizeof(GET_STRING_DESCRIPTOR_IN),
                               &output,
                               4,
                               &nBytes,
                               NULL);

⌨️ 快捷键说明

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