2017-04-15 3 views
0

再プログラミング能力を持つ:私は、UARTモジュールを使用していることから、ESP8266 TCP-に-UART Iはnodemcuレポから、この例で使用してい

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 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と入力すると、luaインタプリタモードになり、元に戻すにはtoggleFlash(false)と入力するだけです。毎回init.luaの更新が簡単になります。

関連する問題