嗨,我正在编写一个小代码来控制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 最初处于打开状态。
使用"sudo ./serial"运行代码时
RTS 和 DTR Led 都熄灭了,而不仅仅是 RTS(编码状态 |= TIOCM_RTS;)并在 getchar() 之后打开。
为什么DTR随着RTS线而变低?也我无法使用TIOCM_CD,TIOCM_DTR等更改其他调制解调器线路,如RI,DCD,DCD,DTR等?
对于TIOCMSET
命令,请将最后一个参数作为引用:
ioctl(fd, TIOCMSET, &status);