Controlling DTR and RTS pins of Serial Port using Win32 API

In the previous tutorial we learned about communicating with the serial port (reading and writing) using Win 32 API. In this tutorial we will look into another function that will let us control two more pins, RTS and DTR. Unlike RXD and TXD pins that are used for sending and receiving data, RTS (Request To Send) and DTR (Data Terminal Ready) pins are used for signaling purposes.

Some of the microcontroller programmers use either the DTR line or RTS line of the PC serial port to reset the microcontroller during the programming operation.

Another use is to control the RS485 chip in USB to RS485 converter for eg In USB2SERIAL, which can also act as a USB to RS485 converter.

~RTS pin is used to control the ~RE pin of MAX485 to put it in receive mode
~DTR pin is used to control the DE pin of MAX485 to put it in transmit mode

if you are interested you can find the complete description here.


link to xanthium enterprises repo on Github containing code for controlling RTS and DTR Pins of Serial Port

All the C sourefiles used in this tutorial can be downloaded from our GitHub Page.


Controlling the RTS and DTR pins

Windows uses EscapeCommFunction() to control the extended functions of the serial port like setting and clearing the RTS and DTR Pins.

EscapeCommFunction() takes two arguments,first one is the handle of the opened serial port and the other one is DWORD representing the Extended function to be performed.

Following are the extended function values concerned to RTS and DTR.

extended function values concerned to RTS and DTR for EscapeCommFunction() in win32

In order to control the RTS and DTR pins, first we have to open a connection to the serial port using CreateFile() and get its handle.

hComm = CreateFile();

Please note that detailed description about opening the serial port using CreateFile() is explained in the previous article.

After that you can use EscapeCommFunction() to perform the desired operation,here we are going to set the RTS line to HIGH..

Status = EscapeCommFunction(hComm, SETRTS);

Please note that if you are using a USB to Serial Converter chip like FT232 (as in USB2SERIAL) both RTS and DTR lines are inverted internally, So Setting them would make the Pin LOW.

You can Clear the RTS pin buy using CLRRTS.

Status = EscapeCommFunction(hComm, CLRRTS);

if you replace the above commands with SETDTR and CLRDTR you can set and Clear the DTR pins too.

Please do not forget to close the Serial Connection after you have finished.

One way to test out the code is to connect LED's to the serial port and observe. If you are using a DB9 hardware serial port, Please use an appropriate RS232 decoder chip or high value resistor.

If you are using a USB2SERIAL converter or any FT232 based converter, Please connect a 1K resistor in series with the RTS pin. Please note that the RTS pins available on the USB2SERIAL converter board are inverted internally.

Program for controlling both the RTS and DTR lines running on windows 7

The zip file contains the code samples for controlling both the RTS and DTR lines.You can compile the code using GCC or Visual Studio Express.
Please remember to change the COM port number to the one corresponding to your System.

Photo of USB2SERIAL connected to LED's through 1K resistors.Ground is taken from USB2SERIAL connector (P4)

controlling rts and dtr pis of ft232 usb to serial converter using win32 API


Back to Serial Programming Tutorials