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

📄 usbhostgraphdlg.cpp

📁 USB2.0原理与工程开发光盘(第二版)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	else
	{
		m_DoData |=0x04;
		m_bDO2=true;
		m_Check_DO2.SetWindowText("开关2 (开)");
	}	
	UsbSwitchOut();
}

void CUSBHOSTGRAPHDlg::OnCheckDo3() 
{
	// TODO: Add your control notification handler code here
	if(m_bDO3)
	{
		m_DoData &=0xF7;
		m_bDO3=false;
		m_Check_DO3.SetWindowText("开关3 (关)");
	}
	else
	{
		m_DoData |=0x08;
		m_bDO3=true;
		m_Check_DO3.SetWindowText("开关3 (开)");
	}	
	UsbSwitchOut();
}

void CUSBHOSTGRAPHDlg::OnCheckDo4() 
{
	// TODO: Add your control notification handler code here
	if(m_bDO4)
	{
		m_DoData &=0xEF;
		m_bDO4=false;
		m_Check_DO4.SetWindowText("开关4 (关)");
	}
	else
	{
		m_DoData |=0x10;
		m_bDO4=true;
		m_Check_DO4.SetWindowText("开关4 (开)");
	}	
	UsbSwitchOut();
}

void CUSBHOSTGRAPHDlg::OnCheckDo5() 
{
	// TODO: Add your control notification handler code here
	if(m_bDO5)
	{
		m_DoData &=0xDF;
		m_bDO5=false;
		m_Check_DO5.SetWindowText("开关5 (关)");
	}
	else
	{
		m_DoData |=0x20;
		m_bDO5=true;
		m_Check_DO5.SetWindowText("开关5 (开)");
	}	
	UsbSwitchOut();
}

void CUSBHOSTGRAPHDlg::OnCheckDo6() 
{
	// TODO: Add your control notification handler code here
	if(m_bDO6)
	{
		m_DoData &=0xBF;
		m_bDO6=false;
		m_Check_DO6.SetWindowText("开关6 (关)");
	}
	else
	{
		m_DoData |=0x40;
		m_bDO6=true;
		m_Check_DO6.SetWindowText("开关6 (开)");
	}	
	UsbSwitchOut();
}

void CUSBHOSTGRAPHDlg::OnCheckDo7() 
{
	// TODO: Add your control notification handler code here
	if(m_bDO7)
	{
		m_DoData &=0x7F;
		m_bDO7=false;
		m_Check_DO7.SetWindowText("开关7 (关)");
	}
	else
	{
		m_DoData |=0x80;
		m_bDO7=true;
		m_Check_DO7.SetWindowText("开关7 (开)");
	}	
	UsbSwitchOut();
}
///////////////////////////////////////////////////////////////////
//
// 以下是AN2131相关函数。
//
///////////////////////////////////////////////////////////////////

BOOLEAN CUSBHOSTGRAPHDlg::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 CUSBHOSTGRAPHDlg::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;
	    PostMessage(WM_DRAWPICTURE,0,0);
		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);
        PostMessage(WM_DRAWPICTURE,0,0);
	}    
    CloseHandle (hDevice);
}
void CUSBHOSTGRAPHDlg::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;
	    PostMessage(WM_DRAWPICTURE,0,0);
		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);          
	    PostMessage(WM_DRAWPICTURE,0,0);
    }
    CloseHandle (hDevice);
}

///////////////////////////////////////////////////////////////////
//
// 以下是处理数据采集的相关函数。
//
///////////////////////////////////////////////////////////////////
void CUSBHOSTGRAPHDlg::OnCustomdrawSliderfreq(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
    int nPos,Freq;
	nPos=m_SliderFreq.GetPos();
    Freq=(int)(pow(2,nPos)*512);
	CString str;
	str.Format("%d",Freq);
	m_Edit_Freq.SetWindowText(str);	
	*pResult = 0;
}
void CUSBHOSTGRAPHDlg::OnButtonFreq() 
{
	// TODO: Add your control notification handler code here
	
}


void CUSBHOSTGRAPHDlg::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)||(g_Transfering))
	{
		MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
		PostMessage(WM_DRAWPICTURE,0,0);
		return;
	}

	if (UsbOpenDriver (&hDevice, DeviceName) != TRUE) 
	{    	 
		MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
	    PostMessage(WM_DRAWPICTURE,0,0);
		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("开始采集失败!");
        CloseHandle(hDevice);	
	    PostMessage(WM_DRAWPICTURE,0,0);
		return;
	}  

	bResult = DeviceIoControl (hDevice,
                           IOCTL_Ezusb_RESETPIPE,
                           &input,
                           sizeof(unsigned long),
                           NULL,
                           0,
                           &nBytes,
                           NULL);

    if (bResult != TRUE) 
	{
    	AfxMessageBox("复位管道8失败!");
        CloseHandle(hDevice);	 
	    PostMessage(WM_DRAWPICTURE,0,0);
	    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[1024];
	CString str;

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

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


void CUSBHOSTGRAPHDlg::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;

    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("撤销管道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("停止采集失败!");
        CloseHandle(hDevice);	
	    return;
	}
    CloseHandle(hDevice);
}

void CUSBHOSTGRAPHDlg::OnSaveButton() 
{
	// TODO: Add your control notification handler code here
   if(g_KeepGoing)
	{
		MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
		PostMessage(WM_DRAWPICTURE,0,0);
		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[512];
			CString str;

			for(int i = 0; i < 512; i++)
			{
                pBuf[i] = (unsigned short)m_PointList[i].y;
			    str.Format(_T("[%d]:0x%x"),i,pBuf[i]);
				m_UsbList.AddString(str);
			}
	        SaveFile.Write( pBuf, 1024); 
			SaveFile.Close();
			delete pBuf;
		}
	}
	PostMessage(WM_DRAWPICTURE,0,0);
}

void CUSBHOSTGRAPHDlg::OnReplayButton() 
{
	// TODO: Add your control notification handler code here
    if(g_KeepGoing)
	{
		MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
		PostMessage(WM_DRAWPICTURE,0,0);
		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[512];
			CString str;
			
			UINT nBytesRead = OpenFile.Read( pBuf, 1024); 		

			for(int i = 0; i < 512; i++)
            {
				m_PointList[i].y=pBuf[i];
                str.Format(_T("[%d]:0x%x"),i,pBuf[i]);
				m_UsbList.AddString(str);
			}
			OpenFile.Close();
			delete pBuf;
		}
	}	
	PostMessage(WM_DRAWPICTURE,0,0);
}

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

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

void CUSBHOSTGRAPHDlg::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 CUSBHOSTGRAPHDlg::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 + -