title: Python read from serial
body: <p>I have a Card read connected to a ttyUSB0 device, I need to make a python script that when ran will wait (for instance 30s) for a card to pass and after it passes and receives a line with data closes the script and prints the line with data only. If the card does not pass within 30s closes the script.</p>
  
  <p>Here is the script:</p>
  
  <pre><code>#!/usr/bin/python
  
  import time
  import serial
  
  ser = serial.Serial(
      port='/dev/ttyUSB0',
      baudrate = 4800,
      parity=serial.PARITY_NONE,
      stopbits=serial.STOPBITS_ONE,
      bytesize=serial.SEVENBITS,
      timeout=1
  )
  
  counter=0
  
  while 1:
      x=ser.readline()
      print x
  </code></pre>
  
  <p>With this what happens is that it keeps printing the line for ever until I hit Ctrl+C</p>
  
  <p><strong>EDIT:</strong> Found how to wait for the read I want, now, what would be the best way to make the whole script timeout after 30s?</p>
  
  <pre><code>#!/usr/bin/python
  
  import time
  import serial
  
  ser = serial.Serial(
          port='/dev/ttyUSB0',
          baudrate = 4800,
          parity=serial.PARITY_NONE,
          stopbits=serial.STOPBITS_ONE,
          bytesize=serial.SEVENBITS,
          timeout=None
  )
  
  
  #while 1:
  x=ser.read(size=16)
  print x
  </code></pre>