Question

I am currently trying to send binary data out through pexpect. For some reason, the data gets through just find except for a 0x04, which is just skipped over. I tracked down the pexpect call to determine that all thats happening is an os.write() call to a file descriptor opened from a pty.fork() command.

Any ideas?

(example code that exemplifies the problem)

import os, pty, sys


pid, child_fd = pty.fork()

if pid: # Parent
    os.write(child_fd, b"'\x04hmm\x04'\n")
    buf = os.read(child_fd, 100)
    print buf
else:   # Child
    text = sys.stdin.readline()
    print ''.join(["%02X " % ord(x) for x in text])

Result:

$ python test.py
'hmm'
27 68 6D 6D 27 0A
Was it helpful?

Solution

0x04 is ^D, which is the end-of-file keypress. Has the pty been set in raw mode? Maybe the driver is eating it.

If you make it:

os.write(child_fd, b"'\x04hmm\x16\x04'\n")

you can see that indeed the driver is doing translation. \x16 is the same as ^V, which is how you quote things. It makes sense the translation would only be happening from the master (the pretend physical terminal) and the slave. The pretend physical terminal is where (on a normal terminal device) the person would be typing

I'm not sure how to get the driver to stop doing that. If the child sets its terminal to raw mode then that will likely do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top