micropython:从函数中的文件加载数据导致无限循环
micropython: loading data from a file in function causing an endless loop
我正在使用 pycom 的 lopy4,在从 txt 文件加载配置数据时遇到了 od 问题:
def loadFromConfigFile():
f= open('config.txt')
for line in f:
if "uuidExpected" in line:
uuidExpected=line[13:len(line)-1].strip()
elif "app_eui" in line:
app_eui = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "app_key" in line:
app_key = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "syncClockTime" in line:
syncClockTime=float(line[14:len(line)-1].strip())
elif "loraJoinTime" in line:
loraJoinTime=float(line[13:len(line)-1].strip())
elif "bleScanInterval" in line:
bleScanInterval=int(line[16:len(line)-1].strip())
elif "mainLoopWaitTime" in line:
mainLoopWaitTime=int(line[17:len(line)-1].strip())
elif "hbInterval" in line:
hbInterval=int(line[11:len(line)-1].strip())
f.close()
loadFromConfigFile()
当我使用这个函数时,我的程序卡在了这里:
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
while not lora.has_joined():
time.sleep(loraJoinTime)
print('Not yet joined...')
print("joined!")
setClock()
睡眠功能不起作用,终端中的打印功能垃圾邮件“尚未加入...”window。
f= open('config.txt')
for line in f:
if "uuidExpected" in line:
uuidExpected=line[13:len(line)-1].strip()
elif "app_eui" in line:
app_eui = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "app_key" in line:
app_key = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "syncClockTime" in line:
syncClockTime=float(line[14:len(line)-1].strip())
elif "loraJoinTime" in line:
loraJoinTime=float(line[13:len(line)-1].strip())
elif "bleScanInterval" in line:
bleScanInterval=int(line[16:len(line)-1].strip())
elif "mainLoopWaitTime" in line:
mainLoopWaitTime=int(line[17:len(line)-1].strip())
elif "hbInterval" in line:
hbInterval=int(line[11:len(line)-1].strip())
f.close()
当我不将此代码包装到函数中时,一切正常。当我在硬编码循环之后编写函数时,一切正常。
感谢 nekomatics 评论我解决了这个问题。我根本不知道 python 中的全局关键字。为了保持与以前相同的逻辑,代码应如下所示。
def loadFromConfigFile():
global uuidExpected
global app_eui
global syncClockTime
global loraJoinTime
global bleScanInterval
global mainLoopWaitTime
global hbInterval
f= open('config.txt')
for line in f:
if "uuidExpected" in line:
uuidExpected=line[13:len(line)-1].strip()
elif "app_eui" in line:
app_eui = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "app_key" in line:
app_key = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "syncClockTime" in line:
syncClockTime=float(line[14:len(line)-1].strip())
elif "loraJoinTime" in line:
loraJoinTime=float(line[13:len(line)-1].strip())
elif "bleScanInterval" in line:
bleScanInterval=int(line[16:len(line)-1].strip())
elif "mainLoopWaitTime" in line:
mainLoopWaitTime=int(line[17:len(line)-1].strip())
elif "hbInterval" in line:
hbInterval=int(line[11:len(line)-1].strip())
f.close()
我正在使用 pycom 的 lopy4,在从 txt 文件加载配置数据时遇到了 od 问题:
def loadFromConfigFile():
f= open('config.txt')
for line in f:
if "uuidExpected" in line:
uuidExpected=line[13:len(line)-1].strip()
elif "app_eui" in line:
app_eui = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "app_key" in line:
app_key = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "syncClockTime" in line:
syncClockTime=float(line[14:len(line)-1].strip())
elif "loraJoinTime" in line:
loraJoinTime=float(line[13:len(line)-1].strip())
elif "bleScanInterval" in line:
bleScanInterval=int(line[16:len(line)-1].strip())
elif "mainLoopWaitTime" in line:
mainLoopWaitTime=int(line[17:len(line)-1].strip())
elif "hbInterval" in line:
hbInterval=int(line[11:len(line)-1].strip())
f.close()
loadFromConfigFile()
当我使用这个函数时,我的程序卡在了这里:
lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
while not lora.has_joined():
time.sleep(loraJoinTime)
print('Not yet joined...')
print("joined!")
setClock()
睡眠功能不起作用,终端中的打印功能垃圾邮件“尚未加入...”window。
f= open('config.txt')
for line in f:
if "uuidExpected" in line:
uuidExpected=line[13:len(line)-1].strip()
elif "app_eui" in line:
app_eui = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "app_key" in line:
app_key = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "syncClockTime" in line:
syncClockTime=float(line[14:len(line)-1].strip())
elif "loraJoinTime" in line:
loraJoinTime=float(line[13:len(line)-1].strip())
elif "bleScanInterval" in line:
bleScanInterval=int(line[16:len(line)-1].strip())
elif "mainLoopWaitTime" in line:
mainLoopWaitTime=int(line[17:len(line)-1].strip())
elif "hbInterval" in line:
hbInterval=int(line[11:len(line)-1].strip())
f.close()
当我不将此代码包装到函数中时,一切正常。当我在硬编码循环之后编写函数时,一切正常。
感谢 nekomatics 评论我解决了这个问题。我根本不知道 python 中的全局关键字。为了保持与以前相同的逻辑,代码应如下所示。
def loadFromConfigFile():
global uuidExpected
global app_eui
global syncClockTime
global loraJoinTime
global bleScanInterval
global mainLoopWaitTime
global hbInterval
f= open('config.txt')
for line in f:
if "uuidExpected" in line:
uuidExpected=line[13:len(line)-1].strip()
elif "app_eui" in line:
app_eui = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "app_key" in line:
app_key = ubinascii.unhexlify((line[8:len(line)-1]).strip())
elif "syncClockTime" in line:
syncClockTime=float(line[14:len(line)-1].strip())
elif "loraJoinTime" in line:
loraJoinTime=float(line[13:len(line)-1].strip())
elif "bleScanInterval" in line:
bleScanInterval=int(line[16:len(line)-1].strip())
elif "mainLoopWaitTime" in line:
mainLoopWaitTime=int(line[17:len(line)-1].strip())
elif "hbInterval" in line:
hbInterval=int(line[11:len(line)-1].strip())
f.close()