首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有重编程能力的ESP8266 Tcp-to-uart

具有重编程能力的ESP8266 Tcp-to-uart
EN

Stack Overflow用户
提问于 2017-04-15 22:01:17
回答 1查看 352关注 0票数 0

我使用了这个来自nodemcu repo的例子:

代码语言:javascript
复制
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)

但是,由于我使用的是串口模块,我不能再通过LuaLoader与我的芯片通信,也不能上传更新的init.lua文件。相反,我必须将芯片设置为闪存上传模式,然后闪存初始nodemcu固件,然后更新我的init.lua。步数太多了。

如何保持通过LuaLoader进行通信的能力?我尝试过这样的东西:

代码语言:javascript
复制
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,但是其他的情况,我不知道是什么。

EN

回答 1

Stack Overflow用户

发布于 2017-04-16 18:34:48

明白了!我不确定这是否是最好的选择,因为我是lua的新手,但它很有效。

代码语言:javascript
复制
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的过程。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43426967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档