This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

UDP command usage problem
#1
Hi,

Need help. Using UDP script from forum with my changes. I need receive by LM some data over UDP, and write data to group depending received data.

As example - if LM received "206On" write to group 0/0/1 "1", if LM received "206Off" write to group 0/0/1 "0". UDP server working, received data I see in the alert tab, but groups not changed... If I need receive data in HEX type how check received data is HEX? Can you please help me and correct my script and very needed comments to study this case. 

if not ready then
  socket = require("socket")
  copas = require("copas")
  alert('not Ready')
  ready=true
 
 function parse(data)
  alert('parsing: %s', data)
end
    
  function fromKNX(command)
    alert('Command from SoLaVi.. '..command)
    --function parse(data)
    log(command)
    loghex(command)
    if command == '206On' then
      grp.write('0/0/1', 1)
       elseif data == '206Off' then
   grp.write('0/0/1', 0)
    else
      -- log other info, not mapped, if useful
      log(data)
      log(Command)
    end
  end
 
  local server = socket.udp()
  serverConfusedetsockname('*', 8891) -- my
  function handler(skts)
    skts = copas.wrap(skts)
    alert("UDP connection handler")
    while true do
      local s, err
      alert("UDP receiving...")
      s, erro = skts:receive(2048)
      if not s then
        alert("UDP Receive error: ", erro)
        break
      end
      alert("Received data: "..s)
      fromKNX(s)
    end
  end
  copas.addserver(server, handler, 1)
   loghex(data) --- log the data
  log(data)
end

if not skt then
  alert('not socket')
  if skt then
    skt:close()
    skt = nil
  end
end
copas.loop()

Thanks, Alex
Reply
#2
Copas is not needed for this. It's needed when you either have several UDP servers or a TCP server. This example is enough for your script, just make sure that resident script sleep time is set to 0: https://forum.logicmachine.net/showthrea...90#pid5590

You can check if data is hex like this:
Code:
function ishex(s)
  return type(s) == 'string' and #s % 2 == 0 and s:match('^%x+$')
end

Then you can use lmcore.hextostr to convert hex-data to a string.
Reply


Forum Jump: