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

📄 testusbdlg.cpp

📁 200元买来的D12开发资料,包括上位机驱动和应用程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	char output[200];
    sprintf(output,"\r\n-----------------------------\r\nUSB_INTERFACE_DESCRIPTOR #%d\r\n", ix);
	m_strDisplay+=output;
    sprintf(output,"bLength = 0x%x\r\n", id->bLength);
	m_strDisplay+=output;
    sprintf(output,"bDescriptorType = 0x%x ( %s )\r\n", 
		id->bDescriptorType, usbDescriptorTypeString( id->bDescriptorType ));
	m_strDisplay+=output;
    sprintf(output,"bInterfaceNumber = 0x%x\r\n", id->bInterfaceNumber);
    m_strDisplay+=output;
    sprintf(output,"bAlternateSetting = 0x%x\r\n", id->bAlternateSetting);
	m_strDisplay+=output;
    sprintf(output,"bNumEndpoints = 0x%x\r\n", id->bNumEndpoints);
	m_strDisplay+=output;
    sprintf(output,"bInterfaceClass = 0x%x\r\n", id->bInterfaceClass);
	m_strDisplay+=output;
    sprintf(output,"bInterfaceSubClass = 0x%x\r\n", id->bInterfaceSubClass);
	m_strDisplay+=output;
    sprintf(output,"bInterfaceProtocol = 0x%x\r\n", id->bInterfaceProtocol);
	m_strDisplay+=output;
    sprintf(output,"bInterface = 0x%x\r\n", id->iInterface);
	m_strDisplay+=output;
}


void CTestUsbDlg::print_USB_ENDPOINT_DESCRIPTOR(PUSB_ENDPOINT_DESCRIPTOR ed, int i)
{
	char output[200];
    sprintf(output,"------------------------------\r\nUSB_ENDPOINT_DESCRIPTOR for Pipe%02d\r\n", i);
	m_strDisplay+=output;
    sprintf(output,"bLength = 0x%x\r\n", ed->bLength);
	m_strDisplay+=output;
    sprintf(output,"bDescriptorType = 0x%x ( %s )\r\n",
		ed->bDescriptorType, usbDescriptorTypeString( ed->bDescriptorType ));
	m_strDisplay+=output;
	if ( USB_ENDPOINT_DIRECTION_IN( ed->bEndpointAddress ) ) {
		sprintf(output,"bEndpointAddress= 0x%x ( INPUT )\r\n", ed->bEndpointAddress);
		m_strDisplay+=output;
	} else {
		sprintf(output,"bEndpointAddress= 0x%x ( OUTPUT )\r\n", ed->bEndpointAddress);
		m_strDisplay+=output;
	}

    sprintf(output,"bmAttributes= 0x%x ( %s )\r\n",
		ed->bmAttributes, usbEndPointTypeString ( ed->bmAttributes ));
	m_strDisplay+=output;
    sprintf(output,"wMaxPacketSize= 0x%x, decimal %d\r\n", ed->wMaxPacketSize, ed->wMaxPacketSize);
	m_strDisplay+=output;
    sprintf(output,"bInterval = 0x%x, decimal %d\r\n", ed->bInterval, ed->bInterval);
	m_strDisplay+=output;
}

void CTestUsbDlg::OnCancel() 
{
    CloseHandle(hDevice);
//	CloseHandle(hFile);
	CDialog::OnCancel();
}



