我使用了这个来自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)但是,由于我使用的是串口模块,我不能再通过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 mode和normal mode,但在LuaLoader中不起作用。我认为问题出在uart的设置上,也许不应该是\n,但是其他的情况,我不知道是什么。
发布于 2017-04-16 18:34:48
明白了!我不确定这是否是最好的选择,因为我是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的过程。
https://stackoverflow.com/questions/43426967
复制相似问题