10.09.2019, 07:20
Hi,
Please help. I have the LM5 running RC1 firmware. Luasocket is upgraded to luasocket_2.0.2-31_imx6.ipk it was working in the 29 build. This particular LM5 is running resident scripts for a websocket client which is working. I really don't want to break that.
The UDP server does not receive when I have the TCP client active. I can comment out the TCP client then it works. The event script can send to the resident script fine. I have tried changing the timeout value for both TCP client and the UDP server. Makes no difference. I need to use copas because I have delay commands sent out to the TCP device.
Does anyone have an idea.
Resident script on 0 seconds
Event script
Please help. I have the LM5 running RC1 firmware. Luasocket is upgraded to luasocket_2.0.2-31_imx6.ipk it was working in the 29 build. This particular LM5 is running resident scripts for a websocket client which is working. I really don't want to break that.
The UDP server does not receive when I have the TCP client active. I can comment out the TCP client then it works. The event script can send to the resident script fine. I have tried changing the timeout value for both TCP client and the UDP server. Makes no difference. I need to use copas because I have delay commands sent out to the TCP device.
Does anyone have an idea.
Resident script on 0 seconds
Code:
if not ready then
copas = require("copas")
socket = require("socket")
server = socket.udp()
server:setsockname("127.0.0.1",51034)
function handler(uskt)
uskt = copas.wrap(uskt)
log("UDP connection handler")
while true do
local s, err
log("receiving...")
s, err = uskt:receive(1024)
if not s then
log("Receive error: ")
log(err)
return
end
log("Received data, bytes:")
log(s)
end
end
copas.addserver(server, handler, 0)
function parse(data)
log(data)
end
function sendtcp(command)
skt:send(command)
sleep(0.5)
end
function init()
log('in init')
sendtcp('#COSMSGS=false\r')
sendtcp('#MODEL?\r')
end
ready=true
end
if not skt then
--add receive thread
copas.addthread(function()
local reconnect = function()
skt, err = socket.connect('192.168.0.175', 4999)
skt:settimeout(1)
if(not err) then
init()
else
log('[tcp-client] error connecting ')
return
end
end
if not skt or skt==nil or skt.state=='CLOSED' then
reconnect()
end
if(skt)then
while true do
local resp,err = skt:receive('*r')
-- if theres no connection start a new connection
if(skt.state=='CLOSED')then
if not resp then
log("[tcp-client] Receive error: ")
copas.removethread(skt)
skt = nil
break
end
else
if(resp~=nil) then
local fd,prtd = pcall(parse,resp)
if(fd==false)then
log("Error with parsemsg")
end
end
end
end
end
end)
end
copas.loop()
Event script
Code:
if event.type=='groupwrite' then
local socket = require('socket')
local client = socket.udp()
local evt = event.dst..','..tostring(grp.getvalue(event.dst))
log(evt)
if client then
client:sendto(evt, '127.0.0.1', 51034)
client:close()
end
elseif event.type =='groupread' then
grp.response(event.dst,grp.getvalue(event.dst))
end