void CTestUsbDlg::OnButton3() //批量写
{
	HANDLE hWriteFile;
	BOOL bResult;
	char writepipe[20];
	char output[200];
	PUCHAR pOutBuf;
	ULONG nBytes;
	DWORD time0,time1;
	ULONG current_rate;
	//用于设定缓冲区大小
	ULONG nTranfer=64;
	pOutBuf=(PUCHAR)malloc(nTranfer+16);
	for(ULONG  i=0;i<nTranfer;i++)
	{
		pOutBuf[i]=(UCHAR)(i%128);
	}
   strcpy(writepipe,LPCSTR("PIPE03"));
   hWriteFile=open_file(writepipe);
   if (hWriteFile==INVALID_HANDLE_VALUE) {
		sprintf(output,"open endpoint errror 0x%0x\r\n",GetLastError());
		m_strDisplay+=output;
	}
	else
	{
	time0 = timeGetTime();
		bResult=WriteFile(hWriteFile,
		     (PVOID)pOutBuf,
		     nTranfer,
		     &nBytes,
		NULL);
	time1 =timeGetTime();
		if (bResult) {
				m_strDisplay+="Write OK\r\n";
	        	for(ULONG i=0;i<nTranfer;i++)
				{
			      sprintf(output,"pOutBuf[%d]=%d\r\n",i,pOutBuf[i]);
			      m_strDisplay+=output;
				}
				if (time0!=time1) {
                     current_rate = nTranfer/(time1-time0);	
					 sprintf(output,"current tranfer rate:%d\r\n",current_rate);
				     m_strDisplay+=output;
				}
			
		}
		else
		{
			m_strDisplay+="Write failed and reset pipe\r\n";
			DeviceIoControl(hWriteFile,
	             	 IOCTL_D12_RESET_PIPE,
		             NULL,0,NULL,0,&nBytes,NULL);
		}
		CloseHandle(hWriteFile);
	}
	free(pOutBuf);
	pOutBuf=NULL;
	UpdateData(FALSE);
}
        //厂商请求  
		//详见固件中protodma.c的说明,共定义了3中请求
		//TANS_REQUEST 传输数据
        //GET_FIRMWARE_VERSION  得到固件版本号
        //GET_BUFFER_SIZE得到缓冲区大小
void CTestUsbDlg::OnButton4() //GET_FIRMWARE_VERSION  得到固件版本号
{
	IO_BLOCK ioBlock;
	ULONG nBytes;
	BOOL bResult;
	char output[100];
	char c=0;

	ioBlock.uOffset = 0;
	ioBlock.uLength = 1;
	ioBlock.pbyData = (PUCHAR)&c;
	ioBlock.uIndex = GET_FIRMWARE_VERSION;
	bResult = DeviceIoControl(hDevice,
			IOCTL_READ_REGISTERS,
			(PVOID)&ioBlock,
			sizeof(IO_BLOCK),
			(PVOID)&c,
			1,
			&nBytes,
			NULL);
	if (bResult) {
		sprintf(output,"The Fireware Version is= 0x%0x\r\n",c);
		m_strDisplay+=output;

	}
	else
	{
		sprintf(output,"vendor request failed.Error=0x%0x\r\n",GetLastError());
		m_strDisplay+=output;
	}
	UpdateData(FALSE);
	
}

void CTestUsbDlg::OnButton6() //TANS_REQUEST 传输数据
{
	    ULONG nBytes;
		BOOL bResult;
		IO_REQUEST ioRequest;
		IO_BLOCK ioBlock;
		char output[180];
		ioRequest.uAddressL = 0;
		ioRequest.bAddressH = 0;
		ioRequest.uSize = 64;
		ioRequest.bCommand = 0x80;	//80请求读 81请求写
		ioBlock.uOffset = 0;
		ioBlock.uLength = sizeof(IO_REQUEST);
		ioBlock.pbyData = (PUCHAR)&ioRequest;
		ioBlock.uIndex = TRANS_REQUEST;
		
		bResult = DeviceIoControl(hDevice,
				IOCTL_WRITE_REGISTERS,
				(PVOID)&ioBlock,
				sizeof(IO_BLOCK),
				NULL,
				0,
				&nBytes,
				NULL);
		if (bResult) {
			sprintf(output,"Vendor request is successful.Return Bytes=0x%0x\r\n",nBytes);
			m_strDisplay+=output;
	
		}
		else
		{
			sprintf(output,"vendor request failed.Error=0x%0x\r\n",GetLastError());
			m_strDisplay+=output;
		}
	UpdateData(FALSE);	
}

