Opening a connection with FT232 using D2XX library and GCC

Pinterest icon

In this section we will learn to use GCC to compile D2XX library from FTDI to open  a connection with the FT232 chip.Opening a simple connection with FT232 will ensure that your tool chain is configured correctly and your hardware is working properly. 

The first thing to do is to check whether the hardware you are using is working properly.Please connect your FT232 breakout board or USB2SERIAL converter to your PC and check whether it is getting detected in the "Device Manager" under COM Ports (something like COM 12,COM30).To be extra sure you can do a simple loop back test by connecting the TX and RX pins of FT232(Serial side) .Send some data to the com port listed under "Device Manager" using a terminal program like PuTTy and check whether it is getting received (it will be printed on the terminal window).

Installing MinGW
The first step is to install the MingW tool chain from it's site. Download the online installer and install the MinGW in the default directory usually C:\MinGW. After the installation is complete make sure that the C:\MinGW\bin directory contains gcc.exe, g++.exe, gdb.exe. You may have to specify explicitly to install g++ and gdb while installing since they are not usually installed under a standard installation.

After installation add the location of your installation (C:\MinGW\bin ) to your system's PATH variable.

On Windows 7 
Right Click on Computer → "Select Properties" → Click  "Advanced System Settings"

on the “System Properties Window” under the “Advanced” tab  → Click on the “Environment Variables

System properties window to configure the path

On the “Environment Variables” Window → under “System Variables” → Scroll down and Select  “Path
Click “Edit “to change the system variable.

System Variables window highlighting the path

Now add the location of your installation C:\MinGW\bin  to the “Variable Value
Also add a semicolon after bin  C:\MinGW\bin;

Edit System variable

Now to check whether the path you configured is working properly or not ,fire up your command prompt ie cmd.exe and type c:\> gcc.exe .If everything is configured correctly you will get a message similar to what is shown in the below figure.

MingW successfull configuration in windows

Dont worry about the fatal error ,gcc is complaining error because you have not provided any input files for it to compile .

If you are not convinced you can type c:\> gcc.exe --version to display the version number of gcc.

finding gcc version on windows cmd.exe

If on the other hand you are getting a message like "gcc is not recognized as an internal or external command ,Operable program or batch file ",it means that your path variable is not properly configured.Check whether you have entered the correct path for the MinGW installation,have you forgot to put the semicolon at the end of the path variable.

Compiling D2XX library with GCC

You can directly obtain D2XX library from FTDI  or you can download it from the code samples along with other source codes. For compiling the D2XX library you would require the “ftd2xx.lib” and the header file “ftd2xx.h”.Please note that if you are using the  D2XX library downloaded directly from FTDI ,use the library in the “i386”  folder.

For compiling the library,create a folder called “FTDI” or any other name of your choice. Copy the D2XX library “ftd2xx.lib” along with the header file “ftd2xx.h” to FTDI folder. Use a text editor of your choice (I use Notepad++) to create a file called “ftdi.c” and save it to FTDI folder.

Type the following code into your text editor and save it.

#include <stdio.h>
#include <windows.h>  // for windows specific keywords in ftd2xx.h
#include "ftd2xx.h"   // Header file for ftd2xx.lib 
void main()
   FT_HANDLE ft_handle;  // handle to the USB ic
   FT_STATUS ft_status;  // for status report(error,io status etc)
   ft_status = FT_Open(0,&ft_handle)//open a connection 
   if(ft_status == FT_OK) //error checking
        printf("\n\n\tConnection with FT 232 successfull\n");
        printf("\n\n\tConnection Failed !");
        printf("\n\tCheck device connection");
    FT_Close(ft_handle);    //Close the connection        

A little bit about the above code

#include <windows.h>  ->   Defines the various data types that are required by the ftd2xx.h,If you exclude it will result in a lot of errors.

#include "ftd2xx.h"  -> Defines the various data types and function prototypes for ftd2xx.lib.

FT_Open(0,&ft_handle) ->  Returns a handle which is used for future communications with the chip .If FT_Open() is successful it returns a FT_OK,Which is used to print the messages.

FT_HANDLE            -> Custom data types defined in ftd2xx.h for creating a handle . 

FT_STATUS            -> custom data types defined in ftd2xx.h for error codes.

FT_Close(ft_handle)  -> Used to close the connection with the chip identified by the handle (ft_handle).


Open cmd.exe and navigate to the ftdi folder .
Compile the code by typing 

    C:\> gcc – o ftdi  ftdi.c  ftd2xx.lib

It would create an exe file of the name ftdi. Before running the ftdi.exe Ensure that a working FT232 board is connected to the USB port of your computer. Check in the Device Manager whether the board is getting detected or not.Now run the exe file by typing ftdi on the command prompt. If the board is working OK ,you will get a “Connection with FT232 successful” message.

compiling ftd2xx.lib using gcc and running it on Windows

Now we have successfully communicated with the FT232 chip using the D2XX library.


Back to Main Page                                                                     Next Tutorial