-
April 15th, 2013, 01:48 AM
#1
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);
}
-
April 16th, 2013, 06:25 AM
#2
Re: ReadFile API for USB device
Originally Posted by Janusz
... 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.
- What is the "last error" after ReadFile "does not read anything"?
- 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|