title: Write and read from a serial port
body: <p>I am using the following python script to write AT+CSQ on serial port ttyUSB1.
  But I cannot read anything.</p>
  
  <p>However, when I fire AT+CSQ on minicom, I get the required results.</p>
  
  <p>What may be the issue with this script?</p>
  
  <p>Logs:
  Manual Script</p>
  
  <pre><code>root@imx6slzbha:~# python se.py
  Serial is open
  Serial is open in try block also
  write data: AT+CSQ
  read data:
  read data:
  read data:
  read data:
  </code></pre>
  
  <p>Logs:</p>
  
  <p>Minicom console</p>
  
  <pre><code>1. ate
  OK
  
  2. at+csq
  +CSQ: 20,99
  
  3. at+csq=?
  OKSQ: (0-31,99),(99)
  </code></pre>
  
  <p>How can I receive these results in the following python script?</p>
  
  <pre><code>import serial, time
  
  #initialization and open the port
  
  #possible timeout values:
  
  #    1. None: wait forever, block call
  
  #    2. 0: non-blocking mode, return immediately
  
  #    3. x, x is bigger than 0, float allowed, timeout block call
  
  ser = serial.Serial()
  
  ser.port = "/dev/ttyUSB1"
  
  ser.baudrate = 115200
  
  ser.bytesize = serial.EIGHTBITS #number of bits per bytes
  
  ser.parity = serial.PARITY_NONE #set parity check: no parity
  
  ser.stopbits = serial.STOPBITS_ONE #number of stop bits
  
  ser.timeout = None          #block read
  
  #ser.timeout = 0             #non-block read
  
  ser.timeout = 3              #timeout block read
  
  ser.xonxoff = False     #disable software flow control
  
  ser.rtscts = False     #disable hardware (RTS/CTS) flow control
  
  ser.dsrdtr = False       #disable hardware (DSR/DTR) flow control
  
  ser.writeTimeout = 2     #timeout for write
  
  try:
  
      ser.open()
  
      print("Serial is open")
  
  except Exception, e:
  
      print "error open serial port: " + str(e)
  
      exit()
  
  if ser.isOpen():
  
      try:
  
          print("Serial is open in try block also")
  
          ser.flushInput() #flush input buffer, discarding all its contents
  
          ser.flushOutput()#flush output buffer, aborting current output
  
                       #and discard all that is in buffer
  
          #write data
  
          ser.write("AT+CSQ")
          time.sleep(1)
  #       ser.write("AT+CSQ=?x0D")
  
          print("write data: AT+CSQ")
  #       print("write data: AT+CSQ=?x0D")
  
          time.sleep(2)  #give the serial port sometime to receive the data
  
          numOfLines = 1
  
          while True:
  
              response = ser.readline()
  
              print("read data: " + response)
  
              numOfLines = numOfLines + 1
  
              if (numOfLines &gt;= 5):
  
                  break
  
          ser.close()
  
      except Exception, e1:
  
          print "error communicating...: " + str(e1)
  
  else:
  
      print "cannot open serial port "
  </code></pre>