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.

template for generic drivers
#1
Hi,

Maybe this helps someone.  Here is basic bi-directional example for connecting the LM to some device that is controlled over TCP.

Thanks,

Roger


Place a resident script with a sleep interval of 0

Code:
if not ready then
 socket = require("socket")
 copas = require("copas")
 alert('not Ready')
 ready=true
 
 function parse(data)
  alert('parsing: %s', data)
   -- use this function to parse the data you receive from the tcp device
 end

  function sendTo(command)
   skt:send(command)
   --optional delay between commands sent tcp device you are controlling
   sleep(1)
  end
   
 function fromKNX(command)
  alert('from knx: %s', command)
   local telegram = string.split(command,',')
   -- for every object that has the event script
   if(telegram[1]=='1/1/1')then      
      sendTo('thisComand ='..telegram[2]..'\r')
   end
 end

  -- use this function to get the status of the remote tcp device
  function init()
   alert('Getting status of tcp device')
   sendTo('some command \r')

  end
 
 local server = socket.udp()
 server:setsockname("127.0.0.1",23456)
 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, bytes: "..s)
     fromKNX(s)
   end
 end
 copas.addserver(server, handler, 1)  
end

if not skt then
  -- create tcp client
  skt, err = socket.connect('10.10.10.41', 4999)
  -- when theres no error connect ok, initialize
  if(not err) then
    skt:settimeout(0)
    -- add receive thread
    copas.addthread(function()
        while true do
          local resp,err = copas.receive(skt)
          -- if theres no connection start a new connection
          if not resp then
            alert("[tcp-client] Receive error: %s", err)
            copas.removeserver(skt)
            skt = nil
            break
          end
          parse(resp)
        end
      end)
    if skt then
      alert('[tcp-client] connection ok')
      -- optional init function use to get initial status of remote device
      -- init()
    -- error while connecting,
    else
      if warningfailed then alert('[tcp-client] connection failed (conn): %s', err) end
      return
    end
  else
    alert('[tcp-client] error connecting %s',err)
    return
  end
end

copas.loop()

Create event based script for
Code:
if event.type=='groupwrite' then
 require('socket')
 local client = socket.udp()
 local evt = event.dst..','..tostring(event.getvalue())
 if client then
   client:sendto(evt, '127.0.0.1', 23456)
   client:close()
 end
elseif event.type =='groupread' then
 grp.response(event.dst,event.getvalue())
end
Reply


Messages In This Thread
template for generic drivers - by rocfusion - 16.06.2017, 23:40
RE: template for generic drivers - by admin - 04.07.2017, 05:59
RE: template for generic drivers - by admin - 04.07.2017, 17:33

Forum Jump: