title: python send and receive data serially
body: <p>I'm trying communicate with STM32L152RB board through COM port 4  which accepts commands from COM4 and displays result in terminal using this code but it's not working ... I'm new to python please let me know what I'm doing wrong.</p>
  
  <pre><code>#Global Variables
  ser = 0
  
  
  def init_serial():
      COMNUM = 4          #Enter Your COM Port Number Here.
      global ser          #Must be declared in Each Function
      ser = serial.Serial()
      ser.baudrate = 9600
      ser.port = COMNUM - 1   #COM Port Name Start from 0
  
      #ser.port = '/dev/ttyUSB0' #If Using Linux
  
      #Specify the TimeOut in seconds, so that SerialPort
      #Doesn't hangs
      ser.timeout = 10
      ser.open()          #Opens SerialPort
  
      # print port open or closed
      if ser.isOpen():
          print 'dis' + ser.portstr
  
  
  
  
  init_serial()
  
  temp = raw_input('Type what you want to send, hit enter:\r\n')
  ser.write('dis')         #Writes to the SerialPort
  
  while 1:    
      bytes = ser.read()  #Read from Serial Port
      print 'You sent: ' + bytes      #Print What is Read from Port
  </code></pre>