更新:这个问题已经解决了。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
是一对设备
目前没有回答
相关问题 更多 >
编程相关推荐