2017-04-15 3 views
0

재 프로그래밍 능력 : 내가 킷에는 UART 모듈을 사용하고 있기 때문에,ESP8266 Tcp를 - 투 - UART 내가 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을 업로드 할 수 없습니다. 대신 칩을 플래시 업로드 모드로 설정 한 다음 초기 노드 카드 펌웨어를 플래시 한 다음 업데이트 된 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이되어서는 안된다. 그러나 다른 조건은 무엇인지 모른다.

답변

0

알았습니다! 내가 루아를 처음 접했을 때 최선의 선택인지는 확실치 않지만 작동한다.

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을 입력하면 루아 인터프리터 모드가 시작되며 다시 전환하려면 toggleFlash(false)을 입력하면됩니다. 매번 init.lua의 업데이트가 간단 해집니다.