Search code examples
python-3.xioraspbiantty

Why can't I read in a freshly opened TTY in Raspbian


I have a small issue with my code, running in Python 3. I'm trying to fool Raspbian, in order to make it believe a tty is an external device. However, I can't read a single word I wrote previously with os.write(slave, text.encode()), using something like os.read(slave, 512).

I open the tty as follow master, slave = os.openpty() I think I'm missing a parameter or something, but I can't find out what.

I tried accessing the tty in another terminal, with a cat <, with a subprocess, but the program still block when it has to read.

Please explain what is the problem.

Regards.


Solution

  • I think your mistake here is that you are trying to read the slave. If you read the master instead you should get your output.

    Quote from: http://www.rkoucha.fr/tech_corner/pty_pdip.html

    A pseudo-terminal is a pair of character mode devices also called pty. One is master and the other is slave and they are connected with a bidirectional channel. Any data written on the slave side is forwarded to the output of the master side. Conversely, any data written on the master side is forwarded to the output of the slave side as depicted in figure 2.

    RPI