I am opening a PTY (in Python/Linux) and writing to it. I can read from it via minicom
perfectly. But, I can't read from it in another Python (or C++) program. Here is a minimized example:
producer.py (opens pty / writes to it):
import os, sys
from time import sleep
master_fd, slave_fd = os.openpty()
print "minicom -D %s" % os.ttyname( slave_fd )
for i in range(0,30):
d = str(i % 10)
os.write( master_fd, d )
sys.stdout.write( d )
sys.stdout.flush()
sleep( 2 )
os.close( slave_fd )
os.close( master_fd )
print "\nDone"
consumer.py (tries to open/read):
import os, sys
from time import sleep
pts=raw_input("Enter pts number:")
while True:
fd=0
try:
fd=os.open('/dev/pts/%d' % (pts,),
os.O_RDONLY | os.O_NONBLOCK )
sys.stdout.write( os.read(fd, 1 ) )
sys.stdout.flush()
except Exception as e: print e
if fd: os.close(fd)
sleep(1)
The result of the read is always:
[Errno 11] Resource temporarily unavailable
If I read in blocking mode, it just blocks until the producer terminates. Then, it says the file doesn't exist.
I have spent days fiddling with trying to set various modes, permissions, locks, etc. and nothing seems to get me anywhere. This sort of thing works fine with regular files easily. Also, note again that minicom can read the pty without a hitch. Further, using lsof
I can see that both minicom and my consumer.py script indeed open the file - it is just the read that doesn't work in the python example. So what's the minicom secret? I tried finding such in the minicom source code, but I did not succeed in finding anything I could use.
Ideally, my producer would make it easy to open and read (like in my consumer example), but if I can see this work, I'm open to modifying either end...