Synchronous Transfer
Transfer data in synchronous mode using function UsbTransfer is rather simple but it blocks calling thread execution until transfer completes. The following is an example of blocking transfer assuming that all handles are opened. Error checks are omitted. For extended information about synchronous transfer refer to 'Pipes' example.
USB_DEVICE_HANDLE hUsb;
USB_PIPE_HANDLE hPipe;
PVOID pBuffer;
ULONG BufferLength, BytesReturned;
// Open first RapidUSB device
hUsb = OpenRapidUsb(0);
// Open first RapidUSB device pipe
hPipe = UsbPipeOpen(hUsb, 0);
// Allocate transfer buffer
pBuffer = malloc(BufferLength);
// Start transfer...
BOOL success = UsbTransfer(hPipe, pBuffer, BufferLength, &BytesReturned);
// thread waits transfer operation to finish
if ( success)
{
// Transfer successful...
}
else
{
// Transfer failed....
}
free(pBuffer);
UsbPipeClose(hPipe);
CloseRapidUsb(hUsb);
|