【问题标题】:ESP8266 Tcp-to-uart with reprogramming abilityESP8266 Tcp-to-uart 具有重新编程能力
【发布时间】:2017-04-16 18:34:47
【问题描述】:

我正在使用来自 nodemcu repo 的这个示例:

uart.setup(0,9600,8,0,1,0)
sv=net.createServer(net.TCP, 60)
global_c = nil
sv:listen(9999, function(c)
    if global_c~=nil then
        global_c:close()
    end
    global_c=c
    c:on("receive",function(sck,pl) uart.write(0,pl) end)
end)

uart.on("data",4, function(data)
    if global_c~=nil then
        global_c:send(data)
    end
end, 0)

但是,由于我使用的是 uart 模块,我无法再通过 LuaLoader 与我的芯片通信,也无法上传更新的init.lua 文件。相反,我必须将芯片置于闪存上传模式,然后刷新初始 nodemcu 固件,然后更新我的 init.lua。步骤太多。

如何保留通过 LuaLoader 进行通信的能力?我试过这样的事情:

uart.on('data', '\n', handleUartResponse, 0)
...
...
function handleUartResponse(response)
    if response == 'flash\n' then
        g_flash = true
        toggleOutput(true)
        uart.write(0, 'flash mode')

    elseif response == 'endflash\n' then    
        g_flash = false 
        uart.write(0, 'normal mode')
        toggleOutput(false)

    elseif g_flash then
        node.input(response)

    else
        if g_conn ~= nil then
            g_conn:send(response, function(sock)
                closeConnection(sock)
                g_conn = nil
            end)
        end
    end
end

function toggleOutput(turnOn) 
    if turnOn then
        node.output(nil, 1)
    else
        node.output(silent, 0)
    end
end

它在另一个串行终端中打印flash modenormal mode,但它在 LuaLoader 中不起作用。我认为问题在于 uart 设置,可能不应该是\n,但其他情况,我不知道是什么。

【问题讨论】:

    标签: esp8266 nodemcu


    【解决方案1】:

    知道了!不确定这是否是最佳选择,因为我是 lua 新手,但它确实有效。

    function handleNormalMode(response)
        if response == 'flash\r' then -- magic code to enter interpreter mode
            toggleFlash(true)        
        else -- tcp-to-uart
            if g_conn ~= nil then
                g_conn:send(response, function(sock)
                    closeConnection(sock)
                    g_conn = nil
                end)
            end
        end
    end
    
    function ignore(x)
    end
    
    function uartSetup(echo)
        uart.setup(0, 115200, 8, 0, 1, echo)
    end
    
    function toggleFlash(turnOn) 
        if turnOn then
            uart.on('data')  -- unregister old callback
            uartSetup(1) -- re-configure uart
            uart.on('data', 0, ignore, 1) -- this allows lua interpreter to work
            node.output(nil) -- turn on lua output to uart
            uart.write(0, 'flash mode') -- notify user
        else
            node.output(ignore, 0) -- turn off lua output to uart
            uart.on('data') -- unregister old callback
            uartSetup(0) -- re-configure uart
            uart.on('data', '\r', handleNormalMode, 0) -- turn on tcp-to-uart
            uart.write(0, 'normal mode') -- notify user
        end
    end
    

    我在脚本开头调用toggleFlash(false)。然后,输入 flash\r 进入 lua 解释器模式并切换回来,我只需输入 toggleFlash(false) 就可以了!这简化了每次更新 init.lua 的过程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 2015-10-01
      相关资源
      最近更新 更多