2017-12-13 2 views
0

Ich bin dieses Skript auf einem Himbeer für ein Spiel, das ich erstellt habe, wo ich drei verschiedene Eingaben in meinem wahr habe :.Python-Socket-Problem mit wahr

Einer von ihnen bekommt eine Socket-Nachricht von einer anderen Himbeere. Aber das Problem ist, dass der Rest von mir nicht mehr ausführt. Nur die erste IF-Anweisung, nachdem ich die Socket-Nachricht erhalten habe.

Wie man sie alle laufen lässt?

Vielen Dank im Voraus

#!/usr/bin/python 
import RPi.GPIO as GPIO 
import time 
import socket 
import pygame 
import serial 

GPIO.setmode(GPIO.BCM) 

UDP_IP = "192.168.0.21" 
UDP_PORT = 20 

sock = socket.socket(socket.AF_INET, # Internet 
        socket.SOCK_DGRAM) # UDP 
sock.bind((UDP_IP, 20)) 

# Klopspelout, pianospel, sleutelspel, totaalspel 

pinList = [27, 22, 4, 17] 
klopinput = 21 
sleutelinput = 11 

# loop through pins 

for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 

GPIO.setup(klopinput, GPIO.IN) 
GPIO.setup(sleutelinput, GPIO.IN, GPIO.PUD_UP) 
# time to sleep between operations in the main loop 
SleepTimeL = 2 

#variables 
totaal = 0 
klop = 0 
sleutel = 0 
piano = 0 
wacht = 0 
GPIO.output(pinList[0], GPIO.LOW) 
GPIO.output(pinList[1], GPIO.LOW) 
GPIO.output(pinList[2], GPIO.LOW) 
GPIO.output(pinList[3], GPIO.LOW) 
data = 0 

# main loop 
#GPIO.cleanup() 
while True: 


    #if GPIO.input(klopinput) == True: 
    # GPIO.output(pinList[0], GPIO.HIGH) 
    # totaal += 1 
    # print ('klopspel is goed') 
    # time.sleep(SleepTimeL) 
    # GPIO.output(pinList[0], GPIO.LOW) 


    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes 
    print "received message:", data 
    if data == "slotpiano2": 
    print "slot open" 
    totaal += 1 
    time.sleep(1) 

    if GPIO.input(klopinput) == True and wacht == 0 and klop == 0: 
    GPIO.output(pinList[1], GPIO.HIGH) 
    totaal += 1 
    klop = 1 
    print ('klop is goed') 
    time.sleep(SleepTimeL) 
    GPIO.output(pinList[1], GPIO.LOW) 
    time.sleep(SleepTimeL) 

    if GPIO.input(sleutelinput) == False and sleutel == 0: 
    GPIO.output(pinList[0], GPIO.HIGH) 
    totaal += 1 
    sleutel = 1 
    wacht = 1 
    print ('Sleutel is goed') 
    time.sleep(SleepTimeL) 
    GPIO.output(pinList[0], GPIO.LOW) 
    time.sleep(SleepTimeL) 
    wacht = 0 

    if totaal == 3: 
    GPIO.output(pinList[3], GPIO.HIGH) 
    totaal = 0 
    sleutel = 0 
    klop = 0 
    print ('reset') 
    time.sleep(SleepTimeL) 
    GPIO.output(pinList[3], GPIO.LOW) 
+0

Auf jeder Schleife wird sock.recvfrom() blockieren, bis die Daten der Steckdose kommt - das ist, was du beabsichtigst? Oder wollten Sie den Socket ignorieren, wenn keine Daten vorhanden sind, und weiter zur Überprüfung der GPIO-Eingänge gehen? –

+0

Oder wollten Sie den Socket ignorieren, wenn keine Daten vorhanden sind, und weiter zur Überprüfung der GPIO-Eingänge gehen? Ja, das letzte ist richtig. –

Antwort

0

OK nach Ihrem Kommentar, wenn Sie auf dem Sockel blockieren wollen vermeiden, dann müssen Sie es nicht blockierend setzen und die select() Funktion verwenden. Ich habe unten eine Kopie Ihres Codes gepostet, die die notwendige Bearbeitung zeigt, basierend auf this answer, und es gibt viele Informationen über select() in der Python-Dokumentation.

#!/usr/bin/python 
import RPi.GPIO as GPIO 
import time 
import socket 
import pygame 
import serial 
import select # select() function <--------- 

GPIO.setmode(GPIO.BCM) 

UDP_IP = "192.168.0.21" 
UDP_PORT = 20 

sock = socket.socket(socket.AF_INET, # Internet 
        socket.SOCK_DGRAM) # UDP 
sock.bind((UDP_IP, 20)) 
sock.setblocking(0) # set to non-blocking <----------- 

# Klopspelout, pianospel, sleutelspel, totaalspel 

pinList = [27, 22, 4, 17] 
klopinput = 21 
sleutelinput = 11 

# loop through pins 

for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 

GPIO.setup(klopinput, GPIO.IN) 
GPIO.setup(sleutelinput, GPIO.IN, GPIO.PUD_UP) 
# time to sleep between operations in the main loop 
SleepTimeL = 2 

#variables 
totaal = 0 
klop = 0 
sleutel = 0 
piano = 0 
wacht = 0 
GPIO.output(pinList[0], GPIO.LOW) 
GPIO.output(pinList[1], GPIO.LOW) 
GPIO.output(pinList[2], GPIO.LOW) 
GPIO.output(pinList[3], GPIO.LOW) 
data = 0 

# main loop 
#GPIO.cleanup() 
while True: 


    #if GPIO.input(klopinput) == True: 
    # GPIO.output(pinList[0], GPIO.HIGH) 
    # totaal += 1 
    # print ('klopspel is goed') 
    # time.sleep(SleepTimeL) 
    # GPIO.output(pinList[0], GPIO.LOW) 

    fds = select.select([sock], [], [], 1.0)  <----------- 
    if (fds[0]): # sock has some data 
     data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes 
     print "received message:", data 
     if data == "slotpiano2": 
     print "slot open" 
     totaal += 1 
     time.sleep(1) 

    if GPIO.input(klopinput) == True and wacht == 0 and klop == 0: 
    GPIO.output(pinList[1], GPIO.HIGH) 
    totaal += 1 
    klop = 1 
    print ('klop is goed') 
    time.sleep(SleepTimeL) 
    GPIO.output(pinList[1], GPIO.LOW) 
    time.sleep(SleepTimeL) 

    if GPIO.input(sleutelinput) == False and sleutel == 0: 
    GPIO.output(pinList[0], GPIO.HIGH) 
    totaal += 1 
    sleutel = 1 
    wacht = 1 
    print ('Sleutel is goed') 
    time.sleep(SleepTimeL) 
    GPIO.output(pinList[0], GPIO.LOW) 
    time.sleep(SleepTimeL) 
    wacht = 0 

    if totaal == 3: 
    GPIO.output(pinList[3], GPIO.HIGH) 
    totaal = 0 
    sleutel = 0 
    klop = 0 
    print ('reset') 
    time.sleep(SleepTimeL) 
    GPIO.output(pinList[3], GPIO.LOW) 

(. NB den Timeout auf etwas zu Ihrer Anwendung gesetzt, wenn 1,0 Sekunden nicht akzeptabel ist)