关于UDP的一些基础知识
-
UDP通信支持一路默认,3路动态连接,共可连接4个。 -
UDP是一种面向无连接的即时通信方式,即时通信意味着通信完毕即是销毁。 -
模块发数据,要先设置一个默认通信的
IP和Port。 -
有
Server和Client之分 -
可实现同时多个
UDP通信
8266做UDP Server
init.lua
gpio.mode(4,gpio.OUTPUT)
gpio.mode(2,gpio.OUTPUT)
gpio.write(4,1)
if adc.force_init_mode(adc.INIT_ADC) then
node.restart()
return
end
tmr.alarm(0, 1000, 1, function()
gpio.write(4,1-gpio.read(4))
end)
tmr.alarm(1, 3000, 0, function()
dofile("wifi.lua")
end)
wifi.lua
wifi.setmode(wifi.STATIONAP)
cfg={}
cfg.ssid="Hellow8266"
cfg.pwd="11223344"
wifi.ap.config(cfg)
apcfg={}
apcfg.ssid="qqqqq"
apcfg.pwd="11223344"
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)
ConnectIP = "192.168.1.103"
ConnectPort = 8080
UdpSocket = net.createUDPSocket()
UdpSocket:listen(ConnectPort)
UdpSocketTable={}
UdpIPTable={}
UdpPortTable={}
UdpConnectCnt = 0
UdpCanConnect = 0
UdpSocket:on("receive", function(socket, data, port, ip)
UdpCanConnect = 1
for i=0,2 do
if UdpIPTable[i] == ip and UdpPortTable[i] == port then
UdpCanConnect = 0
end
end
if ip == ConnectIP and port == ConnectPort then
UdpCanConnect = 0
end
if UdpCanConnect == 1 then
UdpSocketTable[UdpConnectCnt] = socket
UdpIPTable[UdpConnectCnt] = ip
UdpPortTable[UdpConnectCnt] = port
print("\r\n"..UdpConnectCnt.."-Connect")
UdpConnectCnt = UdpConnectCnt + 1
end
if UdpConnectCnt == 3 then
UdpConnectCnt = 0
end
uart.write(0,data)
end)
uart.on("data",0,function(data)
if UdpSocket ~= nil then
UdpSocket:send(ConnectPort,ConnectIP,data)
end
for i=0,2 do
if UdpSocketTable[i] ~= nil then
UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data)
end
end
end, 0)
printip = 0
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
printip = 0
end)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
if printip == 0 then
print("+IP"..T.IP)
end
printip = 1
end)
UdpCanConnect – 等于1表示有新的连接加入!
ConnectIP 则是本机分配的IP地址,查看方法可移步<这里>
socket Tool中的 对方IP 指的是8266分配的IP地址!
print("\r\n"..UdpConnectCnt.."-Connect") - 没有回车换行会挤到一起
程序解读
UdpSocket:on("receive", function(socket, data, port, ip)中做了判断8266接收哪个UDPClient发来的信息,只要一个新的UDPClient(不和默认的,不和表中的一样)和模块建立连接,那就存储那个UDPClient的socket、ip和port到表中,然后打印data到串口。
如果经判断表中已经有个这个udp的信息,那个直接就打印data到串口。
uart.on是串口经过8266向已经连接的UDP Client广播data。




关于为什么会是1然后是许多个1,显然是空闲中断在使坏!
看下广播的结果~






加入空闲中断后的写法,可同时参考<这里>
init.lua
同上
wifi.lua
wifi.setmode(wifi.STATIONAP)
cfg={}
cfg.ssid="Hellow8266"
cfg.pwd="11223344"
wifi.ap.config(cfg)
apcfg={}
apcfg.ssid="qqqqq"
apcfg.pwd="11223344"
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)
ConnectIP = "192.168.1.103"
ConnectPort = 8080
UdpSocket = net.createUDPSocket()
UdpSocket:listen(ConnectPort)
UdpSocketTable={}
UdpIPTable={}
UdpPortTable={}
UdpConnectCnt = 0
UdpCanConnect = 0
UdpSocket:on("receive", function(socket, data, port, ip)
UdpCanConnect = 1
for i=0,2 do
if UdpIPTable[i] == ip and UdpPortTable[i] == port then
UdpCanConnect = 0
end
end
if ip == ConnectIP and port == ConnectPort then
UdpCanConnect = 0
end
if UdpCanConnect == 1 then
UdpSocketTable[UdpConnectCnt] = socket
UdpIPTable[UdpConnectCnt] = ip
UdpPortTable[UdpConnectCnt] = port
print("\r\n"..UdpConnectCnt.."-Connect")
UdpConnectCnt = UdpConnectCnt + 1
end
if UdpConnectCnt == 3 then
UdpConnectCnt = 0
end
uart.write(0,data)
end)
UartReadCnt=0
UartReadCntCopy=0
UartReadData=""
UartReadDataCopy=""
uart.on("data",0,function(data)
UartReadCnt = UartReadCnt + 1
UartReadData = UartReadData..data
end, 0)
tmr.alarm(2, 5, 1, function()
if UartReadCnt ~=0 then
if UartReadCnt == UartReadCntCopy then
UartReadCnt = 0
UartReadCntCopy = 0
UartReadDataCopy = UartReadData
UartReadData=""
NetSend(UartReadDataCopy)
else
UartReadCntCopy = UartReadCnt
end
end
end)
function NetSend(data)
if UdpSocket ~= nil then
UdpSocket:send(ConnectPort,ConnectIP,data)
end
for i=0,2 do
if UdpSocketTable[i] ~= nil then
UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data)
end
end
end
printip = 0
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
printip = 0
end)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
if printip == 0 then
print("+IP"..T.IP)
end
printip = 1
end)

1523

被折叠的 条评论
为什么被折叠?



