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

📄 usbhostdlg.cpp

📁 《usb原理与工程开发》中几个开发实例的代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
void CUsbhostDlg::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 CUsbhostDlg::OnPhackleButton() 
{
	// TODO: Add your control notification handler code here
	if (BigSmall)
	{
		for(int i=0;i<256;i++)
		{
			m_PointList[i].y = (float)i;
            m_PointList[i+256].y = (float)i;
            m_PointList[i+512].y = (float)i;
            m_PointList[i+768].y = (float)i;
		}
		BigSmall=false;
	}
	else
	{
		for(int i=0;i<128;i++)
		{
			m_PointList[i].y = (float)i;
            m_PointList[i+128].y = (float)i;
            m_PointList[i+256].y = (float)i;
            m_PointList[i+384].y = (float)i;
		    m_PointList[i+512].y = (float)i;
            m_PointList[i+640].y = (float)i;
            m_PointList[i+768].y = (float)i;
			m_PointList[i+896].y = (float)i;
		}
		BigSmall=true;
	}
	index=1;	
	CRect r;
	r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
	InvalidateRect(r,TRUE);  
}

void CUsbhostDlg::OnNhackleButton() 
{
	// TODO: Add your control notification handler code here
	if (BigSmall)
	{
		for(int i=0;i<256;i++)
		{		
			m_PointList[i].y = (float)(255-i);
            m_PointList[i+256].y = (float)(255-i);
            m_PointList[i+512].y = (float)(255-i);
            m_PointList[i+768].y = (float)(255-i);
		}
		BigSmall=false;	
	}
	else
	{
		for(int i=0;i<128;i++)
		{
			m_PointList[i].y = (float)(128-i);
            m_PointList[i+128].y = (float)(128-i);
            m_PointList[i+256].y = (float)(128-i);
            m_PointList[i+384].y = (float)(128-i);
		    m_PointList[i+512].y = (float)(128-i);
            m_PointList[i+640].y = (float)(128-i);
            m_PointList[i+768].y = (float)(128-i);
			m_PointList[i+896].y = (float)(128-i);
		}
		BigSmall=true;
	}
	index=2;	
	CRect r;
	r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
	InvalidateRect(r,TRUE);  
}

void CUsbhostDlg::OnTriangleButton() 
{
	// TODO: Add your control notification handler code here
	if (BigSmall)
	{
		for(int i=0;i<256;i++)
		{
			m_PointList[i].y = (float)i;
            m_PointList[i+256].y = (float)(255-i);
            m_PointList[i+512].y = (float)i;
            m_PointList[i+768].y = (float)(255-i);
		}
		BigSmall=false;
	}
	else
	{
		for(int i=0;i<128;i++)
		{
			m_PointList[i].y = (float)i;
            m_PointList[i+128].y = (float)(128-i);
            m_PointList[i+256].y = (float)i;
            m_PointList[i+384].y = (float)(128-i);
			m_PointList[i+512].y = (float)i;
            m_PointList[i+640].y = (float)(128-i);
            m_PointList[i+768].y = (float)i;
			m_PointList[i+896].y = (float)(128-i);
		}
		BigSmall=true;
	}
	index=3;
	CRect r;
	r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
	InvalidateRect(r,TRUE);   
}

void CUsbhostDlg::OnSquareButton() 
{
	// TODO: Add your control notification handler code here
	if (BigSmall)
	{
		for(int i=0;i<256;i++)
		{		
			m_PointList[i].y = 255;
            m_PointList[i+256].y = 0;
            m_PointList[i+512].y = 255;
            m_PointList[i+768].y = 0;
		}
		BigSmall=false;
	}
	else
	{
		for(int i=0;i<128;i++)
		{		
			m_PointList[i].y = 255;
            m_PointList[i+128].y = 0;
            m_PointList[i+256].y = 255;
            m_PointList[i+384].y = 0;
			m_PointList[i+512].y = 255;
            m_PointList[i+640].y = 0;
            m_PointList[i+768].y = 255;
            m_PointList[i+896].y = 0;
		}
		BigSmall=true;
	}
	
	index=4;	
	CRect r;
	r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
	InvalidateRect(r,TRUE); 
}

