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

📄 usbhostfftdlg.cpp

📁 USB2.0原理与工程开发光盘(第二版)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	    pDC->LineTo ((int)m_xLineStart[i],m_InnerDHScreen);
	}

    for(i=0;i<m_yn+1;i++)
	{   	    
	    pDC->MoveTo (m_InnerLWScreen,(int)m_yLineStart[i]);
	    pDC->LineTo (m_InnerRWScreen,(int)m_yLineStart[i]);
    }
	
    pDC->SelectObject (pOldPen);
}

void CUSBHOSTFFTDlg::DrawLab(CDC *pDC,int n)
{
	char str[50];

	pDC->SetBkMode (OPAQUE);
	CFont* pOldFont=(CFont*)pDC->SelectObject(&LabelFont);
    pDC->TextOut(m_InnerLWScreen-30,m_InnerUHScreen-27,"幅度值"); 
	pDC->TextOut(m_InnerLWScreen/2-15 + m_InnerRWScreen/2,m_InnerDHScreen+ 20, "点数值");

	sprintf(str,"频率值: %5.2f",Frequence);
    pDC->TextOut(m_InnerLWScreen+300,m_InnerUHScreen-20, str);
    	
    for(int i=0;i<m_yn+1;i++)
	{   	    
		sprintf(str,"%d",i*m_yn_step);
		pDC->TextOut(m_InnerLWScreen-25,(int)m_yLineStart[i]-10,str); 
    }
	
	for(i=0;i<m_xn+1;i++)
	{   	    
		sprintf(str,"%d",i*m_xn_step);
		pDC->TextOut((int)m_xLineStart[i]-5,m_InnerDHScreen+5,str); 
    }

    sprintf(str,"通道%d频谱",n);

	pDC->SelectObject(TitleFont);
	pDC->TextOut(m_InnerLWScreen+150,m_InnerUHScreen-27,str); 

	pDC->SelectObject(pOldFont); 
}

void CUSBHOSTFFTDlg::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<256;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);
}

///////////////////////////////////////////////////////////////////
//
// 以下是CY7C68013相关函数。
//
///////////////////////////////////////////////////////////////////
BOOLEAN CUSBHOSTFFTDlg::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 CUSBHOSTFFTDlg::OnInterfaceButton() 
{
	// TODO: Add your control notification handler code here
    USB_CONFIGURATION_DESCRIPTOR output;
    USB_INTERFACE_DESCRIPTOR *output0;
	CString str;
	unsigned char output2[200];
	HANDLE hDevice = NULL;
	BOOLEAN bResult = FALSE;
	ULONG nBytes;
    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 CUSBHOSTFFTDlg::OnEndpointButton() 
{
	// TODO: Add your control notification handler code here
	USB_CONFIGURATION_DESCRIPTOR output;
	USB_ENDPOINT_DESCRIPTOR *output0;
	ULONG nBytes;
	unsigned char output2[200];
	HANDLE hDevice = NULL; 
	BOOLEAN bResult = FALSE; 
	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<4;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 CUSBHOSTFFTDlg::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 CUSBHOSTFFTDlg::OnStartButton() 
{
	HANDLE  hDevice = NULL;
	VENDOR_REQUEST_IN	myRequest;
	BOOLEAN bResult = FALSE;
	ULONG   nBytes;
	char buffer[10];

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

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

	myRequest.bRequest = 0xB3;
    myRequest.wValue = 0x0000;
    myRequest.wIndex = 0x0000;
    myRequest.wLength = 0x0001;
    myRequest.bData = 0x00;
    myRequest.direction = 0x01;
	
    bResult = DeviceIoControl (hDevice,
                           IOCTL_Ezusb_VENDOR_REQUEST,
                           &myRequest,
                           sizeof(VENDOR_REQUEST_IN),
                           buffer,
                           1,
                           &nBytes,
                           NULL);

    if (bResult != TRUE) 
    {
        AfxMessageBox("启动系统硬件失败!");
        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[2048];
	CString str;

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

		if (nBytes==2048) 
		{
		    for (int i=0;i<1024;i++)
			{	
				
                if  ((InBuffer[2*i+1]&0x30)==32)
					ADDataBuffer[i] = (InBuffer[2*i+1]&0x0F)*256+InBuffer[2*i];
                else
				{
					AfxMessageBox("发生串道错误!");
			        goto exit;                     
				}
			}
			PostMessage(hWindow,WM_DRAWPICTURE,0,0);
		}
	}
exit:
	CloseHandle(hDevice);
	g_Transfering=false;
	_endthread();	
}

void CUSBHOSTFFTDlg::OnStopButton() 
{
	// TODO: Add your control notification handler code here
	HANDLE  hDevice = NULL;
	VENDOR_REQUEST_IN	myRequest;
	BOOLEAN bResult = FALSE;
	ULONG   nBytes;
	char buffer[10];
	unsigned long input=2;


    if(g_KeepGoing)
		g_KeepGoing=false;
	else
	{
		MessageBox("还未开始数据采集!", "错误", MB_ICONERROR | MB_OK);
	    PostMessage(WM_DRAWPICTURE,0,0);
		return;
	}

	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("撤销管道2失败!"); 
		CloseHandle(hDevice);
		return;
	}
  
    myRequest.bRequest = 0xB4; 
	myRequest.wValue = 0x0000;
    myRequest.wIndex = 0x0000;
    myRequest.wLength = 0x0001; 
	myRequest.bData = 0x00;   
    myRequest.direction = 0x01;  
    bResult = DeviceIoControl (hDevice,
		                       IOCTL_Ezusb_VENDOR_REQUEST,
							   &myRequest,
                               sizeof(VENDOR_REQUEST_IN),
							   buffer, 
							   1, 
							   &nBytes, 
							   NULL);
    if (bResult != TRUE) 
	{
		AfxMessageBox("停止分析失败,请重新启动设备!"); 
		CloseHandle(hDevice); 
		return; 
	}
    CloseHandle(hDevice);
}

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

void CUSBHOSTFFTDlg::OnCustomdrawSliderfreq(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
    int nPos,Freq;
	CString str;
	nPos=m_SliderFreq.GetPos();
	if(nPos<3)
		Freq=(int)(pow(nPos,2)*10+10);
	else
        Freq=(int)((nPos-2)*100);

	str.Format("%d",Freq);
	m_Edit_Freq.SetWindowText(str);	

	*pResult = 0;
}


⌨️ 快捷键说明

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