linux - 为什么当我更改终端属性时,linux 中的虚拟端口不会改变行为?
问题描述
我尝试使用 linux 中的虚拟串行端口测试串行通信。这些是我要做的步骤:
- 创建虚拟端口:
socat -d -d pty,raw,echo=0 pty,raw,echo=0 &
分配/dev/pts/1
和/dev/pts/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
- 开始他们之间的交流:
// from thread 1:
write(fd1, "test\n", 5);
// from thread 2:
read(fd2, buffer, MAX_RECEIVED_LEN);
并且由于某种原因,尽管波特率不同,但通信成功。即使我更改任何其他属性,我仍然可以工作。为什么它不失败?是虚拟端口不模拟真正的串行通信吗?
解决方案
推荐阅读
- kdb - 如何在更新语句中使用每行的值设置函数参数?
- python - f2py 编译失败:找不到库“gomp”
- java - 如何解决通用实例化问题?
- php - 帖子页面加载空白
- python - Kivy - 用户界面问题
- javascript - 如何在每次点击事件上自动增加 AJAX url 页码?
- c# - 容器中 AWS ECS/EC2 上的 Asp.Net Core 文件上传
- ibeacon-android - 更改 ScheduledScanJob 的扫描间隔?
- jenkins - Jenkins 加载作业配置页面的时间过长
- grails - WAR打包错误:TypeError:在对象中找不到函数绑定