Id非常感谢我遇到的问题的帮助。
我有一个与树莓派3B连接的温度传感器。要使用传感器,用户需要在命令提示符下输入I2C地址和轮询频率。我想自动化这些I2C地址和轮询时间的输入。这段代码是由传感器供应商提供的,而不是我写的(完全是python初学者!)
while True:
input = raw_input("Enter command: ")
if input.upper().startswith("LIST_ADDR"):
devices = device.list_i2c_devices()
for i in range(len (devices)):
print devices[i]
# address command lets you change which address the Raspberry Pi will poll
elif input.upper().startswith("ADDRESS"):
addr = int(string.split(input, ',')[1])
device.set_i2c_address(addr)
print("I2C address set to " + str(addr))
# continuous polling command automatically polls the board
elif input.upper().startswith("POLL"):
delaytime = float(string.split(input, ',')[1])
# check for polling time being too short, change it to the minimum timeout if too short
if delaytime < AtlasI2C.long_timeout:
print("Polling time is shorter than timeout, setting polling time to %0.2f" % AtlasI2C.long_timeout)
delaytime = AtlasI2C.long_timeout
# get the information of the board you're polling
info = string.split(device.query("I"), ",")[1]
print("Polling %s sensor every %0.2f seconds, press ctrl-c to stop polling" % (info, delaytime))
这是我要自动执行的命令提示符文本:
感谢您的帮助!
答案 0 :(得分:0)
我是Python的新手,但我立即发现了问题之一:
您的if
语句包含.upper()
而不是.isupper
,因此请对其进行更改。
在这里:
# These are the bits to change, I am sorry if there were any other errors I couldn't point out
if input.isupper().startswith("LIST_ADDR"):
elif input.isupper().startswith("ADDRESS"):
elif input.isupper().startswith("POLL"):