我是 python 编码的新手,我正在使用 Raspberry Pi 作为学习手段。我正在尝试不断轮询我板上的输入并将它们打印到屏幕上,但是代码会打印一次输入状态并且似乎在等待某些东西,我似乎无法弄清楚......任何帮助都会很大赞赏。
# piFace_Control.py
import piface.pfio as pfio
import serial
import time
import struct
def h(val):
return struct.pack('!H', val)
def i(val):
return struct.pack('I', val)
serialA = serial.Serial("/dev/ttyUSB0",9600)
serialA.open()
serialA.flush()
pfio.init()
time.sleep(1)
val2 = 1
run = 1
while (run):
try:
val = serialA.readline()
print(val)
serialA.write(h(0xFE))
serialA.write(' Got it!')
if val2 == 0:
val2 = 1
else:
val2 = 0
print(i(val2))
serialA.write(i(val2))
pfio.digital_write(0,val2)
print "D0: "+str(pfio.digital_read(0))
print "D1: "+str(pfio.digital_read(1))
print "D2: "+str(pfio.digital_read(2))
print "D3: "+str(pfio.digital_read(3))
print "D4: "+str(pfio.digital_read(4))
print "D5: "+str(pfio.digital_read(5))
print "D6: "+str(pfio.digital_read(6))
print "D7: "+str(pfio.digital_read(7))
except (KeyboardInterrupt, SystemExit):
print '\nKeyboard Interrupt Caught...'
run = 0
pass
serialA.close()