1

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

我已经成功地编写了代码来使用 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 熄灭。连接到 RTS 和 DTR 的 LED 最初为 ON。

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

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

为什么 DTR 会随着 RTS 线变低?我也无法通过使用 TIOCM_CD、TIOCM_DTR 等来更改其他调制解调器线路,如 RI、DCD、DCD、DTR 等?

4

1 回答 1

1

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

ioctl(fd, TIOCMSET, &status);
于 2015-08-17T13:30:41.153 回答