void CTestUsbDlg::OnButton5() //GET_BUFFER_SIZE得到缓冲区大小
{
	IO_BLOCK ioBlock;
	ULONG nBytes;
	BOOL bResult;
	char output[100];
	PUCHAR c;
	c=(PUCHAR)malloc(4);

	ioBlock.uOffset = 0;
	ioBlock.uLength = 4;
	ioBlock.pbyData = c;
	ioBlock.uIndex = GET_BUFFER_SIZE;
	bResult = DeviceIoControl(hDevice,
			IOCTL_READ_REGISTERS,
			(PVOID)&ioBlock,
			sizeof(IO_BLOCK),
			(PVOID)c,
			4,
			&nBytes,
			NULL);
	if (bResult) {
		sprintf(output,"Get Buffer size is 0x%0x\r\n",*c);
		m_strDisplay+=output;

	}
	else
	{
		sprintf(output,"vendor request failed.Error=0x%0x\r\n",GetLastError());
		m_strDisplay+=output;
	}
	free(c);
	c=NULL;
	UpdateData(FALSE);
	
}

void CTestUsbDlg::OnButton7() //reset device
{

	ULONG nBytes;
	BOOL bResult;
	bResult=DeviceIoControl(hDevice,
		IOCTL_D12_RESET_DEVICE,
		NULL,0,NULL,0,&nBytes,NULL);
	    if (bResult) {
		   m_strDisplay+="Reset device ok\r\n";
		}
	UpdateData(FALSE);
}
void CTestUsbDlg::OnButton8() //interrupt write
{
	HANDLE hWriteFile;
	BOOL bResult;
	char writepipe[20];
	char output[200];
	PUCHAR pOutBuf;
	ULONG nBytes;
	pOutBuf=(PUCHAR)malloc(4);
	memset(pOutBuf,0,4);
	*(pOutBuf+3) |= 0x2;//用来控制开发板子两个灯的亮与灭
	//0X1 LED1 LIGHT
	//0X2 LED2 LIGHT
	// else LED1 AND LED2 UNLIGHT
   strcpy(writepipe,LPCSTR("PIPE01"));
   hWriteFile=open_file(writepipe);
   if (hWriteFile==INVALID_HANDLE_VALUE) {
		sprintf(output,"open PIPE01 errror 0x%0x\r\n",GetLastError());
		m_strDisplay+=output;
	}
	else
	{
		bResult=WriteFile(hWriteFile,
		     (PVOID)pOutBuf,
		     4,
		     &nBytes,
		NULL);
		if (bResult) {
				m_strDisplay+="Write OK\r\n";
		}
		else
		{
			m_strDisplay+="Write failed and reset pipe\r\n";
			DeviceIoControl(hWriteFile,
	             	 IOCTL_D12_RESET_PIPE,
		             NULL,0,NULL,0,&nBytes,NULL);
		}
		CloseHandle(hWriteFile);
	}
	free(pOutBuf);
	pOutBuf=NULL;
	UpdateData(FALSE);	
}

void CTestUsbDlg::OnTimer(UINT nIDEvent) 
{
	if(m_InterruptIn.ulData[0] & D12_KEYSTATUS)
	{
			m_InterruptIn.ulData[0] &= (!D12_KEYSTATUS);
			if(!m_InterruptIn.ulData[2])
				m_keystatus = _T("Key pressed.");
			else
				m_keystatus = _T("Key released.");
	}
	if (m_InterruptIn.bUpdate == TRUE) {	
		UpdateData(FALSE);
		m_InterruptIn.bUpdate=FALSE;
	}
	CDialog::OnTimer(nIDEvent);
}

