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

📄 usbhostsampledlg.cpp

📁 USB2.0原理与工程开发光盘(第二版)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
void CUSBHOSTSAMPLEDlg::DrawLab(CDC *pDC)
{
	pDC->SetBkMode (OPAQUE);
	CFont* pOldFont=(CFont*)pDC->SelectObject(&LabelFont);   
    pDC->TextOut(m_InnerLWScreen-30,m_InnerUHScreen-20,"通道号"); 
	pDC->TextOut(m_InnerLWScreen/2-15 + m_InnerRWScreen/2,m_InnerDHScreen+ 20, "点数值");

	char str[10];
    	
    for(int i=0;i<m_yn;i++)
	{   	    
		sprintf(str,"CH%d",7-i);
		pDC->TextOut(m_InnerLWScreen-25,(int)m_yLineStart[i]-40,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+240,m_InnerUHScreen-27,"波形显示"); 

	pDC->SelectObject(pOldFont); 
}

void CUSBHOSTSAMPLEDlg::DrawCurve (CDC *pDC)
{
	int x1,y1; 		
  
    CPen* npOldPen=pDC->SelectObject (&CurvePen);
  
    pDC->SetROP2(R2_COPYPEN);  

	int StepY=(m_InnerDHScreen-m_InnerUHScreen)/8;	
 
    for(int j=0;j<8;j++)
	{
       x1=(int)(((m_PointList[j][0].x-m_xStart)/m_XOriginScale+(float)0.5)+m_InnerLWScreen);
       y1=(int)(m_InnerDHScreen-StepY*(7-j)-((m_PointList[j][0].y-m_yStart)/m_YOriginScale+(float)0.5));

       pDC->MoveTo (x1,y1);

       for(int i=1;i<1024;i++)
	   {
	      x1=(int)(((m_PointList[j][i].x-m_xStart)/m_XOriginScale+(float)0.5)+m_InnerLWScreen);
          y1=(int)(m_InnerDHScreen-StepY*(7-j)-((m_PointList[j][i].y-m_yStart)/m_YOriginScale+(float)0.5));
 	      pDC->LineTo (x1,y1);
	   }
	} 
    pDC->SelectObject (npOldPen);
}
///////////////////////////////////////////////////////////////////
//
// 以下是CY7C68013相关函数。
//
///////////////////////////////////////////////////////////////////
BOOLEAN CUSBHOSTSAMPLEDlg::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 CUSBHOSTSAMPLEDlg::OnDeviceButton() 
{
	// TODO: Add your control notification handler code here
    USB_DEVICE_DESCRIPTOR output;
    HANDLE  hDevice = NULL;
	BOOLEAN bResult = FALSE;
    ULONG   nBytes;	
	CString str;

	// Open the driver
    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 CUSBHOSTSAMPLEDlg::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 CUSBHOSTSAMPLEDlg::OnStartButton() 
{
	// TODO: Add your control notification handler code here
    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 = 0xB5;  
    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[16384];
	CString str;

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

		if (nBytes==16384) 
		{
			if  (((InBuffer[1]&0x70)==64)&&((InBuffer[15]&0x70)==0))
			{
				for(int j=0;j<1024;j++)
				{
					for(int i=0;i<8;i++) 
						ADDataBuffer[i][j]=	(InBuffer[2*i+1+j*16]&0x0F)*256+(InBuffer[2*i+j*16]);
				}
			    PostMessage(hWindow,WM_DRAWPICTURE,0,0);
			}
			else
			{
				AfxMessageBox("发生串道错误!");
		        goto exit;
			}		
		}
		else
			{
				AfxMessageBox("返回字节数不对!");
		        goto exit;
			}
	}
exit:
	CloseHandle(hDevice);
	g_Transfering=false;
	_endthread();	
}


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

	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("撤销管道0失败!");  
		CloseHandle(hDevice); 
		return;
	}
    myRequest.bRequest = 0xB6; 
	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 CUSBHOSTSAMPLEDlg::OnExitButton() 
{
	// TODO: Add your control notification handler code here
	 if(g_KeepGoing)
	{
		MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
		return;
	}
	OnOK();		
}

void CUSBHOSTSAMPLEDlg::OnCustomdrawSliderfreq(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int nPos;
	float Freq;
	CString str;
	nPos=m_SliderFreq.GetPos();
	if(nPos<7)
	{
       Freq=SamFreq[nPos];
	   str.Format("%5.2f",Freq);
	}
	else
	{
	   str="Exterior";	
	}	
	m_Edit_Freq.SetWindowText(str);		

	*pResult = 0;
}

void CUSBHOSTSAMPLEDlg::OnAboutButton() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
	PostMessage(WM_DRAWPICTURE,0,0);
}

⌨️ 快捷键说明

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