From 360fb59e6320186be340f2cdd7595a6f6d2fea2d Mon Sep 17 00:00:00 2001 From: funshine Date: Fri, 23 Jan 2015 17:12:54 +0800 Subject: [PATCH] add tcp2uart.lua example --- examples/fragment.lua | 18 ++++++++++++++++++ lua_examples/tcp2uart.lua | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 lua_examples/tcp2uart.lua diff --git a/examples/fragment.lua b/examples/fragment.lua index d619b321..48ef11dd 100644 --- a/examples/fragment.lua +++ b/examples/fragment.lua @@ -319,3 +319,21 @@ m:connect("192.168.18.101",1883) m:subscribe("/topic",0,function(m) print("sub done") end) m:on("message",function(m,t,pl) print(t..":") if pl~=nil then print(pl) end end ) m:publish("/topic","hello",0,0) + +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) + diff --git a/lua_examples/tcp2uart.lua b/lua_examples/tcp2uart.lua new file mode 100644 index 00000000..638a2d77 --- /dev/null +++ b/lua_examples/tcp2uart.lua @@ -0,0 +1,16 @@ +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)