ReadFile API for USB device
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: ReadFile API for USB device

  1. #1
    Join Date
    Apr 2013
    Posts
    3

    ReadFile API for USB device

    Hi,
    This is my first app for USB device and I am really struggling.
    I am using Microsoft Device Simulation Framework and winDDK package to test my app.
    My app simply writes data to a file (USB pipe) and reads from it simple HDLC protocol.
    I am running my app on windows 7 64 bit version, but the app is compiled as 32 bit version.

    I am using USB handle with the OVERLAPPED structure in other words non-blocking code (provided below).
    My problem is that if I call ReadFile with RxSize set to 1024 the function just does not read anything GetOverlappedResult always returns false and my RxCount is 0 so no data has been received.
    If I use nNumberOfBytesToRead set to 256 bytes it works like charm. I have also checked MaximumPacketSize of the USB_ENDPOINT_DESCRIPTOR
    Structure and it says 1024.
    I hope that maybe somebody else has come across the same issue and might have some solution.

    Regards,
    Janusz
    Code;
    Code:
    // ----------------------------------------------------------
    // Name:        Receive
    // Description: Read RX data from the port
    // Inputs:      RxSize - Size of storage for receiving
    // Outputs:     RxBuffer - The storage
    // Returns:     Number of data bytes received
    // ----------------------------------------------------------
    int     UsbAccess::Receive (UCHAR *RxBuffer, int  RxSize)
    {
    	DWORD    ErrorFlags;
    	DWORD    RxCount = 0;
    	OVERLAPPED gOverlapped;
    	HANDLE     event;
    	DWORD dwError;
    	DWORD nBytesRead;
       // set up overlapped structure fields
       gOverlapped.Offset     = 0/*offset*/; 
       gOverlapped.OffsetHigh = 0; 
       gOverlapped.hEvent     = CreateEvent(NULL, TRUE, FALSE, NULL);
    
    	DWORD test = ::GetLastError();
        BOOL bResult = ReadFile (hRead, RxBuffer, RxSize, &RxCount, &gOverlapped);
       // if there was a problem, or the async. operation is still pending. 
       if (!bResult) 
       {
    	   // deal with the error code 
    	   switch (dwError = GetLastError()) 
    	   { 
    	   case ERROR_HANDLE_EOF: 
    		   { 
    			   // we have reached the end of the file 
    			   // during the call to ReadFile 
    
    			   // code to handle that 
    		   } 
    
    	   case ERROR_IO_PENDING: 
    		   { 
    			   // asynchronous i/o is still in progress 
    
    			   // do something else for a while 
    			   WaitForSingleObject(gOverlapped.hEvent, 1000); 
    				nBytesRead = 0;
    
    			   // check on the results of the asynchronous read 
    			   bResult = GetOverlappedResult(hRead, &gOverlapped, 
    				   &nBytesRead, FALSE) ; 
    
    			   // if there was a problem ... 
    			   if (!bResult) 
    			   { 
    				   // deal with the error code 
    				   switch (dwError = GetLastError()) 
    				   { 
    
    				   case ERROR_HANDLE_EOF: 
    					   { 
    						   // we have reached the end of
    						   // the file during asynchronous
    						   // operation
    					   } 
    
    					   // deal with other error cases 
    				   }   //end switch (dwError = GetLastError()) 
       				   BOOL result = CancelIo(hRead);
    				   offset = 0;
    				   // check on the results of the asynchronous read 
    				   bResult = GetOverlappedResult(hRead, &gOverlapped, 
    					   &nBytesRead, TRUE) ;
    				   if(GetLastError() == ERROR_OPERATION_ABORTED) {
    					   nBytesRead = 0;
    				   }
    			   } else {
    				   offset += nBytesRead;
    				   cout << "Got data";
    			   }
    			   return nBytesRead;
    		   } // end case 
    
    		   // deal with other error cases, such as the default 
    
    	   } // end switch (dwError = GetLastError()) 
       } // end if
    
    	CloseHandle(gOverlapped.hEvent);
    	test = ::GetLastError();
        return (RxCount);
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: ReadFile API for USB device

    Quote Originally Posted by Janusz View Post
    ... if I call ReadFile with RxSize set to 1024 the function just does not read anything GetOverlappedResult always returns false and my RxCount is 0 so no data has been received.
    If I use nNumberOfBytesToRead set to 256 bytes it works like charm.
    1. What is the "last error" after ReadFile "does not read anything"?
    2. What is the "last error" after GetOverlappedResult "returns false"?
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center