瀏覽代碼

add tcp2uart.lua example

funshine 9 年之前
父節點
當前提交
360fb59e63
共有 2 個文件被更改,包括 34 次插入0 次删除
  1. 18 0
      examples/fragment.lua
  2. 16 0
      lua_examples/tcp2uart.lua

+ 18 - 0
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)
+

+ 16 - 0
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)