0

我正在研究 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()

睡眠功能不起作用,打印功能在终端窗口中显示“尚未加入...”的垃圾邮件。

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()

当我不将此代码包装到函数中时,一切正常。当我在硬编码循环之后编写函数时,一切正常。

4

1 回答 1

0

谢谢,对于nekomatics的评论,我解决了这个问题。我根本不知道 python 中的 global 关键字。为了保持与以前相同的逻辑,代码应如下所示。

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()
于 2022-01-22T19:41:05.513 回答