“pty模块”操作系统读取“师父的意思是读刚刚写给玛斯的东西

2024-09-27 07:17:31 发布

您现在位置:Python中文网/ 问答频道 /正文

更新:这个问题已经解决了。pty在规范模式下初始化伪设备,必须禁用echo。见this reply。你知道吗

以下单元测试通过:

def test_pty(self):
  mfd, sfd = pty.openpty()
  pty.tty.B38400
  devName = os.ttyname(sfd)

  ser = serial.Serial(devName, baudrate=38400, timeout=1)

  test_str = b'testing'
  os.write(mfd, test_str)

  time.sleep(1)
  self.assertEqual(ser.readline(), test_str)

显示写入主伪设备的字节在从伪设备上读取。你知道吗

但是,如果我切换两个语句的顺序:

def test_pty(self):
  mfd, sfd = pty.openpty()
  pty.tty.B38400
  devName = os.ttyname(sfd)

  test_str = b'testing'
  os.write(mfd, test_str) # Write first

  ser = serial.Serial(devName, baudrate=38400, timeout=1) # Then set up serial

  time.sleep(1)
  self.assertEqual(ser.readline(), test_str)

然后就失败了。ser.readline()读取零字节。你知道吗

我注意到以下情况也发生了:

def test_pty(self):
  mfd, sfd = pty.openpty()
  pty.tty.B38400
  devName = os.ttyname(sfd)

  test_str = b'testing'
  os.write(mfd, test_str) # Write to master

  self.assertTrue(test_str, os.read(mfd, len(test_str)) # Read from master

从本质上讲,如果在我建立到从设备的串行连接之前,我os.write到主设备,那么字节会被回显/写入(?)在主伪终端而不是从终端上

请帮助我理解这是为什么。你知道吗

(Python 3.4.8版)

编辑: 用pseudo-device替换pty,因为pty是一对设备


Tags: testselfosdefserwriteptysfd

热门问题