Your browser does not allow scripts
Please click here to view a static table of contents without scripts
FT_Read
Return to Introduction  Previous page  Next page
Read data from the device.


FT_STATUS
FT_Read
(FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD dwBytesToRead, LPDWORD lpdwBytesReturned)




Parameters
ftHandle
Handle of the device.
lpBuffer
Pointer to the buffer that receives the data from the device.
dwBytesToRead
Number of bytes to be read from the device.
lpdwBytesReturned
Pointer to a variable of type DWORD which receives the number of bytes read from the device.




Return Value
FT_OK if successful, FT_IO_ERROR otherwise.



Remarks
FT_Read
always returns the number of bytes read in lpdwBytesReturned.

This function does not return until
dwBytesToRead have been read into the buffer. The number of bytes in the receive queue can be determined by calling FT_GetStatus or FT_GetQueueStatus, and passed to FT_Read as dwBytesToRead so that the function reads the device and returns immediately.

When a read timeout value has been specified in a previous call to
FT_SetTimeouts, FT_Read returns when the timer expires or dwBytesToRead have been read, whichever occurs first. If the timeout occurred, FT_Read reads available data into the buffer and returns FT_OK.

An application should use the function return value and
lpdwBytesReturned when processing the buffer. If the return value is FT_OK, and lpdwBytesReturned is equal to dwBytesToRead then FT_Read has completed normally. If the return value is FT_OK, and lpdwBytesReturned is less then dwBytesToRead then a timeout has occurred and the read has been partially completed. Note that if a timeout occurred and no data was read, the return value is still FT_OK.

A return value of
FT_IO_ERROR suggests an error in the parameters of the function, or a fatal error like USB disconnect has occurred.



Example
This sample shows how to read all the data currently available.

FT_HANDLE ftHandle;
FT_STATUS ftStatus;
DWORD EventDWord;
DWORD TxBytes;
DWORD BytesReceived;
char RxBuffer[256];

ftStatus = FT_Open(0, &ftHandle);
if(ftStatus != FT_OK) {
   // FT_Open failed
   return;
}

FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord);
if (RxBytes > 0) {
    ftStatus = FT_Read(ftHandle,RxBuffer,RxBytes,&BytesReceived);
    if (ftStatus == FT_OK) {
        // FT_Read OK
    }
    else {
        // FT_Read Failed
    }
}

FT_Close(ftHandle);



This sample shows how to read with a timeout of 5 seconds.

FT_HANDLE ftHandle;
FT_STATUS ftStatus;
DWORD RxBytes = 10;
DWORD BytesReceived;
char RxBuffer[256];

ftStatus = FT_Open(0, &ftHandle);
if(ftStatus != FT_OK) {
   // FT_Open failed
   return;
}

FT_SetTimeouts(ftHandle,5000,0);
ftStatus = FT_Read(ftHandle,RxBuffer,RxBytes,&BytesReceived);
if (ftStatus == FT_OK) {
    if (BytesReceived == RxBytes) {
        // FT_Read OK
    }
    else {
        // FT_Read Timeout
    }
}
else {
    // FT_Read Failed
}

FT_Close(ftHandle);