首页 > 解决方案 > 为什么当我更改终端属性时,linux 中的虚拟端口不会改变行为?

问题描述

我尝试使用 linux 中的虚拟串行端口测试串行通信。这些是我要做的步骤:

  1. 创建虚拟端口: socat -d -d pty,raw,echo=0 pty,raw,echo=0 & 分配/dev/pts/1/dev/pts/2
  2. 从双方打开连接并设置不同的波特率:
// from thread 1:
int fd1 = open("/dev/pts/1", O_RDWR | O_NOCTTY);
int baudrate = B115200;
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = baudrate | CRTSCTS | CLOCAL | CREAD;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);

// from thread 2:
int fd2 = open("/dev/pts/2", O_RDWR | O_NOCTTY);
int baudrate = B9600;
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = baudrate | CRTSCTS | CLOCAL | CREAD;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);

// notice that the baudrate is configured different
  1. 开始他们之间的交流:
// from thread 1:
write(fd1, "test\n", 5);

// from thread 2:
read(fd2, buffer, MAX_RECEIVED_LEN);

并且由于某种原因,尽管波特率不同,但通信成功。即使我更改任何其他属性,我仍然可以工作。为什么它不失败?是虚拟端口不模拟真正的串行通信吗?

标签: linuxserial-portvirtual-serial-port

解决方案


推荐阅读