使用ioctl()设置串口的DTR RTS引脚时遇到问题在linux中调用

时间:2014-12-28 02:55:17

标签: linux serial-port ioctl ftdi termios

您好我正在编写一个小代码来控制Linux上的USB到串口转换器芯片FT232的DTR和RTS线路(Mint Linux 13 Maya,x86)。

我已经成功编写了使用termios读写数据到FT232芯片的代码。 现在我想控制DTR和RTS线,所以我使用ioctl()调用来设置和清除DTR和RTS线。

这是代码

    #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);
     }

代码在gcc上成功编译没有任何错误。我已经将两个LED连接到FT232的RTS和DTR线。由于RTS和DTR线被反转,设置RTS会使LED关闭。 Led与RTS和DTR的连接最初都是ON。

使用“sudo ./serial”运行代码

RTS和DTR Led都关闭,而不仅仅是RTS(编码状态为| = TIOCM_RTS;) 并在getchar()之后打开。

为什么DTR与RTS线一起变低? 也 我无法使用TIOCM_CD,TIOCM_DTR等更改RI,DCD,DCD,DTR等其他调制解调器线路?

1 个答案:

答案 0 :(得分:1)

对于TIOCMSET命令,将最后一个参数作为参考发送:

ioctl(fd, TIOCMSET, &status);