As the title says... Chat on...

User avatar
By vxdandy
#23910 Hello,

I was interested in creating a simplified ESP8255 nodemcu UART<>TCP transparent bridge with the minimum of LUA code. However, I found an issue when transferring bytes between the TCP server connection and the UART. It seems to corrupt data transfer whenever a 0xFF byte is encountered. My code is shown below

Code: Select all-- init.lua
abort = false

wifi.setmode(wifi.STATION)

--modify according your wireless router settings
wifi.sta.config("ssid","password")
wifi.sta.connect()

function startup()
  tmr.stop(2)
  if abort == true then
    print('startup aborted')
    return
  end
  uart.setup(0, 9600, 8, 0, 1, 1)
  dofile("bridge.lua")
end

abort = false

tmr.alarm(1, 1000, 1, function()
    if wifi.sta.getip()== nil then
        print("IP unavailable, Waiting...")
    else
        tmr.stop(1)
        print("Config done, IP is "..wifi.sta.getip())       
        tmr.alarm(2, 5000, 0, startup)
    end
end)


Code: Select all-- Bridge.lua
-- Based loosely on code by Thorsten von Eicken, esp8266-lua-bridge , (C) 2015

uartConn = nil -- current connection that uart input goes to

ser2net = net.createServer(net.TCP, 28800)
ser2net:listen(23, function(conn)
    if uartConn then
      uartConn:close()
    else
      uartConn = conn
    end

  conn:on("sent",function(conn)
    collectgarbage()
  end)
   
  -- Disconnection
  conn:on("disconnection", function(conn)
    uartConn:close()
    collectgarbage()
  end)

  -- Reconnection
  conn:on("reconnection", function(conn)
    if uartConn then
      uartConn:close()
    else
      uartConn = conn
    end
  end)

  -- UART receive, TCP send
  uart.on("data", 0, function(data)
    conn:send(data)
  end, 0)

  -- TCP receive, UART send
  conn:on("receive", function(conn, data)
    uart.write(0, data)
  end)

end)


Sending bytes from TCP Server to UART (shown in hex)
TCP byte send: 11 22 33 44 55 FF 66 77
UART byte receive: 11 22 33 44 55 FF FF 66 77 <-- two FF received, only one expected

This test was repeated with different serial terminal programs to exclude data corruption by an external source. Also, this issue is transfer rate independent, as my testing was done by manually sending bytes (hence no buffering shown).

Can anyone see if there is an issue with my code that would directly cause this problem?

My environment:
    nodemcu_integer_0.9.6-dev_20150406
    Esplorer V0.2.0-rc2
    ESP8266 ESP-12 module
    TTL to UART interface
    3.3V power supply
    Windows Serial terminal programs: Compuphase Termite, HW Hercules, Bray terminal, and bespoke MFC C++ application

Regards,
Andrew.