while tru的python套接字问题

2024-10-06 12:28:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我创建的一个游戏运行这个脚本,在那里我有三个不同的输入正确:。在

其中一个是从另一个树莓收到的套接字消息。但问题是我的while true的其余部分不再执行了。只有我收到套接字消息后的第一个IF语句。在

如何让他们都跑起来?在

提前谢谢

#!/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)

Tags: importoutputgpioiftimesleepsocketlow
1条回答
网友
1楼 · 发布于 2024-10-06 12:28:19

好的,根据您的注释,如果您想避免阻塞套接字,那么您需要将其设置为nonblocking并使用select()函数。我在下面发布了您的代码副本,它显示了基于this answer, 的必要编辑,Python文档中有很多关于select()的信息。在

#!/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)

(注意:如果1.0秒不可接受,请将超时设置为与应用程序相关的内容。)

相关问题 更多 >