title: Python reading and writing to tty
body: <p>BACKGROUND: If you want, skip to the problem section</p>
  
  <p>I am working on a front end for test equipment. The purpose of the front end is to make it easier to write long test scripts. Pretty much just make them more human readable and writable.</p>
  
  <p>The equipment will be tested using a Prologix GPIB-USB Controller (see prologix.biz). We found a tutorial at <a href="http://heliosoph.mit-links.info/gpib-on-debian-linux-the-easy-way/" rel="noreferrer">http://heliosoph.mit-links.info/gpib-on-debian-linux-the-easy-way/</a> and did all of the steps, and it worked!</p>
  
  <p>As we don't have the test equipment yet, we wanted to write an emulator in Python using openpty. We do have the GPIB-USB Controller, just not what gets connected to that. I got the emulator working as a perfect replacement for the GPIB-USB. This means that I would follow the "GPIB on Debian ..." tutorial (above) and get output that I programmed the emulator to return. The input and output were done in the same manner as the tutorial just reading and writing to/from a pty device (ie /dev/pts/2) instead of the tty (ie /dev/ttyUSB0).</p>
  
  <p>Now that the emulator works, we want to write a front end that can be used to write scripts easily. The goal is to make a kind of macro system that writes a bunch of commands when we call a function.</p>
  
  <p>PROBLEM: exists using both the emulator and the device</p>
  
  <p>I am using the following Python functions to read, write, and open the tty/pty devices, but I am not getting the same result that I get if I just use echo and cat in bash.</p>
  
  <pre><code>tty = os.open(tty_path, os.O_RDWR)
  os.read(tty, 100)
  os.write(tty, "++ver")
  </code></pre>
  
  <p>for example, I would expect the following to be equivalent</p>
  
  <pre><code>$ cat &lt; /dev/pty/2 &amp;   # According to the tutorial, this must be run in parallel
  $ echo "++ver" &gt; /dev/pty/2
  Prologix GPIB Version 1.2.3.4 ...
  </code></pre>
  
  <p>and</p>
  
  <pre><code>tty = os.open("/dev/pyt/2", os.o_RDWR)
  os.read(tty, 100) # In separate Thread to be run in parallel
  os.write(tty, "++ver") # in main thread
  </code></pre>
  
  <p>The output is very different, please explain why and how I can fix it.</p>
  
  <p>FULL CODE is here: <a href="http://pastebin.com/PWVsMjD7" rel="noreferrer">http://pastebin.com/PWVsMjD7</a></p>