title: Reliably write and read to serial using python
body: <p>I am communicating with a Fona 808 module from a Raspberry Pi and I can issue AT commands, yey!</p>
  
  <p>Now I want to make a python program where I can reliably issue AT commands using shortcut commands like "b" for getting the battery level and so on.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>import serial
  con = serial.Serial('/dev/ttyAMA0',timeout=0.2,baudraute=115200)
  def sendAtCommand(command):
      if command == 'b':
          con.write("at+cbc\n".encode())
          reply = ''
          while con.inWaiting():
              reply = reply + con.read(1)
          return reply
  
  while True:
      x = raw_input("At command: ")
      if x.strip() == 'q':
          break
      reply = sendAtCommand(x)
      print(reply)
  
  
  con.close()
  </code></pre>
  
  <p>In the sendAtCommand I will have a bunch of if statements that send different at commands depending on the input it receives. </p>
  
  <p>This is somewhat working but is very unreliable. Sometimes I get the full message. Other times I get nothing. Then double message next time and so on.</p>
  
  <p>I would like to create one method that issues a command to the Fona module and then reads the full response and returns it.</p>
  
  <p>Any suggestions?</p>