我正在编写一个应用程序,通过串口与外部板通信。在这个阶段,我需要测试串行协议,但是外部硬件还不能使用。有办法在我的应用程序的(虚拟)串口上打开终端吗?
发布于 2010-10-26 15:23:21
使用"ptmx“接口可能是最好的选择。下面是一个示例程序,它将附加到/dev/ptmx,并触发可以从应用程序附加到的/dev/pts/N设备节点的创建。
有关详细信息,请参阅“男子pty”。
#!/usr/bin/python
# Spawn pseudo-tty for input testing.
# Copyright 2010, Canonical, Ltd.
# Author: Kees Cook <kees@ubuntu.com>
# License: GPLv3
import os, sys, select
parent, child = os.openpty()
tty = os.ttyname(child)
os.system('stty cs8 -icanon -echo < %s' % (tty))
print tty
try:
os.system('stty cs8 -icanon -echo < /dev/stdin')
poller = select.poll()
poller.register(parent, select.POLLIN)
poller.register(sys.stdin, select.POLLIN)
running = True
while running:
events = poller.poll(1000)
for fd, event in events:
if (select.POLLIN & event) > 0:
chars = os.read(fd, 512)
if fd == parent:
sys.stdout.write(chars)
sys.stdout.flush()
else:
os.write(parent, chars)
finally:
os.system('stty sane < /dev/stdin')当您运行这个程序时,它会告诉您要在应用程序中附加到的it的名称,并且您只需输入终端就可以模拟硬件。
https://askubuntu.com/questions/9396
复制相似问题