如何在第一次运行[python]程序时执行部分代码

时间:2018-05-28 09:41:19

标签: python-2.7 raspberry-pi labview

labVIEW程序每次X5发送到rpi1然后rpi1发送ON1或OFF1发送到rpi2。 例如ON1从rpi1发送,然后如果每次接收到ON1时执行rpi2中的语句,现在我的问题是每次我需要此部分时执行GPIO仅在第一次接收到ON1时执行。

#rpi1   
 while True:
            ready_socks,_,_= select.select(socks, [],[])
            for server in ready_socks:
                cmnd, addr = server.recvfrom(1024)
                print(cmnd)
        #print(D)
        if 'X5' in str(cmnd):
                if(GPIO.input(23)==True):
                    bytes = "ON1"
                    sock.sendto(bytes,('192.168.1.22',5007))
                    data, address = so.recvfrom(1024)
                    print(data)

                if(GPIO.input(23)==False):
                    bytes = "OFF1"
                    sock.sendto(bytes,('192.168.1.22',5007))
                    data, address = so.recvfrom(1024)
                    print(data)

rpi 2的代码:

 #rpi 2
    while True:
        ready_socks,_,_= select.select(socks, [], [])
        for sock in ready_socks:
            data,addr = sock.recvfrom(1024)
            print "recived message:", data
            if 'ON1' in str(data):
               print"empty of water run the pump"
               bytes = "succeeded 1"
               sock1.sendto(bytes,('192.168.1.27',6007))
               GPIO.output(16,1)
                   time.sleep(0.1)
               GPIO.output(16,0)
               GPIO.output(18,1)

        elif 'OFF1' in str(data):
                   print "full of water"
                   bytes = "succeeded 2"
                   sock1.sendto(bytes,('192.168.1.27',6007))
                   GPIO.output(18,0)
                   time.sleep(0.1)
                   GPIO.output(18,1)
                   GPIO.output(16,0)

Code

0 个答案:

没有答案