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

📄 usbhostwavedlg.cpp

📁 USB2.0原理与工程开发光盘(第二版)
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	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 2");
			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]);
		}
	}

    CloseHandle (hDevice);	
}

void CUSBHOSTWAVEDlg::OnFrameButton() 
{
	// TODO: Add your control notification handler code here
	unsigned long  output;
    HANDLE  hDevice = NULL;
	BOOLEAN bResult = FALSE;
    ULONG   nBytes;	
	CString str;
	
    if (UsbOpenDriver (&hDevice, DeviceName) != TRUE) 
	{
		MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
		hDevice = NULL;
		return;
	}

	bResult = DeviceIoControl (hDevice,
                               IOCTL_EZUSB_GET_CURRENT_FRAME_NUMBER,
                               NULL,
                               0,
                               &output,
                               sizeof(unsigned long),
                               &nBytes,
                               NULL);
	if (bResult!=TRUE)
		MessageBox("读取当前帧号失败!", "错误", MB_ICONERROR | MB_OK);
    else 
	{
		str.Format(_T("当前帧号:%d"),output); 
        m_UsbList.AddString(str);
    }
	CloseHandle(hDevice);	
}
///////////////////////////////////////////////////////////////////
//
// 以下是DA0832相关函数。
//
///////////////////////////////////////////////////////////////////
void CUSBHOSTWAVEDlg::OnPhackleButton() 
{
	// TODO: Add your control notification handler code here    
  	for(int j=0;j<16;j++)  
	{
		for(int i=0;i<64;i++)
		{		
			m_PointList[i+64*j].x = i+64*j;
			m_PointList[i+64*j].y = i*4; 			
		}
	}
	PostMessage(WM_DRAWPICTURE,0,0);
	if(g_KeepGoing)   ChangeWave();	
}

void CUSBHOSTWAVEDlg::OnNhackleButton() 
{
	// TODO: Add your control notification handler code here
	for(int j=0;j<16;j++)
	  {
		for(int i=0;i<64;i++)
		{
			m_PointList[i+64*j].x = i+64*j;
			m_PointList[i+64*j].y =252-i*4;            
		}
	}
	PostMessage(WM_DRAWPICTURE,0,0);
	if(g_KeepGoing)   ChangeWave();		
}

void CUSBHOSTWAVEDlg::OnTriangleButton() 
{
	// TODO: Add your control notification handler code here
	for(int j=0;j<16;j++)
	  {
		for(int i=0;i<32;i++)
		{		
			m_PointList[i+64*j].x = i+64*j;
            m_PointList[i+64*j].y = i*8;
            m_PointList[i+32+64*j].x = i+32+64*j;
            m_PointList[i+32+64*j].y = 252-i*8;
		}
	  }
	PostMessage(WM_DRAWPICTURE,0,0);
	if(g_KeepGoing)   ChangeWave();	
}

void CUSBHOSTWAVEDlg::OnSquareButton() 
{
	// TODO: Add your control notification handler code here
	for(int j=0;j<16;j++)
	{
		for(int i=0;i<32;i++)
		{		
			m_PointList[i+64*j].x = i+64*j;                        
			m_PointList[i+64*j].y = 255;
			m_PointList[i+32+64*j].x = i+32+64*j;
            m_PointList[i+32+64*j].y = 0;
		}
	}
	PostMessage(WM_DRAWPICTURE,0,0);
	if(g_KeepGoing)   ChangeWave();		
}

void CUSBHOSTWAVEDlg::OnSinButton() 
{
	// TODO: Add your control notification handler code here
	for(int j=0;j<16;j++)
	{
		for(int i=0;i<64;i++)
		{		
			m_PointList[i+64*j].x = i+64*j;
			m_PointList[i+64*j].y = 128+(long)(127*sin(2*PI*i/64));
		}
	}
	PostMessage(WM_DRAWPICTURE,0,0);
	if(g_KeepGoing)   ChangeWave();	
}

void CUSBHOSTWAVEDlg::OnCustomdrawSliderfreq(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int nPos;
	nPos=m_SliderFreq.GetPos();
	CString str;	
	str.Format("%5.1f",(float)(nPos/2.0));
	m_Edit_Freq.SetWindowText(str);	
}

void CUSBHOSTWAVEDlg::OnCustomdrawSlideramp(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int nPos;
	nPos=m_SliderAmp.GetPos();
	CString str;	
	str.Format("%d",nPos*20);
	m_Edit_Amp.SetWindowText(str);
}

void CUSBHOSTWAVEDlg::OnButtonFreq() 
{
	// TODO: Add your control notification handler code here	
}

void CUSBHOSTWAVEDlg::OnButtonAmp() 
{
	// TODO: Add your control notification handler code here	
}

