Linux下串口的RTS控制
#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> #include <sys/ioctl.h> void set_rts(int fd, int state) { int status; ioctl(fd, TIOCMGET, &status); // 获取当前的串口控制信号状态 if (state) { status |= TIOCM_RTS; // 设置 RTS 为高电平 } else { status &= ~TIOCM_RTS; // 设置 RTS 为低电平 } ioctl(fd, TIOCMSET, &status); // 将修改后的状态设置回去 } int main() { int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); // 打开串口设备,这里以 /dev/ttyS0 为例 if (fd < 0) { perror("open"); return -1; } int state = 0; // 初始状态为低电平 while (1) { set_rts(fd, state); // 设置 RTS 信号 state = !state; // 切换状态 sleep(1); // 等待一秒 } close(fd); // 关闭串口设备 return 0; }
gcc rts.c -o rts