title: Debugging pySerial write timeout
body: <p>How do you determine what caused PySerial write to time out? I am having a problem where PySerial begins throwing <code>Write timeout</code> exceptions after running for a variable amount of time. It can run fine for a few minutes (at approx. 200 bytes/sec) and then fail. </p>
  
  <p>All handshaking is disabled.</p>
  
  <p>Prior to the write timeout, I have observed that no more data is presented on the TX data pin. A few seconds later the timeouts begin. If I disable writeTimeout, then the code runs continually with no new data sent out on the TX pin.</p>
  
  <p>I see in the pySerial code that it just passes the written data to an <code>os.write()</code> call. What would cause this to reliably timeout? What would cause it to <em>not</em> timeout, but not present any data on the output pin?</p>
  
  <hr>
  
  <p>Specific information about my application below:</p>
  
  <hr>
  
  <p>I am sending data out over a FTDI adapter to an Arduino board (which emulates some dynamixel servos).  I am monitoring the rx/tx lines with a logic analyzer, so I can see what is actually making it through the FTDI adapter.  </p>
  
  <p>I am running on Ubuntu 14.04 in a VM.</p>
  
  <p>The code is from the <a href="http://wiki.ros.org/dynamixel_controllers" rel="noreferrer">Dynamixel Controller node for ROS</a>. I have been making small changes in order to debug the problem.</p>
  
  <p>Write method, with print statements added for debugging:</p>
  
  <pre><code>def __write_serial(self, data):
      print "TX"
  
      if self.ser is None:
          print "not open"
  
      self.ser.flushInput()
      self.ser.flushOutput()
      try:
          n = self.ser.write(data)
          print n
      except Exception as e:
          print e
  </code></pre>
  
  <p>Read method:</p>
  
  <pre><code>def __read_response(self, servo_id):
      data = []
      print "RX"
  
      try:
          data.extend(self.ser.read(4))
          print data
          if not data[0:2] == ['\xff', '\xff']: 
              print "RX ERR1"
              raise Exception('Wrong packet prefix %s' % data[0:2])
          data.extend(self.ser.read(ord(data[3])))
          data = array('B', ''.join(data)).tolist() # [int(b2a_hex(byte), 16) for byte in data]
      except Exception, e:
          print "RX ERR2"
          raise DroppedPacketError('Invalid response received from motor %d. %s' % (servo_id, e))
  
      # verify checksum
      checksum = 255 - sum(data[2:-1]) % 256
      if not checksum == data[-1]: raise ChecksumError(servo_id, data, checksum)
  
      return data
  </code></pre>
  
  <p>And the serial port declaration. All handshaking is off.</p>
  
  <pre><code>def __init__(self, port, baudrate, readback_echo=False):
      """ Constructor takes serial port and baudrate as arguments. """
      try:
          self.serial_mutex = Lock()
          self.ser = None
          self.ser = serial.Serial(port)
          #self.ser.setTimeout(0.015)
          self.ser.setTimeout(0.05)
          self.ser.baudrate = baudrate
          self.ser.xonxoff = False
          self.ser.rtscts = False
          self.ser.dsrdtr = False
          self.ser.writeTimeout = 1
          self.port_name = port
          self.readback_echo = readback_echo
      except SerialOpenError:
         raise SerialOpenError(port, baudrate)
  </code></pre>
  
  <p>The program begins running as expected. At some point (happens sooner at a higher packet rate), no more data appears on the TX output of the FTDI adapter.  Here is the output from the last good packet and the first bad packet:</p>
  
  <pre><code>TX
  8
  RX
  ['\xff', '\xff', '\x15', '\x13']
  TX
  8
  RX
  []
  RX ERR1
  RX ERR2
  </code></pre>
  
  <p>The <code>self.ser.write(data)</code> call returns 8 (for the 8-byte packet) both times, but data only appears on the output of the FTDI adapter for the first call.</p>
  
  <p>After about 3 sec of trying to send data (at approx. 200 byte/sec), the <code>Write timeout</code> exceptions are thrown.</p>
  
  <pre><code>TX
  Write timeout
  RX
  []
  RX ERR1
  RX ERR2
  </code></pre>
  
  <p>In summary:</p>
  
  <ul>
  <li>Code runs fine for many seconds/minutes</li>
  <li>Data ceases to be output on TX pin of FTDI, no exceptions raised</li>
  <li>After a few seconds, code begins raising <code>Write Timeout</code> exceptions</li>
  </ul>
  
  <p>How can I debug this problem? </p>