【问题标题】:LUA bad argument #2LUA 错误论点 #2
【发布时间】:2020-04-01 12:38:57
【问题描述】:

我是 LUA / ESP8266 的初学者,我正在尝试找出此错误的来源:

PANIC: unprotected error in call to Lua API (bad argument #2 to 'set' (index out of range))

这是串行监视器中的全部消息:

NodeMCU 2.2.0.0 built with Docker provided by frightanic.com
.branch: master
.commit: 11592951b90707cdcb6d751876170bf4da82850d
.SSL: false
.Build type: float
.LFS: disabled
.modules: adc,bit,dht,file,gpio,i2c,mqtt,net,node,ow,spi,tmr,uart,wifi
 build created on 2019-12-07 23:52
 powered by Lua 5.1.4 on SDK 2.2.1(6ab97e9)
> Config done, IP is 192.168.2.168
LED-Server started
PANIC: unprotected error in call to Lua API (bad argument #2 to 'set' (index out of range))

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x40100000, len 27780, room 16 
tail 4
chksum 0xbc
load 0x3ffe8000, len 2188, room 4 
tail 8
chksum 0xba
load 0x3ffe888c, len 136, room 0 
tail 8
chksum 0xf2
csum 0xf2
å¬ú‰.Éo‰ísÉÚo|Ï.å.õd$`..#íú..æÑ2rí.lúN‡.Éo„..l`.Ñ‚r€lÑ$.å...l`.Ñ‚s≤pɉ$.å....l`.Ñ‚r€l.èæ.å...$l`.{$é.êo.Ñü¬cc.ÑÑ".|l.Bè.c.‰è¬.lc‰ÚnÓ.2NN‚....å#€‚n.ÏéÑ.l..$Ïådè|Ïl.é.lÄ.o¸.Ñæ.#".llÏÑè..c...åû„åc.l.Ñb.{$r.

我将此代码 (https://github.com/Christoph-D/esp8266-wakelight) 上传到 ESP8266,并使用所有必需的模块构建了正确的 NodeMCU 固件。

串行输出正常几秒钟,然后我收到此错误并开始重复重新启动。

我从哪里开始寻找问题?

非常感谢!!!

编辑:在 lua 文件中只有几个地方写了关于“set”的任何内容:

local function update_buffer(buffer, c)
  if not c.r_frac then c.r_frac = 0 end
  if not c.g_frac then c.g_frac = 0 end
  if not c.b_frac then c.b_frac = 0 end
  local r2 = c.r_frac >= 0 and c.r + 1 or c.r - 1
  local g2 = c.g_frac >= 0 and c.g + 1 or c.g - 1
  local b2 = c.b_frac >= 0 and c.b + 1 or c.b - 1
  local r3, g3, b3
  local set = buffer.set
  for i = 1, NUM_LEDS do
    if i > c.r_frac then r3 = c.r else r3 = r2 end
    if i > c.g_frac then g3 = c.g else g3 = g2 end
    if i > c.b_frac then b3 = c.b else b3 = b2 end
    set(buffer, i - 1, g3, r3, b3)
  end
end

有什么问题吗?

【问题讨论】:

  • 我最好的猜测是NUM_LEDS 是错误的,所以在某些时候i - 1 大于缓冲区的长度并且它崩溃了。

标签: lua esp8266 nodemcu


【解决方案1】:

就在调用 set 的 for 循环上方,尝试添加:

print(buffer:size(), NUM_LEDS)

如果一切正常,它应该打印两次相同的数字。如果 NUM_LEDS 更大,那就是你的错误。

我真的不明白为什么它在那个地方使用全局变量;正是出于这个原因,使用buffer:size() 会更有意义。

【讨论】:

    猜你喜欢
    • 2017-10-05
    • 2020-12-29
    • 2019-01-09
    • 1970-01-01
    • 2021-07-22
    • 2016-11-14
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多