-
Is it possible to use standard win32 GetOverlappedResult (instead of WinUsb_GetOverlappedResult) and Completion Ports on WinUsb file handle?
-
What is the difference between GetOverlappedResult and WinUsb_GetOverlappedResult?
-
Yes, you can use GetOverlappedResult on the HANDLE that you received when calling CreateFile on a WinUSB controlled device. This means you can also use an io completion port. You cannot use the WINUSB_INTERFACE_HANDLE that you get from calling WinUsb_Initialize as a file handle though, which means you cannot pass it to either GetOverlappedResult or as a parameter to the io completion port.
-
WinUsb_GetOVerlappedResult takes a WINUSB_INTERFACE_HANDLE, GetOverlappedResult takes a HANDLE. They are completely 2 different handles and are not cross compatible.
To be able to use winusb with an io completion port you must provide an OVERLAPPED structurt to WinUsb_Read/WritePipe.
d
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@srce.hr
Sent: Tuesday, October 30, 2007 6:36 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Using GetOverlappedResult and Completion Ports with WinUSB
-
Is it possible to use standard win32 GetOverlappedResult (instead of WinUsb_GetOverlappedResult) and Completion Ports on WinUsb file handle?
-
What is the difference between GetOverlappedResult and WinUsb_GetOverlappedResult?
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
I am a little bit confused… Does it mean that when using standard win32 GetOverlappedResult, I have to use standard ReadFile(Ex)/WriteFile(Ex) methods or WinUsb_ReadPipe/WinUsb_WritePipe functions?
You cannot use Write/ReadFileEx, winusb does not support reads and writes through these APIs. You must use WinUsb_
Write/ReadPipe to communicate with the device. the winusb APIs are compatible with io completion ports as long as you pass an OVERLAPPED structure to them. If you pass NULL, these 2 winusb apis will send the io synchronously.
d
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@srce.hr
Sent: Tuesday, October 30, 2007 10:03 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Using GetOverlappedResult and Completion Ports with WinUSB
I am a little bit confused… Does it mean that when using standard win32 GetOverlappedResult, I have to use standard ReadFile(Ex)/WriteFile(Ex) methods or WinUsb_ReadPipe/WinUsb_WritePipe functions?
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
Hi, first of all, sorry for my english, this is the code that I use, with a Microchip PIC 18f2550 and WinUSB, some extract:
void InicializarAsyncU (tAsyncRead *Async, int num)
{
Async->Pipe = num;
Async->oOver.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
memset(Async->buffer, ‘\0’, sizeof(Async->buffer));
Async->Espera = 0;
Async->bytesRead = 0;
}
int __stdcall LeerTuboA (HANDLE usbHandle, tAsyncRead *Async)
{
BOOL bResultado = FALSE;
//Si el pipe esta a cero, leemos, significa que no hay operaciones pendientes
if(Async->Espera == 0)
{
bResultado = WinUsb_ReadPipe(usbHandle, Async->Pipe, (PUCHAR) Async->buffer, 64, &Async->bytesRead, &Async->oOver);
if(bResultado == 0)
{
Async->Espera = 1;
return 0;
}
else
return 1;
}
//Si el pipe no esta a cero, significa que hay una operacion pendiente, devolvemos -1
else
{
return -1;
}
return 1;
}
int __stdcall WaitAsync(HANDLE usbHandle, tAsyncRead *Async)
{
BOOL bResultado = FALSE;
if(Async->Espera == 1)
{
if(WaitForSingleObject(Async->oOver.hEvent, 0) == WAIT_OBJECT_0)
//Termino, por lo tanto guardamos los valores de lectura del buffer
//y reiniciamos el proceso poniendo pipes a 0
{
GetOverlappedResult(usbHandle, &Async->oOver, &Async->bytesRead, FALSE); //Consultamos los bytes leidos
if(Async->bytesRead > 0) //Prescindible al menos que enviemos un mensaje y no un paquete
printf(“%s”, Async->buffer);
memset(Async->buffer, ‘\0’, sizeof(Async->buffer)); //Limpiamos el buffer
ResetEvent(Async->oOver.hEvent); //Marcamos el evento como nosigned para la siguiente petición
Async->Espera = 0; //Indicamos que estamos preparados para la proxima lectura
return Async->bytesRead;
}
}
return -1;
}
for(int i=0; i < 10; i++)
{
LeerTuboA(usbHandle, &Async[i]);
WaitAsync(usbHandle, &Async[i]);
}
You need a OVERLAPPED struct, and inicialize. The next point, read the pipe and wait to signed state in OVERLAPPED struct (Async in my code).
With 10 pipes and de PIC, I transfer 500 KBytes/sec more or less…
I have one lib with some more functions for simplicity all WinUSB inicialize.
You are being a bit loose with your types. This
int __stdcall LeerTuboA (HANDLE usbHandle, tAsyncRead *Async)
should be
int __stdcall LeerTuboA (WINUSB_INTERFACE_HANDLE usbHandle, tAsyncRead *Async)
and if the HANDLE passed to WaitAsync(HANDLE usbHandle, tAsyncRead *Async) is the same usb interface handle as in LeerTuboA, it should be WaitAsync(WINUSB_INTERFACE_HANDLE usbHandle, tAsyncRead *Async) as well. This means that the call to
GetOverlappedResult(usbHandle, &Async->oOver, &Async->bytesRead, FALSE);
is incorrect because you are passing a WINUSB_INTERFACE_HANDLE when you should be passing a HANDLE. To fix this you have 2 options
-
add a new field to tAsyncRead, HANDLE DeviceHandle; and initialize it to the result of your call to CreateFile (which you also pass to WinUsb_Initialize) and then pass Async->DeviceHandle to GetOverlappedResult
-
pass the WINUSB_INTERFACE_HANDLE usbHandle to WinUsb_GetOverlappedResult
d
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Wednesday, October 31, 2007 9:35 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Using GetOverlappedResult and Completion Ports with WinUSB
Hi, first of all, sorry for my english, this is the code that I use, with a Microchip PIC 18f2550 and WinUSB, some extract:
void InicializarAsyncU (tAsyncRead *Async, int num)
{
Async->Pipe = num;
Async->oOver.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
memset(Async->buffer, ‘\0’, sizeof(Async->buffer));
Async->Espera = 0;
Async->bytesRead = 0;
}
int __stdcall LeerTuboA (HANDLE usbHandle, tAsyncRead *Async)
{
BOOL bResultado = FALSE;
//Si el pipe esta a cero, leemos, significa que no hay operaciones pendientes
if(Async->Espera == 0)
{
bResultado = WinUsb_ReadPipe(usbHandle, Async->Pipe, (PUCHAR) Async->buffer, 64, &Async->bytesRead, &Async->oOver);
if(bResultado == 0)
{
Async->Espera = 1;
return 0;
}
else
return 1;
}
//Si el pipe no esta a cero, significa que hay una operacion pendiente, devolvemos -1
else
{
return -1;
}
return 1;
}
int __stdcall WaitAsync(HANDLE usbHandle, tAsyncRead *Async)
{
BOOL bResultado = FALSE;
if(Async->Espera == 1)
{
if(WaitForSingleObject(Async->oOver.hEvent, 0) == WAIT_OBJECT_0)
//Termino, por lo tanto guardamos los valores de lectura del buffer
//y reiniciamos el proceso poniendo pipes a 0
{
GetOverlappedResult(usbHandle, &Async->oOver, &Async->bytesRead, FALSE); //Consultamos los bytes leidos
if(Async->bytesRead > 0) //Prescindible al menos que enviemos un mensaje y no un paquete
printf(“%s”, Async->buffer);
memset(Async->buffer, ‘\0’, sizeof(Async->buffer)); //Limpiamos el buffer
ResetEvent(Async->oOver.hEvent); //Marcamos el evento como nosigned para la siguiente petici?n
Async->Espera = 0; //Indicamos que estamos preparados para la proxima lectura
return Async->bytesRead;
}
}
return -1;
}
for(int i=0; i < 10; i++)
{
LeerTuboA(usbHandle, &Async[i]);
WaitAsync(usbHandle, &Async[i]);
}
You need a OVERLAPPED struct, and inicialize. The next point, read the pipe and wait to signed state in OVERLAPPED struct (Async in my code).
With 10 pipes and de PIC, I transfer 500 KBytes/sec more or less…
I have one lib with some more functions for simplicity all WinUSB inicialize.
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
xxxxx@gmail.com wrote:
Hi, first of all, sorry for my english, this is the code that I use, with a Microchip PIC 18f2550 and WinUSB, some extract:
…
for(int i=0; i < 10; i++)
{
LeerTuboA(usbHandle, &Async[i]);
WaitAsync(usbHandle, &Async[i]);
}
You CALL this asynchronous, but it should be clear that this is NOT
asynchronous. You are making 10 synchronous calls, one at a time. You
do not submit request #2 until request #1 has completed. That’s
synchronous.
You need a OVERLAPPED struct, and inicialize. The next point, read the pipe and wait to signed state in OVERLAPPED struct (Async in my code).
With 10 pipes and de PIC, I transfer 500 KBytes/sec more or less…
Since the PIC 18F2550 can only handle full-speed transfers (USB 1
speeds), that’s not bad at all. You might be able to get 700 kB/s or
800 kB/s by submitting multiple requests at once.
–
Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.
Thank’s for the feedback,
Fisrtly, I know that the handle is wrong but all the code works really well.
Secondly,
GetOverlappedResult(usbHandle, &Async->oOver, &Async->bytesRead, FALSE);
MSDN documentation says:
hFile
“A handle to the file, named pipe, or communications device. This is the same handle that was specified when the overlapped operation was started by a call to the ReadFile, WriteFile, ConnectNamedPipe, TransactNamedPipe, DeviceIoControl, or WaitCommEvent function.”
I think the text means the handle needs to be a handle for a USB device and not the createfile, and… yes, the handle needs to be HANDLE and not WINUSB_INTERFACE_HANDLE.
“This is the same handle that was specified when the overlapped operation was started by a call to…”… WinUsb_ReadPipe?
P.D: WinUsb_GetOverlappedResult isn’t documented in MSDN, any idea?
> for(int i=0; i < 10; i++)
{
LeerTuboA(usbHandle, &Async[i]);
WaitAsync(usbHandle, &Async[i]);
}
You CALL this asynchronous, but it should be clear that this is NOT asynchronous. You are making 10 > synchronous calls, one at a time. You do not submit request #2 until request #1 has completed. >That’s synchronous.
No, when I call LeerTuboA, the function return inmediatly without wait terminate operation, I make 10 calls for explore all pipes and take one buffer of them (one of the full buffers).
I call LeerTuboA, and explore signed state of OVERLAPPED struct with WaitAsync, but I don’t wait never for anything, only explore. It’s a polling… over 10 pipes…
If you see the code working, you can see that operation it’s executed three or four times (depends of CPU speed) for each pipe until the operation has completed (=info in the buffer is valid).
This is only one example of a lot of code (and this part is only the example, very simple), I use a reconstruction algorithm for repair big packets (parts of 64 bytes in one big of 1024 bytes), when the HOST (usb) empty one pipe, the PIC full this pipe.