title: Pyserial does not play well with virtual port
body: <p><strong>Motivation</strong></p>
  
  <p>I want to start leraning how to use the python library <a href="https://github.com/pyserial/pyserial" rel="noreferrer">Pyserial</a>. It seems like a really nice library that works for a lot of people. I want to use it for an upcoming project in which I have to automate serial communications.</p>
  
  <p><strong>Environment</strong></p>
  
  <p>I'm running Ubuntu 15.04. I'm using Python 2.7.</p>
  
  <p><strong>Setting up virtual ports</strong></p>
  
  <p>I don't currently have a device that I can communicate with over a serial port. I'm using the <a href="http://www.dest-unreach.org/socat/" rel="noreferrer">socat</a> application to create two virtual ports that are connected to each other with a baudrate of 9600.</p>
  
  <pre><code>$ socat -d -d pty,raw,echo=0,b9600 pty,raw,echo=0,b9600
  2016/01/16 12:57:51 socat[18255] N PTY is /dev/pts/2
  2016/01/16 12:57:51 socat[18255] N PTY is /dev/pts/4
  2016/01/16 12:57:51 socat[18255] N starting data transfer loop with FDs [5,5] and [7,7]
  $ echo "hello" &gt; /dev/pts/2
  $ cat /dev/pts/4
  hello
  </code></pre>
  
  <p>Great! It seems like the ports work!</p>
  
  <p><strong>A simple pyserial script</strong></p>
  
  <p>I install pyserial using pip</p>
  
  <pre><code>$ sudo pip install pyserial
  </code></pre>
  
  <p>Then I wrote a little serialtest.py</p>
  
  <pre><code>#!/usr/bin/env python
  import serial
  
  ser = serial.Serial('/dev/pts/2', 9600)
  </code></pre>
  
  <p>That is the entirety of serialtest.py</p>
  
  <p><strong>Running the script and encountering an error</strong></p>
  
  <pre><code>$ python serialtest.py 
  Traceback (most recent call last):
    File "serialtest.py", line 4, in &lt;module&gt;
      ser = serial.Serial('/dev/pts/2')
    File "/home/sbl/.local/lib/python2.7/site-packages/serial/serialutil.py", line 180, in __init__
      self.open()
    File "/home/sbl/.local/lib/python2.7/site-packages/serial/serialposix.py", line 311, in open
      self._update_dtr_state()
    File "/home/sbl/.local/lib/python2.7/site-packages/serial/serialposix.py", line 605, in _update_dtr_state
      fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
  IOError: [Errno 22] Invalid argument
  </code></pre>
  
  <p>What's up with that?</p>
  
  <p><strong>Unsuccessful attempts at debugging</strong> </p>
  
  <p><a href="https://stackoverflow.com/a/5498780/1113637">This guy</a> said he had success when using python 2.6. I couldn't get Pyserial to work with 2.6.</p>
  
  <p><a href="https://github.com/foosel/OctoPrint/issues/230" rel="noreferrer">This guy</a> was having trouble with is baudrate. I double check my baudrate with the command <code>$stty -F /dev/pts/2</code> and confirmed that it was, in fact, at a baudrate of 9600.</p>
  
  <p><a href="https://github.com/daid/Cura/issues/20" rel="noreferrer">This guy</a> also claims to have problems with baudrate and attributes it to his kernel. That was back in 2012, so I don't think it's relevant anymore.</p>
  
  <p><strong>My Question</strong></p>
  
  <p>How can I get my serialtest.py script to run without error?</p>