title: PySerial timeout and callback
body: <p>I have this short snippet of code here that works fine, but I have problem of getting rid of the hardcoded part.</p>
  
  <pre><code>ser = serial.Serial()
  ser.baudrate = 38400 
  ser.port = '/dev/ttyUSB0'
  ser.parity = serial.PARITY_EVEN
  ser.timeout = 1
  
  ser.open()
  ser.flushInput()
  ser.write(command) #command here is a simple request for data to my device 
  msg = ser.read(200)
  ser.close()
  </code></pre>
  
  <p>While this works fine, the problem I'm having is this. The length of the returned message can vary from 8 byte to almost 200 bytes depending on what was registered. By using a timeout, I prevent my read command from stalling if it doesn't receive 200 bytes. I also don't know ahead the length of the returned message I therefore can't change dynamically the ser.read. Also, there is no constant endline or constant character at the end of the transmission to lock on in a while loop. </p>
  
  <p>Is there a more stable/dynamic way to do this? I could run out of time if the request is too long or I could bust my read buffer without having the complete data transmission. On the other end, increasing the timer mean that my request rate will be slowed down (there is no problem in increasing the read buffer however).</p>