void CUsbhostDlg::OnSinButton() 
{
	// TODO: Add your control notification handler code here
	if (BigSmall)
	{
		for(int i=0;i<1024;i++)
		{
			m_PointList[i].y = 127+128*(float)sin(2*PI*i/1024);
		}
		BigSmall=false;
	}
	else
	{
		for(int i=0;i<1024;i++)
		{
			m_PointList[i].y = 127+128*(float)sin(2*PI*i/512);			
		}
		BigSmall=true;
	}

	index=5;
	CRect r;
	r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
	InvalidateRect(r,TRUE);  
}

void CUsbhostDlg::OnRandomButton() 
{
	// TODO: Add your control notification handler code here
    int i=0;
	float temp;
	srand((unsigned)time(NULL));
	if (BigSmall)
	{
		while (i<1024)
		{	
			temp=(float)(rand()/100.0);
		    if(temp<=255)
			{
				m_PointList[i].y = temp;
			    i++;
			}
            BigSmall=false;
		}
	}
	else
	{
		while (i<1024)
		{	
			temp=(float)(rand()/100.0);
		    if(temp<=128)
			{
				m_PointList[i].y = temp;
			    i++;
			}
            BigSmall=true;
		}
	}
	index=6;
	CRect r;
	r.SetRect(m_InnerLWScreen , m_InnerUHScreen, m_InnerRWScreen , m_InnerDHScreen);
	InvalidateRect(r,TRUE); 
}

///////////////////////////////////////////////////////////////////
//
// 以下是USB相关函数。
//
///////////////////////////////////////////////////////////////////
void CUsbhostDlg::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;
	}  
    
	for(int i=0;i<1024;i++)
	{
		PacketoutBuffer[i]=255-(unsigned char)m_PointList[i].y;
	}
    
	if (UsbOpenDriver (&hDevice, DeviceName) != TRUE) 
	{    	 
		MessageBox("无效设备,请重试!", "错误", MB_ICONERROR | MB_OK);
		return;
	}

	IsoControl.PipeNum = 1;  
    IsoControl.BufferCount = 2;
    IsoControl.FramesPerBuffer = 8;
	IsoControl.PacketSize = 16; 
    IsoControl.PacketCount = 16;
    
	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("USB同步OUT传输失败!");
            CloseHandle(hDevice);
			return;
	}     

    g_KeepGoing = true;
    if(_beginthread(SendThreadFunction, 0, hDevice) < 0)
    {
		AfxMessageBox("启动发送数据线程失败!");
    }
}

void __cdecl SendThreadFunction(HANDLE hDevice)
{
    BOOLEAN bResult = FALSE;
    ULONG nBytes;
    PUCHAR outBuffer = NULL;
	ULONG outBufferLength;
	ISO_TRANSFER_CONTROL 	IsoControlOut;
	
    IsoControlOut.PipeNum = 5; 
    IsoControlOut.BufferCount = 2;
    IsoControlOut.FramesPerBuffer = 8;
    IsoControlOut.PacketSize = 16;
    IsoControlOut.PacketCount = 64; 
   
    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] = PacketoutBuffer[i];
    }

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

       if (bResult != TRUE)
	   {
		   AfxMessageBox("USB复位端点10失败!");
	       goto IsoTransferDone;
	   }

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


	   if (bResult != TRUE) 
	   {
           AfxMessageBox("USB同步OUT传输失败!");
	       goto IsoTransferDone;
	   }
   }

IsoTransferDone:
	CloseHandle(hDevice);
	g_KeepGoing=false;
    _endthread();  
}

void CUsbhostDlg::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 = 2;
    IsoControl.FramesPerBuffer = 8;
	IsoControl.PacketSize = 16;  
    IsoControl.PacketCount = 16; 

	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("USB同步OUT传输失败!");
            CloseHandle(hDevice);
			return;
	}     
  	CloseHandle(hDevice);
}

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

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