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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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. pthread_yield example in c
  2. oe-pkgdata-util utility in Yocto
  3. make: Nothing to be done for `default’
  4. StringCchCat Function example
  5. Basics of C Structures
  6. C Operators and Type Conversion
  7. C Functions
  8. C Operators
  9. Introduction to Array in C Programming
  10. Beginners Guide to C Structures: Definition, creation and manipulation

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright