FTDI. Would need to know more details about the FT_Write() function

Viewed 1082

A question about the FT_Write() function.

FT_STATUS FT_Write (FT_HANDLE ftHandle, LPVOID lpBuffer, DWORD dwBytesToWrite, LPDWORD lpdwBytesWritten)

I wonder about the lpdwBytesWritten.

When will it ever return *lpdwBytesWritten < dwBytesToWrite? Would FT_Write return other than FT_OK in that case?

And how much data can I send in one call to FT_Write? What limit has the dwBytesToWrite parameter?

I have not been able to find the answers to these questions. Have read in the FTDI Knowledgebase and also in the D2XX Programmer's Guide.

2 Answers

FT_Write returns status other than FT_OK on "critical" errors.

This function (as well as FT_Read) might return FT_OK and put in *lpdwBytesWritten any number from 0 (timeout) to dwBytesToWrite (transmission finished).

Intermediate values means that not all data transferred yet but transmission process can be continued.

Transmission loop might be like this:

BYTE *buf = pointer_to_data;
DWORD len = length_of_data;

FT_STATUS status;
DWORD written;

for (;;) {
    status = FT_Write(handle, buf, len, &written);
    if (status != FT_OK)
        return status;
    if (written == 0)
        return -1;   // or FT_OTHER_ERROR if no special timeout handling required
    if (written == len)
        return FT_OK;
    len -= written;
    buf += written;
}

See also Example 3, FT2232C Test Application (file t_titan.cpp, function DoRxTest at line 289)

dwBytesToWrite is a double word, that is 32 bits. A rather large number of bytes to write. My guess is that the function can return FT_OK even if the number of bytes written is < the number of bytes to write. It is useful to know how many bytes have been written, so that the next time you call the function, you know exactly where to set your transmit pointer in the buffer.

Related