Home:ALL Converter>Trouble in setting DTR RTS pins of serial port using ioctl() Call in linux

Trouble in setting DTR RTS pins of serial port using ioctl() Call in linux

Ask Time:2014-12-28T10:55:17         Author:rohkumarj

Json Formatter

Hi i am writing a small code to control the DTR and RTS lines of USB to Serial port Converter chip FT232 on Linux (Mint Linux 13 Maya,x86).

I have successfully wrote code to read and write data to the FT232 chip using termios. Now i want to control the DTR and RTS lines so i am using ioctl() call to set and clear the DTR and RTS lines.

here is the code

    #include <stdio.h>
    #include <fcntl.h>       /* File Control Definitions           */
    #include <termios.h>     /* POSIX Terminal Control Definitions */
    #include <unistd.h>      /* UNIX Standard Definitions          */ 
    #include <errno.h>       /* ERROR Number Definitions           */
    #include <sys/ioctl.h>   /* ioctl()                            */

    main(void)
    {
        int fd;     /*File Descriptor*/
        int status; 

        fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY ); //Opening the serial port

        ioctl(fd,TIOCMGET,&status); /* GET the State of MODEM bits in Status */
        status |= TIOCM_RTS;        // Set the RTS pin
        ioctl(fd, TIOCMSET, status);

        getchar(); //To view the change in status pins before closing the port

        close(fd);
     }

The code compiles successfully on gcc without any errors.I have connected two leds to RTS and DTR lines of FT232.Since the RTS and DTR lines are inverted,setting RTS would make the LED off. Led's connected to RTS and DTR are intially ON.

On running the code using "sudo ./serial"

both RTS and DTR Led's go off, instead of just RTS (as coded status |= TIOCM_RTS;) and switch on after getchar().

Why is DTR going LOW along With RTS line? also I am not able to change the other modem lines like RI,DCD,DCD,DTR etc by using TIOCM_CD ,TIOCM_DTR etc?

Author:rohkumarj,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27673344/trouble-in-setting-dtr-rts-pins-of-serial-port-using-ioctl-call-in-linux
yy