RS485 Communication using D2XX library and USB2SERIAL

In this tutorial we will use the D2XX library from FTDI to program the USB2SERIAL board to communicate with a microcontroller board through RS485 protocol.D2XX library is a proprietary library from FTDI to access the additional features of FT232 USB to Serial converter chip.Please note that D2XX library will work only with chips manufactured by FTDI like FT232 or FT245.If you are interested in using other languages like C# or Python for RS485 communication  you can check this article.

For programming the D2XX library we will be using C language and opensource MinGW (GCC) for compiling the code on Windows operating system.A detailed explanation on how to configure MinGW (GCC) , Eclipse and Code::Blocks are already mentioned in the previous articles.

Source Code and Library

link to xanthium enterprises repo containing D2XX programming code on github

All the C sourefiles along with D2XX library used in this tutorial can be downloaded from our GitHub Page.For this Tutorial use the " Comm_RS485 " Directory inside the code repository.


Hardware used is USB2SERIAL USB to RS485 converter board and MSP430 launchpad  board interfaced with a MAX485 RS485 converter chip.The MSP430 on the Launchpad is programmed to transmit and receive a string of characters through a twisted pair RS485 cable which are received/transmitted by the program running on the PC.USB2SERIAL board  is used to interface the RS485 network with the PC.

RS485 programming using D2XX library involves the following steps.

1.  Opening a connection to Serial port using D2xx Library.
2.  Setting Data bits,Stop bits,Parity Bits.
3.  Setting Flow Control bits.
4.  Putting the Chip in CBUS bit Bang Mode.
5.  Writing/Reading a byte to serial port.
6.  Closing the Serial port Connection.

Most of the steps outlined above have been already covered in the previous tutorials like Serial Port programming using D2XX library (steps 1,2,3,5,6),CBUS Bit Bang Mode of FT232 (Step 4).Here we are using CBUS pins CBUS1 and CBUS2  to control the ~RTS and ~DTR pins of MAX485.Please make sure that the jumpers are set to the CBUS mode and the CBUS pins are configured in IO mode using FT Prog Software.

Jumper Settings of USB2SERIAL

Put the 2 jumpers of CONN1 towards the RS485 and the jumpers of CONN2 towards CBUS1 and CBUS2 as shown in the below figure.

Jumper Settings of USB2SERIAL board for RS485 communication using D2XX library

Now TXD and RXD of FT232 are routed to DI and RO pins of MAX485,

CBUS1 controls the ~RE pin of MAX485

and CBUS2 controls the DE pin of MAX485.

The USB2SERIAL communicates with a MSP430 launchpad board through a RS485 network.You can use any microcontroller board instead of MSP430 like 8051,Atmel AVR or ARM.The figure below shows the connection diagram between MSP430G2553 and USB2SERIAL.

The figure  shows the connection diagram between MSP430G2553 and USB2SERIAL.


Here i am going to discuss about the software running on the PC side (using D2XX library) ,the explanation about microcontroller (MSP430) side code can be found here.For simplicity i have divided the software code into two parts.

RS485 Write -- PC sends data to MSP430 which is then used to turn on an LED indicating that a byte has been received.

RS485 Read -- PC receives data from MSP430 board.The received data is then displayed on Terminal/Console of PC.


RS485 Write 

In RS485 Write PC side software using D2XX library sends a byte of data into the MSP430 microcontroller.The MSP430 receives the data and lights up an LED.

1) First a connection is opened using the FT_Open() function to get the handle to the FT232 chip in USB2SERIAL.When you are using the D2XX library there is no      need to provide the COM port number as FT_Open() automatically connects with the FT232 chip connected to your Windows OS.

2) Baud rate for communicating with the MSP430 chip through RS485 network is set at 9600bps using FT_SetBaudRate() function.You can also set other standard rates if you want .Please make sure that you use the corresponding baudrate on the MSP430 side also.

3) In this step we configure the no of stop bits ,no of start bits ,data bits,type of parity (Even ,Odd,None) etc using the FT_SetDataCharacteristics() function.Here we are going to use 8N1 data format (8 data bits,No Parity,One Stop bit).After that we sets the flow control to none (both hardware and software flow control are disabled).

Please note that all the above steps are already discussed in detail at the serial programming tutorial.

4) In this step we will control the MAX485 chip to put the USB2SERIAL board in either transmit or receive mode.

The CBUS1 pin of FT232 is connected to ~RE (active low )(Receive Enable) pin of MAX485 and CBUS2 pin is connected to DE(Driver Enable) pin of MAX485.

To Put the MAX485 chip in Transmit mode We will make the DE  pin HIGH,~RE is also made HIGH to inhibit the Receive mode using the CBUS2 and CBUS1 pins.To control the CBUS pins we enter into the CBUS bit bang mode using 

ft_status = FT_SetBitMode(ft_handle,Mask,Mode);

Where Mode = 0x20 , and Mask = 0x66,

Now in the previous tutorial about CBUS bit bang mode we have discussed how to set the direction and data of CBUS pins (CBUS0 - CBUS3).The figure below illustrates how we have calculated the Mask value.

calculating the mask value

Mask is an 8 bit value which is used to set the direction of CBUS pins and the 4 bit data to be send through it.The Upper nibble of Mask controls the direction of data transmission where a 1 = Output and 0 = input.Here we are setting both CBUS1 and CBUS2 as outputs.

Lower nibble of Mask sends the 4 bit data to the CBUS we are making both CBUS1 and CBUS2 high to put the MAX485 chip in transmit mode.

5) After the CBUS pins are used to put the MAX485 in Transmit mode,FT_Write() is used to send a character through the RS485 network to MSP430.The  connection is then closed by using FT_Close().


Screenshot of the program running on Windows 7 PC.

Screenshot of the program for transmitting through RS485 using D2XX library from FTDI


RS485 Read

In RS485 read program running on the PC acts as the receiver and waits for a string of characters send by the MSP430.On receiving the characters the program displays them on PC console.

The steps(1,2,3) for opening a connection to FT232,Setting Baudrate,data characteristics are same as the RS485 write section (above).After which time outs are set.

The FT232 chip is put in CBUS bit bang mode in order to put the MAX485 chip in Receive mode.

Here the Mask value = 0x60 as both DE and ~RE value = 0.

CBUS bit bang mode value for RS485 reception

After the MAX485 chip is put in receive mode ,we will create a kernel event and wait for the windows to notify us when a character is received.The process of setting up the kernel notification is similar to the one in serial port programming tutorial.The received data is then displayed on the console and the connection is closed.

The sourcecodes can be compiled using opensource GCC compiler.Please make sure that fd2xx.lib,fd2xx.h  and your source code are in the same directory.Make sure that jumper settings of USB2SERIAL board are set  correctly.

After you have compiled the code Please run it.The code will wait for any data to be transmitted from the microcontroller board.

program waiting for data to be received

Download the corresponding code (code to transmit the string "Hello" )to your microcontroller (here MSP430) and then reset the controller.

After the string is received ,it is displayed on the console window.


Previous Tutorial                                                                               Main Page