title: PySerial and Value Splitting Error
body: <p>I have encountered an error that has been seriously bothering me, and I have been unable to find a workaround. I have a python program that utilizes PySerial to import serial port values from a pulse oximeter. The problem is, when the function ser.readline() is invoked (in other words, when the python program is told to read values from the Arduino's serial monitor, the Arduino's serial values become distorted and the program returns a value unpack error. </p>
  
  <p>Here is the python program. </p>
  
  <hr>
  
  <pre><code>import serial
  import time 
  import pylab
  import matplotlib.pyplot as plt
  import numpy as np
  import os
  import csv
  
  #time load
  timestr = time.strftime("%Y_%m_%d")
  
  #establish serial connection with ACM0
  ser = serial.Serial('/dev/ttyACM0', 115200)
  
  #establish variables
  thymeL = [ ]
  bpmL = [ ]
  sp02L = [ ]
  array_data = thymeL, bpmL, sp02L
  
  #declare time
  thyme = 1
  
  #graph attributes
  plt.ion()
  plt.title("Pulse [BPM] &amp; SPo2 [%] v. Time [s]", fontsize = "16")
  plt.xlabel("Time [s]", fontsize = "14")
  plt.ylabel("Pulse (red) [BPM] &amp; SPo2 (blue) [%]", fontsize = "14")
  
  while True:         
      data_in = ser.readline()
      print data_in
      data_in = data_in.strip('\n')
      bpm,sp02 = data_in.split(",") 
  
  #convert string vals to float
      thyme = float(thyme)
      bpm = float(bpm)
      sp02 = float(sp02)
  
  #print to terminal
      print "Time [s]: %s" % (thyme)
      print "HR [BPM]: %s" % (bpm)
      print "SPO2 [%%]: %s" % (sp02)  
      print 
  
  #append vectors
      thymeL.append(thyme)
      bpmL.append(bpm)
      sp02L.append(sp02)
  
  #print values to plot
      plt.scatter(thyme,bpm,color="red")
      plt.scatter(thyme,sp02,color="blue")
      plt.pause(0.1)
      time.sleep(0.05)
  
  #update time
      thyme = thyme + 0.5
  
  #write to .csv
      with open(full_path, 'w') as f:
      writer = csv.writer(f)
      for t, b, s in zip(array_data[0], array_data[1], array_data[2]):
          writer.writerow([t, b, s])
  </code></pre>
  
  <hr>
  
  <p>The most important snippet of all is:</p>
  
  <hr>
  
  <pre><code>while True:         
      data_in = ser.readline()
      print data_in
      data_in = data_in.strip('\n')
      bpm,sp02 = data_in.split(",") 
  </code></pre>
  
  <hr>
  
  <p>The Arduino program is as follows: </p>
  
  <pre><code>#include &lt;PinChangeInt.h&gt;
  #include &lt;eHealth.h&gt;
  
  int cont = 0;
  
  void setup() {
    Serial.begin(115200);  
    eHealth.initPulsioximeter();
  
    PCintPort::attachInterrupt(6, readPulsioximeter, RISING);
  }
  
  void loop() {
  
    char buffer[32]; // make sure buffer is large enough
    sprintf(buffer,"%d,%d \n",eHealth.getBPM(),eHealth.getOxygenSaturation());
    Serial.print(buffer);
    delay(500);
  
  }
  
  //=========================================================================
  void readPulsioximeter(){  
  
    cont ++;
  
    if (cont == 50) { //Get only of one 50 measures to reduce the latency
      eHealth.readPulsioximeter();  
      cont = 0;
    }
  }
  </code></pre>
  
  <p>So, the serial monitor is outputting values like this: </p>
  
  <pre><code>67,95
  66,95
  67,96
  </code></pre>
  
  <p>and so on. </p>
  
  <p>But <strong>only</strong> when ser.readline() is invoked, the values become skewed and are unable to be unpacked by the split(',') function. In the following photo <a href="http://i.imgur.com/WAlAfjT.jpg" rel="nofollow">(1)</a> and <a href="http://i.imgur.com/Vy71GtH.jpg" rel="nofollow">(2)</a>, you can see the distortion of the values right when the ser.readline() is called.</p>
  
  <p>How can I re-word the python OR Arduino program in such a way to circumvent this distortion and allow the values to be split and unpacked without any errors?</p>