阅读:1438回复:1
usb传输问题求教!
我用的是D12,我的端点1,2在测试传输的时候在我的固件里面必须加上一段延时我的应用程序才能准确的收到数据,如果不加延时,我的应用程序就收不到数据或者收不全数据,我用的是周立功给的驱动,而且我发现如果我的程序不加延时,周立功的usbdebug也不能读出我的数据来,我现在要尽可能的提高传输速度,请各位大侠赐教,要多少分我就给多少
固件程序部分 if( bEPPflags.bits.ep1_rxdone) { DISABLE; bEPPflags.bits.ep1_rxdone=0; ENABLE; if(GenEpBuf[0]==1)//首先通过端点1来控制端点 1,2的读写 { for( uu=2;uu<256;uu++) { EpBuf[0]=2; EpBuf[1]=uu; EpBuf[63]=9; D12_WriteEndpoint(5,64,EpBuf); delay(70); } } else if( GenEpBuf[0]==2) { for( uu=3;uu<256;uu++) { GenEpBuf[0]=1; GenEpBuf[1]=uu; GenEpBuf[15]=8; D12_WriteEndpoint(3,16,GenEpBuf); delay(700); } } else { for( u=0;u<16;u++) GenEpBuf+=1; D12_WriteEndpoint(3,16,GenEpBuf); } } 下面是应用程序部分 ////////////////////////////////////////////////////////// UINT ReadPipe(void * pParam) { char temp [80],cc[40]; BOOLEAN bResult = FALSE; ULONG nBytes = 0; struct CThreadParam * threadParam; HANDLE hFile, hDevice; CString msg; threadParam=(struct CThreadParam *)pParam; DCDialog *testDlg=threadParam->tDialog; threadParam->bInThread = FALSE; threadParam->bUpdate = FALSE; strcpy(threadParam->pipe_name,(LPCSTR)\"PIPE00\"); strcpy(threadParam->driver_name, (LPCSTR)\"D12TEST-0\"); threadParam->uiLength=16; hFile = USB_open_file(threadParam->pipe_name); if(hFile == INVALID_HANDLE_VALUE) { strcpy(temp, \"Open driver failed! Can\'t read from \"); strcat(temp, threadParam->pipe_name); strcat(temp, \".\"); AfxMessageBox(temp); return 0; } hDevice = USB_Createfile((LPGUID) & GUID_CLASS_D12_BULK,0); if(hDevice == INVALID_HANDLE_VALUE) { CloseHandle(hDevice); strcpy(temp, \"Open device handle failed! Can\'t read from \"); strcat(temp, threadParam->pipe_name); strcat(temp, \".\"); AfxMessageBox(temp); return 0; } threadParam->bInThread = TRUE; testDlg->m_Mstart.EnableWindow(FALSE); testDlg->m_Mstop.EnableWindow(TRUE); threadParam->ulCount = 0; OVERLAPPED ol; ol.Offset=0; ol.OffsetHigh=0; ol.hEvent=CreateEvent(NULL,true,false,NULL); if(ol.hEvent==NULL) { CloseHandle(hFile); return 0; } LPOVERLAPPED lpOverlapped; lpOverlapped=&ol; threadParam->bInThread = true; threadParam->pcIoBuffer = (unsigned char *) malloc (threadParam->uiLength ); for(; ; ) { CWinThread * yt = AfxBeginThread( Ep1RecUpdate, // thread function threadParam); // argument to thread function ResetEvent(ol.hEvent); threadParam->pcIoBuffer = (unsigned char *) malloc (threadParam->uiLength ); if( threadParam->pcIoBuffer==NULL) { CloseHandle(hFile); CloseHandle(ol.hEvent); return 0; } criticalSection.Lock(); bResult = ReadFile(hFile, threadParam->pcIoBuffer, threadParam->uiLength, &nBytes, &ol); criticalSection.Unlock(); if (bResult != TRUE) { if(GetLastError()!=ERROR_IO_PENDING) { msg.Format(\"GET_PIPE01_DATA failed %x\\n\",GetLastError()); goto EXIT; } while(WaitForSingleObject(ol.hEvent,1)==WAIT_TIMEOUT) { if(!threadParam->bInThread) { CancelIo(hFile); goto EXIT; } } if(!::GetOverlappedResult(hFile,lpOverlapped,&nBytes,FALSE)) { continue; } } threadParam->ulCount+=1; Ep1Update.SetEvent(); } //The thread terminated by itself, delete resources EXIT: CloseHandle(hFile); CloseHandle(hDevice); CloseHandle(ol.hEvent); testDlg->m_Mstart.EnableWindow(TRUE); testDlg->m_Mstop.EnableWindow(FALSE); free (threadParam->pcIoBuffer); threadParam->pcIoBuffer = NULL; return 0; } 主端点读写程序 UINT ReadWrite(void *pParam) { char temp [80],cc[40]; //unsigned int i; BOOLEAN bResult = FALSE; ULONG nBytes = 0; struct CThreadParam * threadParam; HANDLE hFile, hDevice; DWORD dwError; threadParam=(struct CThreadParam *)pParam; DCDialog *testDlg=threadParam->tDialog; threadParam->bInThread = FALSE; threadParam->bUpdate = FALSE; strcpy(threadParam->pipe_name,(LPCSTR)\"PIPE02\"); strcpy(threadParam->driver_name, (LPCSTR)\"D12TEST-0\"); threadParam->uiLength=64; hFile = USB_open_file(threadParam->pipe_name); if(hFile == INVALID_HANDLE_VALUE) { strcpy(temp, \"Open driver failed! Can\'t read from \"); strcat(temp, threadParam->pipe_name); strcat(temp, \".\"); AfxMessageBox(temp); return 0; } hDevice = USB_Createfile((LPGUID)&GUID_CLASS_D12_BULK,0); if(hDevice == INVALID_HANDLE_VALUE) { CloseHandle(hDevice); strcpy(temp, \"Open device handle failed! Can\'t read from \"); strcat(temp, threadParam->pipe_name); strcat(temp, \".\"); AfxMessageBox(temp); return 0; } threadParam->bInThread = TRUE; testDlg->m_Mstart.EnableWindow(FALSE); testDlg->m_Mstop.EnableWindow(TRUE); threadParam->ulCount = 0; OVERLAPPED ol; ol.Offset=0; ol.OffsetHigh=0; ol.hEvent=CreateEvent(NULL,true,false,NULL); if(ol.hEvent==NULL) { CloseHandle(hFile); return 0; } LPOVERLAPPED lpOverlapped; lpOverlapped=&ol; threadParam->bInThread = true; threadParam->pcIoBuffer = (unsigned char *) malloc (threadParam->uiLength ); if( threadParam->pcIoBuffer==NULL) { CloseHandle(hFile); CloseHandle(ol.hEvent); return 0; } for(;;) { CWinThread * yt = AfxBeginThread( Ep2RecUpdate, // thread function threadParam); // argument to thread function ResetEvent(ol.hEvent); criticalSection.Lock(); bResult = ReadFile(hFile, threadParam->pcIoBuffer, threadParam->uiLength, &nBytes, &ol); criticalSection.Unlock(); if (bResult != TRUE) { if(GetLastError()!=ERROR_IO_PENDING) { msg.Format(\"GET_PIPE01_DATA failed %x\\n\",GetLastError()); goto EXIT; } while(WaitForSingleObject(ol.hEvent,0)==WAIT_TIMEOUT) { if(!threadParam->bInThread) { CancelIo(hFile); goto EXIT; } } if(!::GetOverlappedResult(hFile,lpOverlapped,&nBytes,TRUE)) { dwError=GetLastError(); if(dwError == ERROR_IO_INCOMPLETE) continue; } } threadParam->ulCount+=nBytes; } Ep2Update.SetEvent(); threadParam->ulCount+=1; //The thread terminated by itself, delete resources EXIT: CWinThread * yt = AfxBeginThread( Ep2RecUpdate, // thread function threadParam); // argument to thread function ResetEvent(ol.hEvent); CloseHandle(hFile); CloseHandle(hDevice); CloseHandle(ol.hEvent); testDlg->m_Mstart.EnableWindow(TRUE); testDlg->m_Mstop.EnableWindow(FALSE); free (threadParam->pcIoBuffer); threadParam->pcIoBuffer = NULL; return 0; }// |
|
最新喜欢:![]() |
沙发#
发布于:2004-04-23 17:06
自己顶一下
|
|