title: Parsing serial communication in case statements between Arduino and Python after 1st char
body: <p>I'm having trouble in my case statement. A python script either serial.writes an "s" for sensor information or "r" for relay information. The sensor information part works perfectly. I'm having trouble getting the relay information right. The python script sends out an "r" followed by relay information which should be stored in array[x]. The arduino acknowledges the "r" in the case statement but doesnt process the proceeding packet which follows. All I get are empty arrays. I had a look here <a href="http://gammon.com.au/serial" rel="nofollow">Nick Gammons Serial page</a> but can't figure out how to get it to work in my situation, I just don't have the experience.</p>
  
  <p>Any help is much appreciated.</p>
  
  <p>ARDUINO CODE</p>
  
  <pre><code>void SerialCommunication()
  { 
    if (Serial.available()&gt;0)
    {
      char inChar = Serial.read(); 
      switch (inChar) 
      {
        case 'r':
          Sensors();
          break;
        case 'w':
          Relays();
          ProcessRelays();
          break;
        default:
          break;
      }
    } 
  }
  
  void Relays()
  {
    while (Serial.available() ==0);
    // Read all serial data available, as fast as possible
    while(Serial.available() &gt; 0)
    {
      char inChar = Serial.read();
      if(inChar == SOP)
      {
         index = 0;
         inData[index] = '\0';
         started = true;
         ended = false;
      }
      else if(inChar == EOP)
      {
         ended = true;
         break;
      }
      else
      {
        if(index &lt; 79)
        {
          inData[index] = inChar;
          index++;
          inData[index] = '\0';
        }
      }
    }
  
    // We are here either because all pending serial
    // data has been read OR because an end of
    // packet marker arrived. Which is it?
  
    if(started &amp;&amp; ended)
    {
      // The end of packet marker arrived. Process the packet
  
      if (strlen(inData) &gt; 0)
     {
  
        char *token = strtok(inData, ",");
        if(token)
        {
           index = 0;
  
           array[index] = atoi(token);
  
           while (token = strtok(NULL, ","))
           {
              array[index++] = atoi(token);
           }
        }
      }
  
      // Reset for the next packet
      started = false;
      ended = false;
      index = 0;
      inData[index] = '\0';
    }
  }
  void ProcessRelays()
  {
    Serial.println(array[0]);
    Serial.println(array[1]);
    Serial.println(array[2]);
  }
  </code></pre>
  
  <p>PYTHON CODE</p>
  
  <pre><code>#Import libraries
  import serial
  import string
  import MySQLdb
  import pprint
  from time import strftime
  
  #Connect to arduino
  ser = serial.Serial('/dev/ttyACM0', baudrate=9600, timeout = 2)
  ser.open()
  #Connects to database
  db = MySQLdb.connect(host="localhost", # your host, usually localhost
                       user="zzz", # your username
                       passwd="zzz", # your password
                       db="zzz") # name of the data base
  
  
  cur = db.cursor()
  with db:
          cur = db.cursor()
          cur.execute("SELECT * FROM relayschedule WHERE id=1")
          rows = cur.fetchall()
  
      for row in rows:
          s = str(row)
          ser.write("w" + s)
  ser.close()
  db.close()
  </code></pre>