• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Serial Port Programming – Reading/Writing Status of Control Lines: DTR/RTS/CTS/DSR

by admin

tiocmget and tiocmset

In the 2.4 and older kernels, there used to be a number of tty ioctl calls to get and set the different control line settings. These were denoted by the constants TIOCMGET, TIOCMBIS, TIOCMBIC, and TIOCMSET. TIOCMGET was used to get the line setting values of the kernel, and as of the 2.6 kernel, this ioctl call has been turned into a tty driver callback function called tiocmget. The other three ioctls have been simplified and are now represented with a single tty driver callback function called tiocmset.

If the user is interested in finding out the status of Control Lines:DTR/DSR/RTS/CTS, he can use ‘TIOCMGET’ control code in the ioctl call. ‘TIOCMSET’ control code in ioctl allows you to set/clear DTR and RTS lines as they are output.

Example Code

Consider the example shown below:

#include <stdio.h>
#include <sys/types.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define SERIAL_DEVICE	"/dev/ttyUSB0"
int set_DTR(int fd, unsigned short level)
{
	int status;

	if (fd < 0) {
		perror("Set_DTR(): Invalid File descriptor");
		return -1;
	}

	if (ioctl(fd, TIOCMGET, &status) == -1) {
		perror("set_DTR(): TIOCMGET");
		return -1;
	}

	if (level) 
		status |= TIOCM_DTR;
	else 
		status &= ~TIOCM_DTR;

	if (ioctl(fd, TIOCMSET, &status) == -1) {
		perror("set_DTR(): TIOCMSET");
		return -1;
	}
	return 0;

}

int set_RTS(int fd, unsigned short level)
{
	int status;

	if (fd < 0) {
		perror("Invalid File descriptor");
		return -1;
	}

	if (ioctl(fd, TIOCMGET, &status) == -1) {
		perror("set_RTS(): TIOCMGET");
		return -1;
	}

	if (level) 
		status |= TIOCM_RTS;
	else 
		status &= ~TIOCM_RTS;

	if (ioctl(fd, TIOCMSET, &status) == -1) {
		perror("set_RTS(): TIOCMSET");
		return -1;
	}
	return 0;
}


int main()
{
	int fd;
	int retval;
	int serial;

	fd = open(SERIAL_DEVICE, O_RDWR);
	if (fd < 0) {
		perror("Failed to open SERIAL_DEVICE");
		exit(1);
	}
	
	retval = ioctl(fd, TIOCMGET, &serial);
	if (retval < 0) {
		perror("ioctl() failed");
		exit(0);
	}

	if (serial & TIOCM_DTR)
		printf("%s:DTR is set\n", SERIAL_DEVICE);
	else
		printf("%s:DTR is not set\n", SERIAL_DEVICE);

	if (serial & TIOCM_LE)
		printf("%s:DSR is set\n", SERIAL_DEVICE);
	else
		printf("%s:DSR is not set\n", SERIAL_DEVICE);

	if (serial & TIOCM_RTS)
		printf("%s:RTS is set\n", SERIAL_DEVICE);
	else
		printf("%s:RTS is not set\n", SERIAL_DEVICE);

	if (serial & TIOCM_CTS)
		printf("%s:CTS is set\n", SERIAL_DEVICE);
	else
		printf("%s:CTS is not set\n", SERIAL_DEVICE);

	if (set_RTS(fd, 0)) {
		printf("%s: Failed to set RTS\n", SERIAL_DEVICE);
		exit(1);
	}
	if (set_DTR(fd, 0)) {
		printf("%s: Failed to set DTR\n", SERIAL_DEVICE);
		exit(1);
	}
	retval = ioctl(fd, TIOCMGET, &serial);
	if (retval < 0) {
		perror("ioctl() failed");
		exit(0);
	}
	if (serial & TIOCM_RTS)
		printf("%s:RTS is set\n", SERIAL_DEVICE);
	else
		printf("%s:RTS is not set\n", SERIAL_DEVICE);
	if (serial & TIOCM_DTR)
		printf("%s:DTR is set\n", SERIAL_DEVICE);
	else
		printf("%s:DTR is not set\n", SERIAL_DEVICE);
	return 0;
}

Output:

Reading/Writing Status of Control Lines: DTR/RTS/CTS/DSR

Filed Under: C, C Library

Some more articles you might also be interested in …

  1. How to use execl (example included)
  2. C Operators
  3. clang: Compiler for C, C++, and Objective-C source files. Can be used as a drop-in replacement for GCC
  4. Basics of C Arrays
  5. Serial Port Programming: tcflush – TCIFLUSH,TCOFLUSH example
  6. C if, if-else and switch statements
  7. C while, do-while and for Loops
  8. Basics of C programming
  9. make: Nothing to be done for `default’
  10. C Pointers

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright