Reading serial and responding with Python
<p>Everyone, hello!</p>

<p>I'm currently using trying to communicate with my Arduino (whom is hooked up my Raspberry Pi through Serial) and using the information in my Python script on my Raspberry Pi.</p>

<p>That said, my Python script has to wait for the Arduino to report back it's data before I want the script to continue, although, I'm not entirely sure on how to do that.</p>

<p>This is what I've got so far:</p>

<pre><code>#!/usr/bin/env python
import time
import serial
import RPi.GPIO as GPIO

ser = serial.Serial('/dev/ttyACM0', 9600)

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(20, GPIO.OUT) #green LED
GPIO.setup(16, GPIO.IN, GPIO.PUD_UP) #green button

GPIO.output(20, True) #green ON

start = time.time()

while True:
        if (GPIO.input(16) == False):
                print "green button pressed"
                time.sleep(0.25)
                start = time.time()
                while (GPIO.input(16) == False):
                        time.sleep(0.01)
                if (GPIO.input(16) == True):
                        print "released!"
                        end = time.time()
                        elapsed = end - start
                        print elapsed
                        if elapsed &gt;= 5:
                                print "longer than 5s"
                        else:
                                print "shorter than 5s"
                                ser.write("0")
                                while True:
                                       print ser.readline().rstrip()
                                       if ser.readline().rstrip() == "a":
                                               print "ready"
                                               continue
                                       if ser.readline().rstrip() == "b":
                                               print "timeout"
                                               break
                                       if ser.readline().rstrip()[0] == "c":
                                               print "validated: " + ser.readline().rstrip()[2]
                                               break
</code></pre>

<p>As you can see, I'm sending the number 0 to my Arduino, and wait for it to respond with a, which means it's ready. After which, when it has the data, it sends out the message "c", as a result, I need to wait for 2 different seperate messages.</p>

<p>I've tried to do this by having a loop and breaking it when I have what I need, but this doesn't work.</p>

<p>It currently does go into the loop, and it prints out the "a" message, but doesn't come back with the second message.</p>

<p>Any idea how to properly tie this loop?</p>

<p>Thank you!</p>

<p>use functions</p>

<pre><code>def wait_for(ser,targetChar):
    resp = ""
    while True:
       tmp=ser.read(1)
       resp = resp + tmp
       if not tmp or tmp == targetChar: 
          return resp

first_resp = wait_for(ser,'a')
second_resp = wait_for(ser,'c')
while not second_resp.endswith('c'):
    print "RETRY"
    second_resp = wait_for(ser,'c')
</code></pre>

python pyserial