Python Kodundaki Dışarıdan Gelen Veriyi Tutan Değişken Hangisi?

Katılım
18 Haziran 2019
Mesajlar
1.986
Makaleler
1
Çözümler
9
Arkadaşlar selamlar. Python bilgim üst düzey olmadığı öğrenmek ve uygulamak için internetten buldum. Bu kod, IR alıcıdan gelen kodu ekrana yazdırıyor. Kod sıkıntısız çalışıyor ama benim kumandadan gelen veriye ihtiyacım var. Ne denediysem bir türlü gelen veriyi tutan değişkeni bulamadım. Yardımlarınızı bekliyorum. Kod;
[CODE lang="python" title="Python"]import RPi.GPIO as GPIO
from time import time

def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


def binary_aquire(pin, duration):
# aquires data as quickly as possible
t0 = time()
results = []
while (time() - t0) < duration:
results.append(GPIO.input(pin))
return results


def on_ir_receive(pinNo, bouncetime=150):
# when edge detect is called (which requires less CPU than constant
# data acquisition), we acquire data as quickly as possible
data = binary_aquire(pinNo, bouncetime/1000.0)
if len(data) < bouncetime:
return
rate = len(data) / (bouncetime / 1000.0)
pulses = []
i_break = 0
# detect run lengths using the acquisition rate to turn the times in to microseconds
for i in range(1, len(data)):
if (data != data[i-1]) or (i == len(data)-1):
pulses.append((data[i-1], int((i-i_break)/rate*1e6)))
i_break = i
# decode ( < 1 ms "1" pulse is a 1, > 1 ms "1" pulse is a 1, longer than 2 ms pulse is something else)
# does not decode channel, which may be a piece of the information after the long 1 pulse in the middle
outbin = ""
for val, us in pulses:
if val != 1:
continue
if outbin and us > 2000:
break
elif us < 1000:
outbin += "0"
elif 1000 < us < 2000:
outbin += "1"
try:
return int(outbin, 2)
except ValueError:
# probably an empty code
return None


def destroy():
GPIO.cleanup()


if __name__ == "__main__":
setup()
try:
print("Starting IR Listener")
while True:
print("Waiting for signal")
GPIO.wait_for_edge(11, GPIO.FALLING)
code = on_ir_receive(11)
if code:
print(str(hex(code)))
else:
print("Invalid code")
except KeyboardInterrupt:
pass
except RuntimeError:
# this gets thrown when control C gets pressed
# because wait_for_edge doesn't properly pass this on
pass
print("Quitting")
destroy()



[/CODE]
Kod bu şekilde. Ayrıca bu kodu Raspberry üzerinde GPIO kontrolü için çalıştırıyorum. Şuanda yapmak istediğim, sonuna bir "if" bloğu ekleyip, "gelen_veri_degiskeni=="0xffa25d" kodunu yazmak ve altını doldurmak istiyorum. Biraz uzun ve karışık olduğu için özür dilerim. yardımlarınızı bekliyorum. @SideWinder
 
Son düzenleyen: Moderatör:
Uyarı! Bu konu 5 yıl önce açıldı.
Muhtemelen daha fazla tartışma gerekli değildir ki bu durumda yeni bir konu başlatmayı öneririz. Eğer yine de cevabınızın gerekli olduğunu düşünüyorsanız buna rağmen cevap verebilirsiniz.

Technopat Haberler

Yeni konular

Geri
Yukarı