title: Python code to receive variable data continuously from serial port
body: <p>I have Python 2.7.4 and pyserial-2.5 win32 installed on my PC. Here I am using a microcontroller device as a master (primary) and my pc as a slave (secondary). Here every time microcontroller will transmit data, and my PC has to receive the data through serial port. I want a code in Python to receive continuous data. Here the transmitted data size will vary all the time. Here I wrote a code to transmit data, and the code is </p>
  
  <pre><code>import serial
  ser= serial.serial("COM10", 9600)
  ser.write("Hello world\n")
  x = ser.readline()
  print(x)     
  </code></pre>
  
  <p>With this code I can transmit data to the other PC, I crosschecked by opening HyperTerminal on the other PC and I can see the transmitted data (hello world).</p>
  
  <p>I also wrote the code to receive data:</p>
  
  <pre><code>import serial
  ser=serial.serial("COM10", 9600)  
  while 1:
  if ser.inwaiting():
  val = ser.readline(ser.inwaiting())
   print(val)  
  </code></pre>
  
  <p>if I send the data (how are you) from HyperTerminal, I can receive the data in my PC, with the above code.</p>
  
  <p>Until this every thing is fine.</p>
  
  <p>My question now is, when the microcontroller is transmitting variable data at variable time periods, I need to receive that data in my PC with Python. Do I need to use a buffer to store the received data? If yes, how will the code be? Why and how to use a buffer in Python? According to my search in internet, buffer is used to slice the string.</p>