Controlling the RTS and DTR pins of Serial Port in Linux

Welcome to the second part of the tutorial on how to configure the Serial Port on Linux.In this section we will learn to control the RTS and DTR pins of the Serial Port using ioctl() system call.

In the previous section ,I have explained how to configure the serial port using the termios API . Termios API is fine for reading and writing to the serial port ,now if you want to use MODEM pins like RTS or DTR you have to use ioct() calls.


link to xanthium enterprises repo on Github containing code for controlling RTS and DTR  pins of linux serial port

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

Code Explanation

ioctl() calls (I/O Control) are generic calls that can be used to communicate with wide variety of hardware devices on a Linux system.We are going to use ioct() calls to set and clear the RTS and DTR pins .The basic format of the ioctl() call is as follows



fd is the file descriptor pointing to the device which we want to control,In our case Serial Port.
command is a number representing an operation like set or clear a pin.
argument is the corresponding argument for the command ,like which pin to set.

Now lets create a small program to control RTS pins of the Serial port.

//Other header files
#include <sys/ioctl.h//ioctl() call defenitions
   int fd;
   fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY );//Open Serial Port
   int RTS_flag;
   RTS_flag = TIOCM_RTS;
   ioctl(fd,TIOCMBIS,&RTS_flag);//Set RTS pin
   ioctl(fd,TIOCMBIC,&RTS_flag);//Clear RTS pin

The definitions for ioctl() command are in sys/ioctl.h header files .Please note that for the above program to work properly you should include other header files too.You can find the complete file(rts.c) in the zip file accompanying this tutorial.

Open the serial port in read write mode using open() system call.Here i am using a FT232 based serial converter (USB2SERIAL) which is recognized by my Linux system as /dev/ttyUSB0, this may change in your system.You can check the first part of this article on how to identify your serial port.

TIOCM_RTS is a constant defined in the “termios.h” header file to identify the RTS pin of the serial port.You can open the header file to see constants corresponding to other pins too.

In Ubuntu/Linux Mint you can find the “termios.h” header file at this location “/usr/include/asm­generic/termios.h

TIOCM_RTS - Constant for RTS line
TIOCM_DTR – Constant for DTR line

fd as usual is the file descriptor pointing to the opened serial port .
TIOCMBIS is a command code send to set the bit specified by the argument &RTS_flag.
TIOCMBIC is a command code used to clear the bit specified by the argument &RTS_flag.

Both TIOCMBIS and TIOCMBIC are defined in the sys/ioctl.h header file.

If you want to control the DTR pin just replace the TIOCM_RTS with TIOCM_DTR in the above program.You can control both of them together also.

You can compile the code (rts.c) from the downloaded zip file (at the bottom of page) using gcc and run it.
Please remember to execute code as root(use sudo).​

code for controlling RTS and DTR pins of serial port running on linux ubuntu

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/DTR pins available on the USB2SERIAL converter board are inverted internally.Setting the RTS/DTR pin will clear it.Below I have included the diagram of the USB2SERIAL board showing the RTS and DTR pins.

USB2SERIAL showing all the nine pins

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

controlling rts and dtr pins of FT232 usb to serial converter on Linux

The USB2SERIAL board can be configured to act like a USB to RS485 converter enabling you to control a RS485 network from your windows or linux PC .One use of controlling the RTS and DTR pins is to create an USB to RS485 converter which can be used to control an RS485 network from your PC .

If you are interested you can check out the Cross Platform RS485 Communication using USB2SERIAL board.


Back to Serial Programming Tutorials 




Hi I downloaded your zip it works great, but when the program ends it loses the effect. How to edit the code such a way that when you last time press any key, the pins remain how they were set?

Usually after you close the connection to your port by using close(fd); Linux frees the port so that other users can use it.