我正在尝试创建一个模拟TTY连接的目标设备,我可以通过minicom连接。我正在使用/dev/ptmx
创建一个pty并打印出要由minicom打开的从属名称:
Please connect to: /dev/pts/4
。在python方面,我使用操作系统读取以及操作系统写入执行io并模拟我的目标:
import os, re, termios
from ctypes import *
class dev():
def __init__(self):
pass
def createpty(self):
self.fd3 = os.open("/dev/ptmx", os.O_RDWR | os.O_NONBLOCK);
if self.fd3 < 0:
print("Couldn't open output /dev/ptmx\n")
libc = cdll.LoadLibrary("libc.so.6")
libc.grantpt(self.fd3);
libc.unlockpt(self.fd3);
libc.ptsname.restype = c_char_p
self.slave = libc.ptsname(self.fd3)
print("Please connect to:" + self.slave);
self.old = termios.tcgetattr(self.fd3)
n = termios.tcgetattr(self.fd3)
n[3] = n[3] & ~(termios.ECHO|termios.ICANON) # c_lflag
n[3] = n[3] & 0
n[4+1] = n[4+1] & 0xffff0000;
termios.tcsetattr(self.fd3, termios.TCSANOW, n)
整个过程相当好斗。我想在python端使用select
,这会迫使我再次使用ctypes。我包不住这件衣服自身.fd3因为我需要防止close
被调用自身.fd3. 你知道吗
所以我有两个问题:
正如@CristiFati所说的,Python的select可以用于FDs,但是为了完整起见,下面是我使用ctype的select的拙劣技巧。这实现了一个tty echo服务器。连接到打印出来的pty slave,即
minicom -D /dev/pty/4
相关问题 更多 >
编程相关推荐