void CTestUsbDlg::OnButton9() 
//一个完整传输测试,先写后读
//这里读写都是通过用户定义的厂商请求(请求传输)发出的
{
	char output[200];
	ULONG nBytes;
	BOOL bResult;
	IO_REQUEST ioRequest;
	IO_BLOCK ioBlock;

	HANDLE hWriteFile,hReadFile;
	char writepipe[20],readpipe[20];
	PUCHAR pOutBuf,pInBuf;
	ioRequest.uAddressL = 0;
	ioRequest.bAddressH = 0;
	ioRequest.uSize = 64;
	ioRequest.bCommand = 0x81;	//80请求读 81请求写
	ioBlock.uOffset = 0;
	ioBlock.uLength = sizeof(IO_REQUEST);
	ioBlock.pbyData = (PUCHAR)&ioRequest;
	ioBlock.uIndex = TRANS_REQUEST;
	//请求写
	bResult = DeviceIoControl(hDevice,
			IOCTL_WRITE_REGISTERS,
			(PVOID)&ioBlock,
			sizeof(IO_BLOCK),
			NULL,
			0,
			&nBytes,
			NULL);
	if (bResult) {
		sprintf(output,"request-write is successful.Return Bytes=0x%0x\r\n",nBytes);
		m_strDisplay+=output;
	}
	else
	{
		sprintf(output,"request-write failed.Error=0x%0x\r\n",GetLastError());
		m_strDisplay+=output;
	}
    //写操作
	pOutBuf=(PUCHAR)malloc(64+16);
	for(UCHAR  i=0;i<64;i++)
	{
		pOutBuf[i]=i;
	}
   strcpy(writepipe,LPCSTR("PIPE03"));
   hWriteFile=open_file(writepipe);
   if (hWriteFile==INVALID_HANDLE_VALUE) {
		sprintf(output,"open PIPE03 error 0x%0x\r\n",GetLastError());
		m_strDisplay+=output;
	}
	else
	{
		bResult=WriteFile(hWriteFile,
		     (PVOID)pOutBuf,
		     64,
		     &nBytes,
		NULL);
		if (bResult) {
				m_strDisplay+="Write OK\r\n";
	        	for(ULONG i=0;i<64;i++)
				{
			      sprintf(output,"pOutBuf[%d]=%d\r\n",i,pOutBuf[i]);
			      m_strDisplay+=output;
				}
		}
		else
		{
			m_strDisplay+="Write failed and reset pipe\r\n";
			DeviceIoControl(hWriteFile,
	             	 IOCTL_D12_RESET_PIPE,
		             NULL,0,NULL,0,&nBytes,NULL);
		}
	}
	//请求读
	ioRequest.uAddressL = 0;
	ioRequest.bAddressH = 0;
	ioRequest.uSize = 64;
	ioRequest.bCommand = 0x80;	//80请求读 81请求写
	ioBlock.uOffset = 0;
	ioBlock.uLength = sizeof(IO_REQUEST);
	ioBlock.pbyData = (PUCHAR)&ioRequest;
	ioBlock.uIndex = TRANS_REQUEST;
	bResult = DeviceIoControl(hDevice,
			IOCTL_WRITE_REGISTERS,
			(PVOID)&ioBlock,
			sizeof(IO_BLOCK),
			NULL,
			0,
			&nBytes,
			NULL);
	if (bResult) {
		sprintf(output,"request-read is successful. Return Bytes=0x%0x\r\n",nBytes);
		m_strDisplay+=output;
	}
	else
	{
		sprintf(output,"request-read failed. Error=0x%0x\r\n",GetLastError());
		m_strDisplay+=output;
	}
	//读操作
	pInBuf=(PUCHAR)malloc(64+16);
	memset(pInBuf,0,64);
   strcpy(readpipe,LPCSTR("PIPE02"));
   hReadFile=open_file(readpipe);
   if (hReadFile==INVALID_HANDLE_VALUE) {
		sprintf(output,"open PIPE02 error 0x%0x\r\n",GetLastError());
		m_strDisplay+=output;
	}
	else
	{
		bResult=ReadFile(hReadFile,
		     (PVOID)pInBuf,
		     64,
		     &nBytes,
		NULL);
		if (bResult) {
				m_strDisplay+="Read OK\r\n";
	        	for(ULONG i=0;i<64;i++)
				{
			      sprintf(output,"pInBuf[%d]=%d\r\n",i,pInBuf[i]);
			      m_strDisplay+=output;
				}
		}
		else
		{
			m_strDisplay+="Read failed and reset pipe\r\n";
			DeviceIoControl(hReadFile,
	             	 IOCTL_D12_RESET_PIPE,
		             NULL,0,NULL,0,&nBytes,NULL);
		}
		
	}
    CloseHandle(hWriteFile);//close pipe
    CloseHandle(hReadFile); //close pipe
	free(pInBuf);
	free(pOutBuf);
	pInBuf=NULL;
	pOutBuf=NULL;
	UpdateData(FALSE);	
}

void CTestUsbDlg::OnButton10() //清空
{
	m_strDisplay="";
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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