Logic Machine Forum
UDP command usage problem - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: General (https://forum.logicmachine.net/forumdisplay.php?fid=2)
+--- Thread: UDP command usage problem (/showthread.php?tid=975)



UDP command usage problem - AlexLV - 03.09.2017

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


RE: UDP command usage problem - admin - 04.09.2017

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/showthread.php?tid=960&pid=5590#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.