title: python to arduino serial read & write
body: <p>I'm trying to "ping pong" info back and forth between some python code and arduino code.
  I want to send two setpoints to the arduino code periodically (for instance on the minute), read them on arduino &amp; update variables then send status info from arduino back to python periodically (such as on the :30 second).  Eventually python will be sending and pulling info from a mySQL db (later dev).</p>
  
  <p>Right now I can't get the info to bounce back and forth reliably.  I haven't found anything close to this in the searches and everything I've tried to modify isn't working.  Closest I have is this (and it doesn't actually switch back and forth between send and receive):</p>
  
  <p>Python</p>
  
  <pre><code>#!/usr/bin/python
  import serial
  import syslog
  import time
  
  #The following line is for serial over GPIO
  port = '/dev/ttyS0'
  
  
  ard = serial.Serial(port,9600,timeout=5)
  
  i = 0
  
  while (i &lt; 4):
      # Serial write section
  
      setTempCar1 = 63
      setTempCar2 = 37
      ard.flush()
      setTemp1 = str(setTempCar1)
      setTemp2 = str(setTempCar2)
      print ("Python value sent: ")
      print (setTemp1)
      ard.write(setTemp1)
      time.sleep(4)
  
      # Serial read section
      msg = ard.readline()
      print ("Message from arduino: ")
      print (msg)
      i = i + 1
  else:
      print "Exiting"
  exit()
  </code></pre>
  
  <p>Arduino:</p>
  
  <pre><code>// Serial test script
  
  int setPoint = 55;
  String readString;
  
  void setup()
  {
  
    Serial.begin(9600);  // initialize serial communications at 9600 bps
  
  }
  
  void loop()
  {
    while(!Serial.available()) {}
    // serial read section
    while (Serial.available())
    {
      if (Serial.available() &gt;0)
      {
        char c = Serial.read();  //gets one byte from serial buffer
        readString += c; //makes the string readString
      }
    }
  
    if (readString.length() &gt;0)
    {
      Serial.print("Arduino received: ");  
      Serial.println(readString); //see what was received
    }
  
    delay(500);
  
    // serial write section
  
    char ard_sends = '1';
    Serial.print("Arduino sends: ");
    Serial.println(ard_sends);
    Serial.print("\n");
    Serial.flush();
  }
  </code></pre>
  
  <p>All I end up getting is the same values repeated (not what was actually sent, not sure if its a string or byte issue) and nothing back to the python script.  Any help or ideas are greatly appreciated.  Thanks.</p>
  
  <p>EDIT: Modified code to what I'm currently running as suggested below.  Arduino is receiving fine and serial communication verified by minicom.  But python script still prints a blank line after "Message from arduino: ".</p>