yorkleo
驱动牛犊
驱动牛犊
  • 注册日期2008-04-08
  • 最后登录2009-08-27
  • 粉丝0
  • 关注0
  • 积分-2分
  • 威望32点
  • 贡献值1点
  • 好评度1点
  • 原创分0分
  • 专家分0分
阅读:4406回复:3

USB 驱动使用WriteFile传数据给设备,工作一段时间后出现"配额不足,无法完成请求的服务"

楼主#
更多 发布于:2009-07-16 09:46
我这个设备有两个功能:1.获得USB 设备的按键信息{开启了一个线程,在里面不断的轮讯这个设备看数据是否有输入即ReadFile}
                                          2.向USB 设备传输信息

我不知道为什么.在写数据的时候工作一段时间后.就出现1453"配额不足,无法完成请求的服务"这个错误,


请高手指教,问题的原因是什么,我该如何解决??
void WriteOutputReport(int out_type,int num,char address1,char byte1,char byte2,char byte3,char byte4,char byte5)
{

    
    //HANDLE            hDevice;
    unsigned long    numBytesReturned;

    BOOL            bResult;
    
    OVERLAPPED        HidOverlapped;
    HANDLE            ReportEvent;

    DWORD       dwError;
    DWORD       dwErrorFlags;
    char        szError[10];
    COMSTAT     ComStat;


        Sleep(1);

    
        
        /* Create a new event for report capture */
        ReportEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
        /* fill the HidOverlapped structure so that Windows knows which
        event to cause when the device sends an IN report */
        HidOverlapped.hEvent = ReportEvent;
        HidOverlapped.Offset = 0;
        HidOverlapped.OffsetHigh = 0;

        /* Use WriteFile to send an output report to the HID device.  In this
        case we are turning on the READY LED on the target */
        
        //outbuffer[3]=1;    
        outbuffer[0]=out_type;//type
        outbuffer[1]=num;//num
        outbuffer[2]=address1;
        outbuffer[3]=byte1;
        outbuffer[4]=byte2;
        outbuffer[5]=byte3;
        outbuffer[6]=byte4;
        outbuffer[7]=byte5;
        

        

    
        bResult = WriteFile(w_handle,&outbuffer[0],w_length,&numBytesReturned,(LPOVERLAPPED) &HidOverlapped);

/*

        if   (!bResult)  
        {
              if   ((dwError=GetLastError())   ==   ERROR_IO_PENDING)  
              {  
                  while   (!GetOverlappedResult(w_handle, &HidOverlapped,&numBytesReturned,TRUE))  
                  {      
                      dwError   =   GetLastError();  
                      if(dwError   ==   ERROR_IO_INCOMPLETE)  
                      {  
                           ::MessageBox(m_hWnd,"write   io   pending",0,0);
                            continue;  
                      }  
                      else  
                       {  
                                    //an   error   occurred,   try   to   recover  
                                  wsprintf(szError,   "\n\r   <CE-%u>",dwError)   ;  
                                   ::MessageBox(m_hWnd,szError,0,0);  
                                  ClearCommError(w_handle,&dwErrorFlags,&ComStat)   ;  
                                  if   (dwErrorFlags>0)  
                                  {  
                                  wsprintf(szError,"\n\r   <CE-%u>",dwErrorFlags)   ;  
                                  ::MessageBox(m_hWnd,szError,0,0);  
                                  }  
                                      break;  
                        }
                  }
              }
              else  
              {    
                  //   some   other   error   occurred  
                  wsprintf(szError,   "\n\r   <CE-%u>",dwError);  
                  ::MessageBox(m_hWnd,szError,0,0);    
                   ClearCommError(w_handle,&dwErrorFlags,&ComStat);                  
                  if   (dwErrorFlags>0)  
                  {  
                      wsprintf(   szError,   "\n\r   <CE-%u>",   dwErrorFlags)   ;  
                      //OutputDebugString(szError);
                      ::MessageBox(m_hWnd,szError,0,0);
                  }  
                                  
                  return;  
             }  
        }
        */
        bResult = WaitForSingleObject(ReportEvent,30000);


        if(bResult == WAIT_TIMEOUT || bResult == WAIT_ABANDONED)
                {
                    //::MessageBox(m_hWnd,"!!!!!!!!",0,0);
                    CancelIo(&w_handle);


                }


        

        CloseHandle(ReportEvent);
    
}



