如何在ESP8266 NodeMCU上读取stdio

我正在尝试从Lua控制台读取用户输入,但似乎无法从stdio读取

print("输入您的姓名:")
if file.open("stdio") then
  line = file.read("*a")
  file.close()
  print("你好 "..line)
end

我的固件具有模块 file,gpio,net,node,ow,tmr,uart,wifi,tls

如果我尝试使用 io.read("*a"),我会得到错误 Lua错误:stdin:1:尝试索引全局变量'io'(一个空值)

输入图像说明

原文链接 https://stackoverflow.com/questions/69822681

点赞
stackoverflow用户2858170
stackoverflow用户2858170

在NodeMCU固件中没有io库,因此无法调用io.read

如果要读取串行输入,可以使用uart库。 https://nodemcu.readthedocs.io/en/release/modules/uart/

-- 当接收到4个字符时。
uart.on("data", 4,
  function(data)
    print("receive from uart:", data)
    if data=="quit" then
      uart.on("data") -- 注销回调函数
    end
end, 0)
-- 当接收到'\r'时。
uart.on("data", "\r",
  function(data)
    print("receive from uart:", data)
    if data=="quit\r" then
      uart.on("data") -- 注销回调函数
    end
end, 0)
2021-11-03 11:31:10