Pseudoterminal master reads what it has just written

前端 未结 2 520
轻奢々
轻奢々 2021-02-06 02:22

I\'m working on a project that interfaces \"virtual devices\" (python processes) that use serial port connections with real devices that also use serial ports, and I\'m using ps

2条回答
  •  有刺的猬
    2021-02-06 03:14

    In case someone finds this question, and jszakmeister's answer doesn't work, here is what worked for me.

    openpty seems to create pty's in canonical mode with echo turned on. This is not what one might expect. You can change the mode using the tty.setraw function, like in this example of a simple openpty echo server:

    master, slave = os.openpty()
    tty.setraw(master, termios.TCSANOW)
    print("Connect to:", os.ttyname(slave))
    
    while True:
        try:
            data = os.read(master, 10000)
        except OSError:
            break
        if not data:
            break
        os.write(master, data)
    

提交回复
热议问题