void UsbDevice_DataInput(LPVOID lpParameter)
{


    unsigned long    numBytesReturned;

    DWORD       dwError;
    DWORD       dwErrorFlags;
    char        szError[10];
    COMSTAT     ComStat;

    BOOL            bResult;
    OVERLAPPED        HidOverlapped;
    HANDLE            ReportEvent;
    char b;

    DataInputThreadActive = TRUE;

    //bOpenHidDevice(&r_handle,VID,PID);

        /* Create a new event for report capture */
        ReportEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

        /* fill the HidOverlapped structure so that Windows knows which
        event to cause when the device sends an IN report */
        HidOverlapped.hEvent = ReportEvent;
        HidOverlapped.Offset = 0;
        HidOverlapped.OffsetHigh = 0;

    
                    

        while(endthread == FALSE)
        {

Sleep(1);
            //ClearCommError(r_handle,&dwErrorFlags,&ComStat);

            /* get a temperature report from the HID device.  Note that
            this call to ReadFile only kicks off the reading of the report.
            If the input buffer is full then this routine will cause an
            event to happen and data will be presented immediately.  If not
            the event will occur when the HID decide provides a HID report
            on the IN endpoint. */        
            bResult = ReadFile(r_handle,                            /* handle to device */
                                &inbuffer[0],                        /* IN report buffer to fill */
                                r_length,    /* input buffer size */
                                &numBytesReturned,                    /* returned buffer size */
                                (LPOVERLAPPED) &HidOverlapped );    /* long pointer to an OVERLAPPED structure */

                /*if   (!bResult)  
                {  
              if   ((dwError=GetLastError())   ==   ERROR_IO_PENDING)  
              {  

                  // ::MessageBox(m_hWnd,"write   io   pending",0,0);
                  while   (!GetOverlappedResult(r_handle,&HidOverlapped,&numBytesReturned,TRUE))  
                  {      
                      dwError   =   GetLastError();  
                      if(dwError   ==   ERROR_IO_INCOMPLETE)  
                      {  
                          
                            break;  
                      }  
                      else  
                       {  
                                
                                  ClearCommError(r_handle,&dwErrorFlags,&ComStat);                                  
                                   break;  
                        }
                  }
              }
              else  
              {    
                  //   some   other   error   occurred  
                  wsprintf(   szError,   "\n\r   <CE-%u>",dwError);  
                  ::MessageBox(m_hWnd,szError,0,0);    
                   ClearCommError(r_handle,&dwErrorFlags,&ComStat);                  
                  if   (dwErrorFlags>0)  
                  {  
                      wsprintf(szError,"\n\r   <CE-%u>",dwErrorFlags);  
                      ::MessageBox(m_hWnd,szError,0,0);
                      
                  }  
                                  
                  return ;  
             }  

                }
*/


            /* wait for IN report event.  Note that if a report does not occur
            in the time set by the 'dwMilliseconds' parameter, then this function
            returns with a FALSE result and the HID device did not provide a
            report.  If the function returns TRUE, then a report did occur and the
            data is ready to be read from the buffer specified in the ReadFile
            function call.*/

                bResult = WaitForSingleObject(     ReportEvent,
                                                20000);    
                /* if the transaction timed out, then we have to manually cancel
                the request */
                if(bResult == WAIT_TIMEOUT || bResult == WAIT_ABANDONED)
                {
                    //::MessageBox(m_hWnd,"@@@@@@@@",0,0);
                    CancelIo(&r_handle);
                    

                }
                    

                /* Process in the input data.  Note that the first byte (i.e.
                inbuffer[0] is the report ID for the HID report.  We can just
                ignore this value.  The first data byte of interest is
                inbuffer[1] */

                if(bResult == WAIT_OBJECT_0)
                {                                        
                    /* check if user pressed button  */
                if(inbuffer[1]!=00)
                    {
                        if(inbuffer[1]==0x23)
                        {
                            //::PostMessage(m_hWnd,UM_KeyUp,inbuffer[3],0);

                            SendKeyEvent(m_hWnd,inbuffer[3]);
                        }
                            if(inbuffer[1]==0x24)
                        {
                            //::PostMessage(m_hWnd,UM_KeyUp,inbuffer[3],0);

                                
                            {        b='0'+inbuffer[2];
                                    ver[0]=b;
                                    b='0'+inbuffer[3];
                                    ver[1]=b;
                                    b='0'+inbuffer[4];
                                    ver[2]=b;
                                    b='0'+inbuffer[5];
                                    ver[3]=b;
                            }
                        }
                        
                }


                
                }

        }

    
    DataInputThreadActive = FALSE;

}




游客

返回顶部