BOOLEAN CUSBHOSTWAVEDlg::ChangeWave() 
{
	// TODO: Add your control notification handler code here
    HANDLE  hDevice = NULL;
	BOOLEAN bResult = FALSE;
    ULONG nBytes;
    PUCHAR outBuffer = NULL;
	ULONG outBufferLength;
	ISO_TRANSFER_CONTROL 	IsoControlOut;	
    
	if (UsbOpenDriver (&hDevice, DeviceName) != TRUE) 
	{    	 
		MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
		return FALSE;
	}    
  
    IsoControlOut.PipeNum = 5;  
    IsoControlOut.BufferCount = 1;    
    IsoControlOut.FramesPerBuffer = 1;  
    IsoControlOut.PacketSize = 64;    
    IsoControlOut.PacketCount = 1;  

    outBufferLength = IsoControlOut.PacketCount * (IsoControlOut.PacketSize + sizeof(USBD_ISO_PACKET_DESCRIPTOR));
    outBuffer = (PUCHAR) malloc(outBufferLength);
   
    for (ULONG i = 0; i < (IsoControlOut.PacketCount * IsoControlOut.PacketSize); i++)
    {
         outBuffer[i] = (unsigned char)m_PointList[i].y;
    }

       bResult = DeviceIoControl (hDevice,
                             IOCTL_Ezusb_RESETPIPE,
                             &IsoControlOut.PipeNum,
                             sizeof(ULONG),
                             NULL,
                             0,
                             &nBytes,
                             NULL);

       if (bResult != TRUE)
	   {
		   AfxMessageBox("USB复位端点10失败!");
	       CloseHandle(hDevice);
            return FALSE;
	   }

       bResult = DeviceIoControl (hDevice,
                             IOCTL_EZUSB_ISO_WRITE,
                             &IsoControlOut,
                             sizeof(ISO_TRANSFER_CONTROL),
                             outBuffer,
                             outBufferLength,
                             &nBytes,
                             NULL);

	if (bResult != TRUE) 
	{
            AfxMessageBox("USB同步OUT传输失败!");
            CloseHandle(hDevice);
			return FALSE;
	}  	
  	CloseHandle(hDevice);
	return TRUE;
}


void CUSBHOSTWAVEDlg::OnSendButton() 
{
	// TODO: Add your control notification handler code here
	HANDLE  hDevice = NULL;
	BOOLEAN bResult = FALSE;
	ULONG   nBytes;
	PUCHAR  buffer;
	ULONG   bufferLength;
    ISO_TRANSFER_CONTROL 	IsoControl;

    if(g_KeepGoing)
	{
		MessageBox("系统已经工作!", "错误", MB_ICONERROR | MB_OK);
		return;
	}
    
    if(!ChangeWave())   return;    

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

	IsoControl.PipeNum = 1;      
    IsoControl.BufferCount = 1;
    IsoControl.FramesPerBuffer = 1;
	IsoControl.PacketSize = 16;  
    IsoControl.PacketCount = 1;   

	bufferLength = IsoControl.PacketCount * (IsoControl.PacketSize + sizeof(USBD_ISO_PACKET_DESCRIPTOR));
    buffer = (PUCHAR) malloc(bufferLength);

	memset(buffer, 0, bufferLength);
	*buffer=0x55;  

	bResult = DeviceIoControl (hDevice,
                               IOCTL_Ezusb_RESETPIPE,
                               &IsoControl.PipeNum,
                               sizeof(ULONG),
                               NULL,
                               0,
                               &nBytes,
                               NULL);
	
	if (bResult != TRUE) 
	{
            AfxMessageBox("USB复位端点8失败!");
			CloseHandle(hDevice);
            return;
	}     

    bResult = DeviceIoControl (hDevice,
                               IOCTL_EZUSB_ISO_WRITE,
                               &IsoControl,
                               sizeof(ISO_TRANSFER_CONTROL),
                               buffer,
                               bufferLength,
                               &nBytes,
                               NULL);

	if (bResult != TRUE) 
	{
            AfxMessageBox("开始输出波形失败!");
            CloseHandle(hDevice);
			return;
	}
	 g_KeepGoing=true;
  	CloseHandle(hDevice);
}


void CUSBHOSTWAVEDlg::OnEndButton() 
{
	// TODO: Add your control notification handler code here
	HANDLE  hDevice = NULL;
	BOOLEAN bResult = FALSE;
	ULONG   nBytes;
	PUCHAR  buffer;
	ULONG   bufferLength;
    ISO_TRANSFER_CONTROL 	IsoControl;

    g_KeepGoing = false;

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

	IsoControl.PipeNum = 3;   
    IsoControl.BufferCount = 1;
    IsoControl.FramesPerBuffer = 1;
	IsoControl.PacketSize = 16;   
    IsoControl.PacketCount = 1; 

	bufferLength = IsoControl.PacketCount * (IsoControl.PacketSize + sizeof(USBD_ISO_PACKET_DESCRIPTOR));
    buffer = (PUCHAR) malloc(bufferLength);
	memset(buffer, 0, bufferLength);
	*buffer=0xaa;

	bResult = DeviceIoControl (hDevice,
                               IOCTL_Ezusb_RESETPIPE,
                               &IsoControl.PipeNum,
                               sizeof(ULONG),
                               NULL,
                               0,
                               &nBytes,
                               NULL);
	
	if (bResult != TRUE) 
	{
            AfxMessageBox("USB复位端点9失败!");
			CloseHandle(hDevice);
            return;
	}     

    bResult = DeviceIoControl (hDevice,
                               IOCTL_EZUSB_ISO_WRITE,
                               &IsoControl,
                               sizeof(ISO_TRANSFER_CONTROL),
                               buffer,
                               bufferLength,
                               &nBytes,
                               NULL);

	if (bResult != TRUE) 
	{
            AfxMessageBox("停止输出波形失败!");
            CloseHandle(hDevice);
			return;
	}     
  	CloseHandle(hDevice);
}

void CUSBHOSTWAVEDlg::OnExitButton() 
{
	// TODO: Add your control notification handler code here
	if(g_KeepGoing)
	{
		MessageBox("系统正在工作,请首先停止输出!", "错误", MB_ICONERROR | MB_OK);
		return;
	}	
	OnOK();
}

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

⌨️ 快捷键说明

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