【问题标题】:How can I read stdio on ESP8266 NodeMCU如何在 ESP8266 NodeMCU 上读取 stdio
【发布时间】:2021-12-17 16:54:41
【问题描述】:

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

print("Enter your name:")
if file.open("stdio") then
  line = file.read("*a")
  file.close()
  print("Hello "..line)
end

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

如果我尝试io.read("*a") 我得到错误 Lua error: stdin:1: attempt to index global 'io' (a nil value)

【问题讨论】:

    标签: lua esp8266 nodemcu


    【解决方案1】:

    NodeMCU 固件中没有 io 库。因此你不能打电话给io.read

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

    -- when 4 chars is received.
    uart.on("data", 4,
      function(data)
        print("receive from uart:", data)
        if data=="quit" then
          uart.on("data") -- unregister callback function
        end
    end, 0)
    -- when '\r' is received.
    uart.on("data", "\r",
      function(data)
        print("receive from uart:", data)
        if data=="quit\r" then
          uart.on("data") -- unregister callback function
        end
    end, 0)
    

    【讨论】:

      猜你喜欢
      • 2016-09-20
      • 2017-01-27
      • 1970-01-01
      • 2016-08-11
      • 2021-09-20
      • 2020-04-22
      • 1970-01-01
      • 2019-01-18
      • 2021-05-25
      相关资源
      最近更新 更多