This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Version 2.01Introduction to FTDI’s D2XX 2.0 Driver Technology
FTDI’s “D2XX Direct Drivers” for Windows offer an alternative solution to our VCP drivers which allows application software to interface with FT232 USB UART and FT245 USB FIFO devices using a DLL instead of a Virtual Com Port. The architecture of the D2XX drivers consists of a Windows WDM driver that communicates with the device via the Windows USB Stack and a DLL which interfaces the Application Software (written in VC++, C++ Builder, Delphi, VB etc.) to the WDM driver. An INF installation file, Uninstaller program and D2XX Programmers Guide complete the package.
The new version of the D2XX drivers contains many enhanced features and has been divided into four groups for clarity. The Classic Interface Section documents the original D2XX functions that are retained in this new release. The Classic Interface provides a simple, easy to use, set of functions to access these FTDI USB devices. New sections are “The EEPROM Interface” which allows application software to read / program the various fields in the 93C46 EEPROM including a user defined area which can be used for application specific purposes; “The FT232BM / FT245BM Enhancements” which allow control of the additional features in our 2nd generation devices, and the “FT-Win32 API” which is a more sophisticated alternative to the Classic Interface – our equivalent to the native Win 32 API calls that are used to control a legacy serial port. Using the FT-Win32 API, existing Windows legacy Comms applications can easily be converted to use the D2XX interface simply by replacing the standard Win32 API calls with the equivalent FT-Win32 API calls.
Please Note – the Classic Interface and the FT-Win32 API interface are alternatives. Developers should choose one or the other – the two sets of functions should not be mixed.
D2XX Classic Programming Interface – IntroductionAn FTD2XX device is an FT232 USB UART or FT245 USB FIFO interfacing to Windows application software using FTDI’s WDM driver FTD2XX.SYS. The FTD2XX.SYS driver has a programming interface exposed by the dynamic link library FTD2XX.DLL, and this document describes that interface.
D2XX Classic Programming Interface – OverviewFT_ListDevices returns information about the FTDI devices currently connected. In a system with multiple devices this can be used to decide which of the devices the application software wishes to access (using FT_OpenEx below).
Before the device can be accessed, it must first be opened. FT_Open and FT_OpenEx return a handle that is used by all functions in the Classic Programming Interface to identify the device. When the device has been opened successfully, I/O can be performed using FT_Read and FT_Write. When operations are complete, the device is closed using FT_Close.
Once opened, additional functions are available to reset the device (FT_ResetDevice); purge receive and transmit buffers (FT_Purge); set receive and transmit timeouts (FT_SetTimeouts); get the receive queue status (FT_GetQueueStatus); get the device status (FT_GetStatus); set and reset the break condition (FT_SetBreakOn, FT_SetBreakOff); and set conditions for event notification (FT_SetEventNotification).
For FT232 devices, functions are available to set the baud rate (FT_SetBaudRate), and set a non-standard baud rate (FT_SetDivisor); set the data characteristics such as word length, stop bits and parity (FT_SetDataCharacteristics); set hardware or software handshaking (FT_SetFlowControl); set modem control signals (FT_SetDTR, FT_ClrDTR, FT_SetRTS, FT_ClrRTS); get modem status (FT_GetModemStatus); set special characters such as event and error characters (FT_SetChars). For FT245 devices, these functions are redundant and can effectively be ignored.
D2XX Classic Programming Interface – ReferenceThe functions that make up the D2XX Classic Programming Interface are defined in this section. Type definitions of the functional parameters and return codes used in the D2XX Classic Programming Interface are contained in the Appendix.
FT_ListDevicesGet information concerning the devices currently connected. This function can return such information as the number of devices connected, and device strings such as serial number and product description.
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksThis function can be used in a number of ways to return different types of information.
In its simplest form, it can be used to return the number of devices currently connected. If FT_LIST_NUMBER_ONLY bit is set in dwFlags, the parameter pvArg1 is interpreted as a pointer to a DWORD location to store the number of devices currently connected.
It can be used to return device string information. If FT_OPEN_BY_SERIAL_NUMBER bit is set in dwFlags, the serial number string will be returned from this function. If FT_OPEN_BY_DESCRIPTION bit is set in dwFlags, the product description string will be returned from this function. If neither of these bits is set, the serial number string will be returned by default.
It can be used to return device string information for a single device. If FT_LIST_BY_INDEX bit is set in dwFlags, the parameter pvArg1 is interpreted as the index of the device, and the parameter pvArg2 is interpreted as a pointer to a buffer to contain the appropriate string. Indexes are zero-based, and the error code FT_DEVICE_NOT_FOUND is returned for an invalid index.
It can be used to return device string information for all connected devices. If FT_LIST_ALL bit is set in dwFlags, the parameter pvArg1 is interpreted as a pointer to an array of pointers to buffers to contain the appropriate strings, and the parameter pvArg2 is interpreted as a pointer to a DWORD location to store the number of devices currently connected. Note that, for pvArg1, the last entry in the array of pointers to buffers should be a NULL pointer so the array will contain one more location than the number of devices connected.
ExamplesSample code shows how to get the number of devices currently connected.
FT_STATUS ftStatus;DWORD numDevs;
ftStatus = FT_ListDevices(&numDevs,NULL,FT_LIST_NUMBER_ONLY);if (ftStatus == FT_OK) { // FT_ListDevices OK, number of devices connected is in numDevs}else { // FT_ListDevices failed}
This sample shows how to get the serial number of the first device found. Note that indexes are zero-based. If more than one device is connected, incrementing devIndex will get the serial number of each connected device in turn.
ftStatus = FT_ListDevices((PVOID)devIndex,Buffer,FT_LIST_BY_INDEX|FT_OPEN_BY_SERIAL_NUMBER);if (FT_SUCCESS(ftStatus)) { // FT_ListDevices OK, serial number is in Buffer}else { // FT_ListDevices failed}
This sample shows how to get the product descriptions of all the devices currently connected.
FT_STATUS ftStatus;char *BufPtrs[3]; // pointer to array of 3 pointerschar Buffer1[64]; // buffer for product description of first device foundchar Buffer2[64]; // buffer for product description of second deviceDWORD numDevs;
// initialize the array of pointersBufPtrs[0] = Buffer1;BufPtrs[1] = Buffer2;BufPtrs[2] = NULL; // last entry should be NULL
ftStatus = FT_ListDevices(BufPtrs,&numDevs,FT_LIST_ALL|FT_OPEN_BY_DESCRIPTION);if (FT_SUCCESS(ftStatus)) { // FT_ListDevices OK, product descriptions are in Buffer1 and Buffer2, and // numDevs contains the number of devices connected}else { // FT_ListDevices failed}
FT_OpenOpen the device and return a handle which will be used for subsequent accesses.
FT_STATUS FT_Open ( int iDevice, FT_HANDLE *ftHandle )
ParametersiDevice
Must be 0 if only one device is attached. For multiple devices 1, 2 etc.
ftHandlePointer to a variable of type FT_HANDLE where the handle will be stored. This handle must be used to access the device.
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksAlthough this function can be used to open multiple devices by setting iDevice to 0, 1, 2 etc. there is no ability to open a specific device. To open named devices, use the function FT_OpenEx.
FT_OpenExOpen the named device and return a handle which will be used for subsequent accesses. The device name can be its serial number or device description.
Meaning depends on dwFlags, but it will normally be interpreted as a pointer to a null terminated string.
dwFlagsFT_OPEN_BY_SERIAL_NUMBER or FT_OPEN_BY_DESCRIPTION.
ftHandlePointer to a variable of type FT_HANDLE where the handle will be stored. This handle must be used to access the device.
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksThis function should be used to open multiple devices. Multiple devices can be opened at the same time if they can be distinguished by serial number or device description.
ExampleThese samples show how to open two devices simultaneously.
Suppose one device has serial number “FT000001”, and the other has serial number “FT999999”.
lpBufferPointer to the buffer that receives the data from the device.
dwBytesToReadNumber of bytes to be read from the device.
lpdwBytesReturnedPointer to a variable of type DWORD which receives the number of bytes read from the device.
Return ValueFT_OK if successful, FT_IO_ERROR otherwise.
RemarksFT_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.
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksThe application note “Setting Baud rates for the FT8U232AM”, which is available on our web site www.ftdichip.com, describes how to calculate the divisor for a non standard baud rate.
FT_GetStatusGets the device status including number of characters in the receive queue, number of characters in the transmit queue, and the current event status.
dwEventMask Conditions that cause the event to be set.
pvArgInterpreted as a handle of an event
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksAn application can use this function to setup conditions which allow a thread to block until one of the conditions is met. Typically, an application will create an event, call this function, then block on the event. When the conditions are met, the event is set, and the application thread unblocked.
dwEventMask is a bit-map that describes the events the application is interested in. pvArg is interpreted as the handle of an event which has been created by the application. If one of the event conditions is met, the event is set.
If FT_EVENT_RXCHAR is set in dwEventMask, the event will be set when a character has been received by the device. If FT_EVENT_MODEM_STATUS is set in dwEventMask, the event will be set when a change in the modem signals has been detected by the device.
Sometime later, block the application thread by waiting on the event, then when the event has occurred, determine the condition that caused the event, and process it accordingly.
EEPROM Programming Interface – IntroductionFTDI has included EEPROM programming support in the D2XX library, and this document describes that interface.
EEPROM Programming Interface – OverviewFunctions are provided to program the EEPROM (FT_EE_Program), and read the EEPROM (FT_EE_Read). Unused space in the EEPROM is called the User Area (EEUA), and functions are provided to access the EEUA. FT_EE_UASize gets its size, FT_EE_UAWrite writes data into it, and FT_EE_UARead is used to read its contents.
EEPROM Programming Interface - ReferenceThe EEPROM programming interface functions are described in this section. Type definitions of the functional parameters and return codes used in the D2XX EEPROM programming interface are contained in the Appendix.
RemarksThis function interprets the parameter pvArgs as a pointer to a struct of type FT_PROGRAM_DATA that contains the data to write to the EEPROM. The data is written to EEPROM, then read back and verified.
If the SerialNumber field in FT_PROGRAM_DATA is NULL, or SerialNumber points to a NULL string, a serial number based on the ManufacturerId and the current date and time will be generated.
If pvArgs is NULL, the device will be programmed with the default data { 0x0403, 0x6001, “FTDI”, “FT”, “USB HS Serial Converter”, “”, 44, 1, 0, 1,FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 0 }
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksThis function interprets the parameter pvArgs as a pointer to a struct of type FT_PROGRAM_DATA that contains storage for the data to be read from the EEPROM.
The function does not perform any checks on buffer sizes, so the buffers passed in the FT_PROGRAM_DATA struct must be big enough to accommodate their respective strings (including null terminators). The sizes shown in the following example are more than adequate and can be rounded down if necessary. The restriction is that the Manufacturer string length plus the Description string length is less than or equal to 40 characters.
pucDataPointer to a buffer that contains storage for data to be read.
dwDataLenSize, in bytes, of buffer that contains storage for data to be read.
lpdwBytesReadPointer to a DWORD that receives the number of bytes read.
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksThis function interprets the parameter pucData as a pointer to an array of bytes of size dwDataLen that contains storage for the data to be read from the EEUA. The actual number of bytes read is stored in the DWORD referenced by lpdwBytesRead. If dwDataLen is less than the size of the EEUA, then dwDataLen bytes are read into the buffer. Otherwise, the whole of the EEUA is read into the buffer..
An application should check the function return value and lpdwBytesRead when FT_EE_UARead returns.
ftStatus = FT_EE_UARead(ftHandle,Buffer,64,&BytesRead);if (ftStatus == FT_OK) { // FT_EE_UARead OK // User Area data stored in Buffer // Number of bytes read from EEUA stored in BytesRead}else { // FT_EE_UARead FAILED!}
FT_STATUS FT_EE_UAWrite ( FT_HANDLE ftHandle, PUCHAR pucData, DWORD dwDataLen )
ParametersftHandle
Handle of the device.
pucDataPointer to a buffer that contains the data to be written.
dwDataLenSize, in bytes, of buffer that contains the data to be written.
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksThis function interprets the parameter lpData as a pointer to an array of bytes of size dwDataLen that contains the data to be written to the EEUA. It is a programming error for dwDataLen to be greater than the size of the EEUA.
ftStatus = FT_EE_UASize(ftHandle,&EEUA_Size);if (ftStatus == FT_OK) { // FT_EE_UASize OK // EEUA_Size contains the size, in bytes, of the EEUA}else { // FT_EE_UASize FAILED!}
Extended Programming Interface - IntroductionFT232BM is FTDI’s second generation USB UART IC. FT245BM is FTDI’s second generation USB FIFO IC. They offer extra functionality, including programmable features, to their predecessors. The programmable features are supported by extensions to the D2XX driver, and the programming interface is exposed by FTD2XX.DLL.
Extended Programming Interface - OverviewNew features include a programmable receive buffer timeout and bit bang mode. The receive buffer timeout is controlled via the latency timer functions FT_GetLatencyTimer and FT_SetLatencyTimer. Bit bang mode is controlled via the functions FT_GetBitMode and FT_SetBitMode.
Before these functions can be accessed, the COM port must first be opened. The Win32API function, CreateFile, returns a handle that is used by all functions in the programming interface to identify the port. After opening the port successfully, the function FT_GetDeviceInfo can be used to get information about the device underlying the port, and to confirm that the port is a virtual COM port.
Extended Programming Interface - ReferenceThe functions that comprise the FTD2XX programming interface are described in this section. See the Appendix for definitions of data types used in the descriptions of the functions below.
FT_GetLatencyTimerGet the current value of the latency timer.
FT_STATUS FT_GetLatencyTimer ( FT_HANDLE ftHandle, PUCHAR pucTimer )
ParametersftHandle
Handle of the device.
pucTimerPointer to unsigned char to store latency timer value.
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksIn the FT8U232AM and FT8U245AM devices, the receive buffer timeout that is used to flush remaining data from the receive buffer was fixed at 16 ms. In the FT232BM, this timeout is programmable and can be set at 1 ms intervals between 1ms and 255 ms. This allows the device to be better optimized for protocols requiring faster response times from short data packets.
ExampleHANDLE ftHandle; // valid handle returned from FT_W32_CreateFileFT_STATUS ftStatus;UCHAR LatencyTimer;ftStatus = FT_GetLatencyTimer(ftHandle,&LatencyTimer);if (ftStatus == FT_OK) { // LatencyTimer contains current value}else { // FT_GetLatencyTimer FAILED!}
ucTimerRequired value, in milliseconds, of latency timer. Valid range is 1 - 255.
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksIn the FT8U232AM and FT8U245AM devices, the receive buffer timeout that is used to flush remaining data from the receive buffer was fixed at 16 ms. In the FT232BM, this timeout is programmable and can be set at 1 ms intervals between 1ms and 255 ms. This allows the device to be better optimized for protocols requiring faster response times from short data packets.
Example
HANDLE ftHandle; // valid handle returned from FT_W32_CreateFileFT_STATUS ftStatus;UCHAR LatencyTimer = 10;ftStatus = FT_SetLatencyTimer(ftHandle,LatencyTimer);if (ftStatus == FT_OK) { // LatencyTimer set to 10 milliseconds}else { // FT_SetLatencyTimer FAILED!}
dwOutTransferSizeTransfer size for USB OUT request.
Return ValueFT_OK if successful, otherwise the return value is an FT error code.
RemarksPreviously, USB request transfer sizes have been set at 4096 bytes and have not been configurable. This function can be used to change the transfer sizes to better suit the application’s requirements.
Note that, at present, only dwInTransferSize is supported.
ExampleHANDLE ftHandle; // valid handle returned from FT_W32_CreateFileFT_STATUS ftStatus;DWORD InTransferSize = 16384;ftStatus = FT_SetUSBParameters(ftHandle,InTransferSize,0);if (ftStatus == FT_OK); // In transfer size set to 16 Kbyteselse; // FT_SetUSBParameters FAILED!
FT-Win32 Programming Interface - IntroductionThe D2XX interface now incorporates functions based on Win32 API and Win32 COMM API calls. This facilitates the porting of communications applications from VCP to D2XX.
FT-Win32 Programming Interface - OverviewBefore the device can be accessed, it must first be opened. FT_W32_CreateFile returns a handle that is used by all functions in the programming interface to identify the device. When the device has been opened successfully, I/O can be performed using FT_W32_ReadFile and FT_W32_WriteFile. When operations are complete, the device is closed using FT_W32_CloseHandle.
FT-Win32 Programming Interface - ReferenceThe functions that comprise the FTD2XX programming interface are described in this section. See the Appendix for definitions of data types used in the descriptions of the functions below.
FT_W32_CreateFileOpen the named device and return a handle that will be used for subsequent accesses. The device name can be its serial number or device description.
Pointer to a null terminated string that contains the name of the device. The name of the device can be its serial number or description as obtained from the FT_ListDevices function.
dwAccessType of access to the device. Access can be GENERIC_READ, GENERIC_WRITE, or both.
dwShareModeHow the device is shared. This value must be set to 0.
lpSecurityAttributesThis parameter has no effect and should be set to NULL.
dwCreateThis parameter must be set to OPEN_EXISTING.
dwAttrsAndFlagsFile attributes and flags. This parameter is a combination of FILE_ATTRIBUTE_NORMAL, FILE_FLAG_OVERLAPPED if overlapped I/O is used, FT_OPEN_BY_SERIAL_NUMBER if lpszName is the device’s serial number, and FT_OPEN_BY_DESCRIPTION if lpszName is the device’s description.
hTemplateThis parameter must be NULL.
Return ValueIf the function is successful, the return value is a handle.If the function is unsuccessful, the return value is the Win32 error code INVALID_HANDLE_VALUE.
RemarksThis function must be used if overlapped I/O is required.
if (ftHandle == INVALID_HANDLE_VALUE); // FT_W32_CreateDevice failedelse { // FT_W32_CreateFile OK, so do some work, and eventually ... FT_W32_CloseHandle(ftHandle);}
lpBufferPointer to a buffer that receives the data from the device.
dwBytesToReadNumber of bytes to be read from the device.
lpdwBytesReturnedPointer to a variable that receives the number of bytes read from the device.
LpOverlappedPointer to an overlapped structure.
Return ValueIf the function is successful, the return value is nonzero.If the function is unsuccessful, the return value is zero.
RemarksThis function supports both non-overlapped and overlapped I/O.
Non-overlapped I/O
The parameter, lpOverlapped, must be NULL for non-overlapped I/O.
This function 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 as dwBytesToRead so that the function reads the device and returns immediately.
When a read timeout has been setup in a previous call to FT_W32_SetCommTimeouts, this function returns when the timer expires or dwBytesToRead have been read, whichever occurs first. If a timeout occurred, any available data is read into lpBuffer and the function returns a non-zero value.
An application should use the function return value and lpdwBytesReturned when processing the buffer. If the return value is non-zero and lpdwBytesReturned is equal to dwBytesToRead then the function has completed normally. If the return value is non-zero and lpdwBytesReturned is less then dwBytesToRead then a timeout has occurred, and the read request has been partially completed. Note that if a timeout occurred and no data was read, the return value is still non-zero.
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.
Overlapped I/O
When the device has been opened for overlapped I/O, an application can issue a request and perform some additional work while the request is pending. This contrasts with the case of non-overlapped I/O in which the application issues a request and receives control again only after the request has been completed.
The parameter, lpOverlapped, must point to an initialized OVERLAPPED structure.
If there is enough data in the receive queue to satisfy the request, the request completes immediately and the return code is non-zero. The number of bytes read is returned in lpdwBytesReturned.
If there is not enough data in the receive queue to satisfy the request, the request completes immediately, and the return code is zero, signifying an error. An application should call FT_W32_GetLastError to get the cause of the error. If the error code is ERROR_IO_PENDING, the overlapped operation is still in progress, and the application can perform other processing. Eventually, the application checks the result of the overlapped request by calling FT_W32_GetOverlappedResult.
If successful, the number of bytes read is returned in lpdwBytesReturned.
ExampleThis example shows how to read 256 bytes from the device using non-overlapped I/O.
FT_HANDLE ftHandle; // setup by FT_W32_CreateFile for non-overlapped i/ochar Buf[256];DWORD dwToRead = 256;DWORD dwRead;
if (FT_W32_ReadFile(ftHandle, Buf, dwToRead, &dwRead, &osWrite)) { if (dwToRead == dwRead) ; // FT_W32_ReadFile OK else ; // FT_W32_ReadFile timeout}else ; // FT_W32_ReadFile failed
This example shows how to read 256 bytes from the device using overlapped I/O.
FT_HANDLE ftHandle; // setup by FT_W32_CreateFile for overlapped i/ochar Buf[256];DWORD dwToRead = 256;DWORD dwRead;OVERLAPPED osRead = { 0 };
if (!FT_W32_ReadFile(ftHandle, Buf, dwToRead, &dwRead, &osWrite)) { if (FT_W32_GetLastError(ftHandle) == ERROR_IO_PENDING) { // write is delayed so do some other stuff until ... if (!FT_W32_GetOverlappedResult(ftHandle, &osRead, &dwRead, FALSE)) ; // error else { if (dwToRead == dwRead) ; // FT_W32_ReadFile OK else ; // FT_W32_ReadFile timeout } }}else { // FT_W32_ReadFile OK}
lpBufferPointer to the buffer that contains the data to write to the device.
dwBytesToWriteNumber of bytes to be written to the device.
lpdwBytesWrittenPointer to a variable that receives the number of bytes written to the device.
lpOverlappedPointer to an overlapped structure.
Return ValueIf the function is successful, the return value is nonzero.If the function is unsuccessful, the return value is zero.
RemarksThis function supports both non-overlapped and overlapped I/O.
Non-overlapped I/O
The parameter, lpOverlapped, must be NULL for non-overlapped I/O.
This function always returns the number of bytes written in lpdwBytesWritten.
This function does not return until dwBytesToWrite have been written to the device.
When a write timeout has been setup in a previous call to FT_W32_SetCommTimeouts, this function returns when the timer expires or dwBytesToWrite have been written, whichever occurs first. If a timeout occurred, lpdwBytesWritten contains the number of bytes actually written, and the function returns a non-zero value.
An application should always use the function return value and lpdwBytesWritten. If the return value is non-zero and lpdwBytesWritten is equal to dwBytesToWrite then the function has completed normally. If the return value is non-zero and lpdwBytesWritten is less then dwBytesToWrite then a timeout has occurred, and the write request has been partially completed. Note that if a timeout occurred and no data was written, the return value is still non-zero.
Overlapped I/O
When the device has been opened for overlapped I/O, an application can issue a request and perform some additional work while the request is pending. This contrasts with the case of non-overlapped I/O in which the application issues a request and receives control again only after the request has been completed.
The parameter, lpOverlapped, must point to an initialized OVERLAPPED structure.
This function completes immediately, and the return code is zero, signifying an error. An application should call FT_W32_GetLastError to get the cause of the error. If the error code is ERROR_IO_PENDING, the overlapped operation is still in progress, and the application can perform other processing. Eventually, the application checks the result of the overlapped request by calling FT_W32_GetOverlappedResult.
If successful, the number of bytes written is returned in lpdwBytesWritten.
ExampleThis example shows how to write 128 bytes to the device using non-overlapped I/O.
FT_HANDLE ftHandle; // setup by FT_W32_CreateFile for overlapped i/ochar Buf[128]; // contains data to write to the deviceDWORD dwToWrite = 128;DWORD dwWritten;
if (FT_W32_WriteFile(ftHandle, Buf, dwToWrite, &dwWritten, &osWrite)) { if (dwToWrite == dwWritten) ; // FT_W32_WriteFile OK else ; // FT_W32_WriteFile timeout}else ; // FT_W32_WriteFile failed
This example shows how to write 128 bytes to the device using overlapped I/O.
FT_HANDLE ftHandle; // setup by FT_W32_CreateFile for overlapped i/ochar Buf[128]; // contains data to write to the deviceDWORD dwToWrite = 128;DWORD dwWritten;OVERLAPPED osWrite = { 0 };
if (!FT_W32_WriteFile(ftHandle, Buf, dwToWrite, &dwWritten, &osWrite)) { if (FT_W32_GetLastError(ftHandle) == ERROR_IO_PENDING) { // write is delayed so do some other stuff until ... if (!FT_W32_GetOverlappedResult(ftHandle, &osWrite, &dwWritten, FALSE)) ; // error else { if (dwToWrite == dwWritten) ; // FT_W32_WriteFile OK else ; // FT_W32_WriteFile timeout } }}else { // FT_W32_WriteFIle OK}
FT_W32_GetLastErrorGets the last error that occurred on the device.
BOOL FT_W32_GetLastError ( FT_HANDLE ftHandle )
ParametersftHandle
Handle of the device.
Return ValueIf the function is successful, the return value is nonzero.If the function is unsuccessful, the return value is zero.
RemarksThis function is normally used with overlapped I/O. For a description of its use, see FT_W32_ReadFile and FT_W32_WriteFile..
if (memcmp(&oldCS, &newCS, sizeof(FTCOMSTAT))) { bChanged = TRUE; oldCS = newCS;} if (bChanged) {
if (dwErrors & CE_BREAK) ; // BREAK condition detected if (dwErrors & CE_FRAME) ; // Framing error detected if (dwErrors & CE_RXOVER) ; // Receive buffer has overflowed if (dwErrors & CE_TXFULL) ; // Transmit buffer full if (dwErrors & CE_OVERRUN) ; // Character buffer overrun if (dwErrors & CE_RXPARITY) ; // Parity error detected
if (newCS.fCtsHold) ; // Transmitter waiting for CTS if (newCS.fDsrHold) ; // Transmitter is waiting for DSR if (newCS.fRlsdHold) ; // Transmitter is waiting for RLSD if (newCS.fXoffHold) ; // Transmitter is waiting because XOFF was received if (newCS.fXoffSent) ; // if (newCS.fEof) ; // End of file character has been received if (newCS.fTxim) ; // Tx immediate character queued for transmission
// newCS.cbInQue contains number of bytes in receive queue // newCS.cbOutQue contains number of bytes in transmit queue
dwFuncThe extended function to perform can be one of the following values.
CLRDTR Clear the DTR signalCLRRTS Clear the RTS signalSETDTR Set the DTR signalSETRTS Set the RTS signalSETBREAK Set the BREAK conditionCLRBREAK Clear the BREAK condition
Return ValueIf the function is successful, the return value is nonzero.If the function is unsuccessful, the return value is zero.
ExampleThis example shows how to use this function.
lpdwStatPointer to a variable to contain modem control value. The modem control value can be a combination of the following.
MS_CTS_ON Clear to Send (CTS) is onMS_DSR_ON Data Set Ready (DSR) is onMS_RING_ON Ring Indicator (RI) is onMS_RLSD_ON Receive Line Signal Detect (RLSD) is on
Return ValueIf the function is successful, the return value is nonzero.If the function is unsuccessful, the return value is zero.
ExampleThis example shows how to use this function.
FT_HANDLE ftHandle; // setup by FT_W32_CreateFileDWORD dwStatus;
if (FT_W32_GetCommModemStatus(ftHandle,&dwStatus)) { // FT_W32_GetCommModemStatus ok if (dwStatus & MS_CTS_ON) ; // CTS is on if (dwStatus & MS_DSR_ON) ; // DSR is on if (dwStatus & MS_RI_ON) ; // RI is on if (dwStatus & MS_RLSD_ON) ; // RLSD is on}else ; // FT_W32_GetCommModemStatus failed
dwMaskMask containing events that the device has to monitor. This can be a combination of the following.
EV_BREAK BREAK condition detectedEV_CTS Change in Clear to Send (CTS)EV_DSR Change in Data Set Ready (DSR)EV_ERR Error in line statusEV_RING Ring Indicator (RI) detectedEV_RLSD Change in Receive Line Signal Detect (RLSD)EV_RXCHAR Character receivedEV_RXFLAG Event character receivedEV_TXEMPTY Transmitter empty
Return ValueIf the function is successful, the return value is nonzero.If the function is unsuccessful, the return value is zero.
RemarksThis function specifies the events that the device should monitor. An application can call the function FT_W32_WaitCommEvent to wait for an event to occur.
ExampleThis example shows how to monitor changes in the modem status lines DSR and CTS.
Return ValueIf the function is successful, the return value is nonzero.If the function is unsuccessful, the return value is zero.
ExampleThis example shows how to use this function to change the baud rate.
FT_HANDLE ftHandle; // setup by FT_W32_CreateFileFTDCB ftDCB;
if (FT_W32_GetCommState(ftHandle,&ftDCB)) { // FT_W32_GetCommState ok, device state is in ftDCB ftDCB.BaudRate = 921600; if (FT_W32_SetCommState(ftHandle,&ftDCB)) ; // FT_W32_SetCommState ok else ; // FT_W32_SetCommState failed} else ; // FT_W32_GetCommState failed
lpftTimeoutsPointer to a COMMTIMEOUTS structure that contains timeout information.
Return ValueIf the function is successful, the return value is nonzero.If the function is unsuccessful, the return value is zero.
RemarksTimeouts are calculated using the information in the FTTIMEOUTS structure.
For read requests, the number of bytes to be read is multiplied by the total timeout multiplier, and added to the total timeout constant. So, if TS is an FTTIMEOUTS structure and the number of bytes to read is dwToRead, the read timeout, rdTO, is calculated as follows. rdTO = (dwToRead * TS.ReadTotalTimeoutMultiplier) + TS.ReadTotalTimeoutConstant
For write requests, the number of bytes to be written is multiplied by the total timeout multiplier, and added to the total timeout constant. So, if TS is an FTTIMEOUTS structure and the number of bytes to write is dwToWrite, the write timeout, wrTO, is calculated as follows. wrTO = (dwToWrite * TS.WriteTotalTimeoutMultiplier) + TS.WriteTotalTimeoutConstant
lpdwEventPointer to a location that receives a mask that contains the events that occurred.
lpOverlappedPointer to an OVERLAPPED structure.
Return ValueIf the function is successful, the return value is nonzero.If the function is unsuccessful, the return value is zero.
RemarksThis function supports both non-overlapped and overlapped I/O.
Non-overlapped I/O
The parameter, lpOverlapped, must be NULL for non-overlapped I/O.
This function does not return until an event that has been specified in a call toFT_W32_SetCommMask has occurred. The events that occurred and resulted in this function returning are stored in lpdwEvent.
Overlapped I/O
When the device has been opened for overlapped I/O, an application can issue a request and perform some additional work while the request is pending. This contrasts with the case of non-overlapped I/O in which the application issues a request and receives control again only after the request has been completed.
The parameter, lpOverlapped, must point to an initialized OVERLAPPED structure.
This function does not return until an event that has been specified in a call toFT_W32_SetCommMask has occurred.
If an event has already occurred, the request completes immediately, and the return code is non-zero. The events that occurred are stored in lpdwEvent.
If an event has not yet occurred, the request completes immediately, and the return code is zero, signifying an error. An application should call FT_W32_GetLastError to get the cause of the error. If the error code is ERROR_IO_PENDING, the overlapped operation is still in progress, and the application can perform other processing. Eventually, the application checks the result of the overlapped request by callingFT_W32_GetOverlappedResult. The events that occurred and resulted in this function returning are stored in lpdwEvent.
ExampleThis example shows how to write 128 bytes to the device using non-overlapped I/O.
FT_HANDLE ftHandle; // setup by FT_W32_CreateFile for non-overlapped i/oDWORD dwEvents;
This example shows how to write 128 bytes to the device using overlapped I/O.
FT_HANDLE ftHandle; // setup by FT_W32_CreateFile for overlapped i/oDWORD dwEvents;DWORD dwRes;OVERLAPPED osWait = { 0 };
if (!FT_W32_WaitCommEvent(ftHandle, &dwEvents, &osWait)) { if (FT_W32_GetLastError(ftHandle == ERROR_IO_PENDING) { // wait is delayed so do some other stuff until ... if (!FT_W32_GetOverlappedResult(ftHandle, &osWait, &dwRes, FALSE)) ; // error else ; // FT_W32_WaitCommEvent OK // Events that occurred are stored in dwEvents }}else { // FT_W32_WaitCommEvent OK // Events that occurred are stored in dwEvents}
Type DefinitionsExcerpts from the header file FTD2XX.H are included in this Appendix to explain any references in the descriptions of the functions in this document.
For Visual C++ applications, these values are pre-declared in the header file, FTD2XX.H, which is included in the driver release. For other languages, these definitions will have to be converted to use equivalent types, and may have to be defined in an include file or within the body of the code. For non-VC++ applications check the application code examples on the FTDI web site as a translation of these may already exist.
UCHAR unsigned char (1 byte)PUCHAR Pointer to unsigned char (4 bytes)PCHAR Pointer to char (4 bytes)DWORD unsigned long (4 bytes)FT_HANDLE DWORD
Notification Events (see FT_SetEventNotification)FT_EVENT_RXCHAR = 1FT_EVENT_MODEM_STATUS = 2
FT_PROGRAM_DATA (EEPROM Programming Interface)
typedef struct ft_program_data { WORD VendorId; // 0x0403 WORD ProductId; // 0x6001 char *Manufacturer; // “FTDI” char *ManufacturerId; // “FT” char *Description; // “USB HS Serial Converter” char *SerialNumber; // “FT000001” if fixed, or NULL WORD MaxPower; // 0 < MaxPower <= 500 WORD PnP; // 0 = disabled, 1 = enabled WORD SelfPowered; // 0 = bus powered, 1 = self powered WORD RemoteWakeup; // 0 = not capable, 1 = capable // // Rev4 extensions // bool Rev4; // true if Rev4 chip, false otherwise bool IsoIn; // true if in endpoint is isochronous bool IsoOut; // true if out endpoint is isochronous bool PullDownEnable; // true if pull down enabled bool SerNumEnable; // true if serial number to be used bool USBVersionEnable; // true if chip uses USBVersion WORD USBVersion; // BCD (0x0200 => USB2)} FT_PROGRAM_DATA, *PFT_PROGRAM_DATA;