0

我不熟悉 Python,但我在 Google 上搜索时制作了以下脚本。

该脚本从设备中提取数据并将其注入 Domoticz。当我在我的 Windows 机器上运行脚本时,它每分钟都会正确循环并将数据注入 Domoitcz。当我在我的 Raspberry Pi 上运行相同的脚本时,它返回一个 SyntaxError: invalid syntax (line 155) on the except Exception 行...

即使我在该行之前放了一个 # 也会引发错误。

#!/usr/bin/env python3

from APSystemsECUR import APSystemsECUR
import time
import asyncio
import urllib.request
import urllib.parse
import urllib
from pprint import pprint


ecu_ip = "192.168.178.xx"
sleep = 60

url = 'http://192.168.178.xx:8080/json.htm?'
puntcomma = '\u003B'

loop = asyncio.get_event_loop()
ecu = APSystemsECUR(ecu_ip)

while True:
        try:
                data = loop.run_until_complete(ecu.async_query_ecu())
                #pprint(data)

                today_energy_kwh = str(data.get('today_energy')*1000)
                print('Today energy [kWh]: ' + today_energy_kwh)
                getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 212, 'svalue': (today_energy_kwh)}
                webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                #print(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                
                getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 213, 'svalue': data.get('current_power')}
                webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')     
                #print(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                
                getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 195, 'svalue': data.get('timestamp')}
                webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                #print(url + urllib.parse.urlencode(getVars))
#inverter values
                inverters = data.get('inverters')
#count number of inverters
                Inverter_qty = len(data.get('inverters'))
                print('Inverter_cnt: ' + str(Inverter_qty))
# loop trough all inverters and get the data
                for i in range(Inverter_qty):
                 Inverter = list(inverters.keys())[i]
                 print('InverterId: ' + Inverter)
                 InverterOnline = data['inverters'][Inverter]['online']
                 print('Online: ' + str(InverterOnline))
                 InverterTemperature = data['inverters'][Inverter]['temperature']
                 print('Temperature: ' + str(InverterTemperature))
                 nPower = len(data['inverters'][Inverter]['power'])
                 for x in range(nPower):
                  power = data['inverters'][Inverter]['power'][x]
                  print('Power inverter ' + str(i + 1) + ' panel ' + str(x + 1) + ': ' + str(power) + ' W')

#upload values to Domoticz voor inverter 1
                  if (i == 0) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 173, 'svalue': InverterTemperature}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                   if InverterOnline == True :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 174, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
                   else :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 174, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))

#upload values to Domoticz voor inverter 2
                  if (i == 1) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 179, 'svalue': InverterTemperature}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                   if InverterOnline == True :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 180, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
                   else :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 180, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))

#upload values to Domoticz voor inverter 3
                  if (i == 2) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 185, 'svalue': InverterTemperature}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                   if InverterOnline == True :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 186, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
                   else :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 186, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))

#upload values to Domoticz voor inverter 4
                  if (i == 3) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 191, 'svalue': InverterTemperature}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))
                   if InverterOnline == True :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 192, 'switchcmd': 'On', 'level': 0, 'passcode': '' }
                   else :
                    getVars = {'type' : 'command', 'param' : 'switchlight', 'idx': 192, 'switchcmd': 'Off', 'level': 0, 'passcode': '' }
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars))

#upload power values to Domoticz voor inverter 1
                  if (i == 0) and (x == 0) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 196, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 0) and (x == 1) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 197, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 0) and (x == 2) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 198, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 0) and (x == 3) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 199, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')

#upload power values to Domoticz voor inverter 2
                  if (i == 1) and (x == 0) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 200, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 1) and (x == 1) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 201, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 1) and (x == 2) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 202, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 1) and (x == 3) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 203, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')

#upload power values to Domoticz voor inverter 3
                  if (i == 2) and (x == 0) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 204, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 2) and (x == 1) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 205, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 2) and (x == 2) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 206, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 2) and (x == 3) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 207, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')

#upload power values to Domoticz voor inverter 4
                  if (i == 3) and (x == 0) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 208, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 3) and (x == 1) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 209, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 3) and (x == 2) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 210, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')
                  elif (i == 3) and (x == 3) :
                   getVars = {'type' : 'command', 'param' : 'udevice', 'nvalue' : 0, 'idx': 211, 'svalue': (power)}
                   webUrl = urllib.request.urlopen(url + urllib.parse.urlencode(getVars) + (puntcomma) + '0')


        except Exception as err:
                print(f"[ERROR]", {err})

        #print(f"Sleeping for {sleep} sec")
        time.sleep(sleep)

为什么它可以在我的 Windows 机器上正确运行,而不是在 Raspberry Pi 上?

4

1 回答 1

0

引发错误的行print是使用 f 字符串(例如f"{variable_name}")的语句。F-strings 是在 Python 3.6 中引入的,所以我猜你的树莓派使用的是早于那个版本的版本,而你的 windows 机器不是。

尝试检查您的树莓派上运行的 python 版本(python --version从终端)。如果它早于 3.6,则 f 字符串会导致代码中到处出现语法错误。

我能想到的唯一其他问题是您实际上可能正在使用默认python命令运行 python 2.7(如果 rasbian 像某些发行版那样执行此操作,我不记得了)。如果您通过上面的版本检查获得 2.7,请尝试使用python3并检查该版本。您需要以至少 3.6 的版本运行该脚本才能执行该脚本。

顺便说一句,您在第 156 行有另一个 f 字符串,这可以解释为什么如果您注释掉 155,它会引发完全相同的语法错误 :)

于 2021-04-27T04:54:15.517 回答