我有一个在Raspberry Pi 3上运行的客户端脚本 . 客户端从服务器接收它是TRUE还是FALSE . 在此之后,它接收一个类似字节的对象,并对其进行解码并使用此信息在GUI中显示图像 .
它会检查按下按钮时GPIO引脚是否为TRUE,然后循环回到第一个.recv() .
我希望客户端一直检查它是否收到消息“新游戏”,如果是,则转到第一个.recv(),检查它是否为TRUE或FALSE .
但是因为我的.recv()函数阻塞了,所以当我卡在那里时我什么也做不了 .
如何在检查GPIO状态是否变为TRUE时检查是否收到信息 .
代码如下所示:
#imports needed
import socket
import RPi.GPIO as GPIO
import time
import tkinter as tk
import pygame
#Setup of GPIO pin for buttons used as bumpers
GPIO.setmode(GPIO.BOARD)
GPIO.setup(36, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Pin 36 = GPIO16
#Setup of root window for the GUI and the different images
root = tk.Tk()
root.attributes('-fullscreen',True)
image6 = tk.PhotoImage(file="/home/pi/Desktop/wav/wrong.gif")
wronglabel = tk.Label(image=image6)
image5 = tk.PhotoImage(file="/home/pi/Desktop/wav/correct.gif")
correctlabel = tk.Label(image=image5)
image4 = tk.PhotoImage(file="/home/pi/Desktop/wav/questionmark.gif")
questionlabel = tk.Label(image=image4)
pygame.init()
#Setup of the different sounds to use in the project
correctsound = pygame.mixer.Sound('/home/pi/Desktop/wav/correct.wav')
wrongsound = pygame.mixer.Sound('/home/pi/Desktop/wav/wrong.wav')
HOST = '192.168.1.34' # The remote host,
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((HOST, PORT))
except:
print("FAILED. Sleep briefly & try again")
time.sleep(10)
continue
questionlabel.pack()
root.update()
while True:
data = s.recv(1024)
if data == b'True':
print('I am the true cone')
state = True
if data == b'False':
print('I am the false cone')
state = False
byte_string = s.recv(1024)
path = '/home/pi/Desktop/wav/%s.gif' % byte_string.decode()
questionlabel.pack_forget()
image1 = tk.PhotoImage(file=path) #x should change accordingly to the received message
Thelabel = tk.Label(image=image1)
Thelabel.pack()
root.update()
i=0
while i==0:
if GPIO.input(36) == True:
if state == True:
Thelabel.pack_forget()
correctlabel.pack()
root.update()
correctsound.play()
s.sendall(b'True Hit')
time.sleep(5)
correctlabel.pack_forget()
questionlabel.pack()
root.update()
if state == False:
Thelabel.pack_forget()
wronglabel.pack()
root.update()
wrongsound.play()
time.sleep(5)
wronglabel.pack_forget()
questionlabel.pack()
root.update()
i=1
1 回答
那么你很难理解你在这里尝试过的东西,你只有全局变量,命令式代码风格,而且一切都是交错的...我正在努力得到你需要的东西 .
我的代码不可能立即解决你的问题,但我希望它能帮助你理解在哪里挖掘来解决你的问题: