gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:54:20 33 4
gpt4 key购买 nike

您好,我正在编写一个小代码来控制 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);

关于linux - 在 linux 中使用 ioctl() 调用设置串口的 DTR RTS 引脚时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27673344/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com