Python serial.readline() not blocking
<p>I'm trying to use hardware serial port devices with Python, but I'm having timing issues.  If I send an interrogation command to the device, it should respond with data.  If I try to read the incoming data too quickly, it receives nothing.</p>

<pre><code>import serial
device = serial.Serial("/dev/ttyUSB0", 9600, timeout=0)
device.flushInput()
device.write("command")
response = device.readline()
print response
''
</code></pre>

<p>The <code>readline()</code> command isn't blocking and waiting for a new line as it should.  Is there a simple workaround?</p>

<p>I couldn't add a commend so I will just add this as an answer. You can reference <a href="http://stackoverflow.com/questions/13017840/using-pyserial-is-it-possble-to-wait-for-data">this stackoverflow thread</a>. Someone attempted something similar to your question.</p>

<p>Seems they put their data reading in a loop and continuously looped over it while data came in. You have to ask yourself one thing if you will take this approach, when will you stop collecting data and jump out of the loop? You can try and continue to read data, when you are already collecting, if nothing has come in for a few milliseconds, jump out and take that data and do what you want with it. </p>

<p>You can also try something like:</p>

<pre><code>While True:
    serial.flushInput()
    serial.write(command)
    incommingBYTES = serial.inWaiting()
    serial.read(incommingBYTES)
    #rest of the code down here
</code></pre>

python blocking pyserial