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 whether you accept or reject these cookies being set.

Receive UDP string
#1
Hello,

In our project there is UDP ekey system, it sends UDP strings to server/UDPreceiver when needed.
What I need for now is to have simple UDP port (8891) listener on LM to receive that string.
Maybe someone have some example or similar script?

Thanks !
Reply
#2
Resident script:
Code:
if not server then
  require('socket')
  server = socket.udp()
  server:setsockname('*', 8891)
  server:settimeout(1)
end

data = server:receive()
if data then
  loghex(data)
end
Reply
#3
(24.08.2017, 17:51)admin Wrote: Resident script:
Code:
if not server then
 require('socket')
 server = socket.udp()
 server:setsockname('*', 8891)
 server:settimeout(1)
end

data = server:receive()
if data then
 loghex(data)
end

Super, thanks admin.

but what I need to add to script, if an example I send ASCII 'group 1/1/1 on' (HEX 67 72 6f 75 70 20 31 2f 31 2f 31 20 6f 6e) to LM from other device and LM set group 1/1/1 to "TRUE" or to "1" or to something else regarding this group data type? And what sleep interval better to set for this resident script or it does not matter?? No so experienced with LUA, how convert "data" from this script and compare with my ASCII or HEX I send to LM to make changes in groups regarding received data??

By the way found free packet sender: https://packetsender.com/download
can be used to send as example data to LM using UDP for some testing. Works fine but I see some delays, but I send data to LM over WiFi...
Reply
#4
It works, but if I make a script like this there is 5-10 delay of sending KNX address (in if statement) after UDP string is sent by the client:
I use this code as resident with 0s interval.
Any thoughts of making this script work faster?
Code:
if not server then
 require('socket')
 server = socket.udp()
 server:setsockname('*', 8891)
 server:settimeout(1)
end

data = server:receive()

local TerminalID = string.byte(data, 9, 12)
local FingerID = string.byte(data, 33, 36)

if (FingerID == 6 and TerminalID == 63) then
 grp.write('4/0/10', 1)
elseif (FingerID == 7 and TerminalID == 63) then
 grp.write('0/5/18', 0)  
end

Nevermind! I solved it by putting logic in "if data then" statement
Reply
#5
@AlexLV, see this example, it will accept "on", "off" and any numeric value:
Code:
data = server:receive()
if data then
  chunks = data:split(' ')
  
  if #chunks == 3 and chunks[ 1 ] == 'group' then
    addr = chunks[ 2 ]
    value = chunks[ 3 ]

    if value == 'on' then
      value = true
    elseif value == 'off' then
      value = false
    else
      value = tonumber(value)
    end

    if value ~= nil then
      grp.write(addr, value)
    end
  end
end
Reply
#6
Thank you very mutch for helping! Will test today.
Reply
#7
Hi I'm after a similar function but require a TCP connection to ensure retransmission of any lost packets. Is there an example of a TCP server?

is there anything wrong with this from the lua socket example? can it be improved for the lm?

Code:
if not server then
-- load namespace
socket = require("socket")
-- create a TCP socket and bind it to the local host, at any port
server = assert(socket.bind("*", 7777))
-- find out which port the OS chose for us
ip, port = server:getsockname()

-- print a message informing what's up
log("Please telnet to localhost on port "..port)
log("After connecting, you have 10s to enter a line to be echoed")
-- loop forever waiting for clients
while 1 do
  -- wait for a connection from any client
  local client = server:accept()
  -- make sure we don't block waiting for this client's line
  client:settimeout(10)
  -- receive the line
data, err = client:receive()
    log(data)
  -- done with client, close the object
  client:close()
end
end
Reply
#8
Hi,

I created these in 2014, not sure if they still valid, but have no reason to assume they are not..

.txt   Tcp (Telnet) Sender.txt (Size: 700 bytes / Downloads: 9)
.txt   Tcp (Telnet) Receiver.txt (Size: 2.04 KB / Downloads: 10)
Reply
#9
See this example as well: https://openrb.com/example-lm2-as-tcp-se...-requests/
Reply
#10
Thanks,

I have dome some small tests with the following and seems to be working ok

Code:
if not server then
-- load namespace
socket = require("socket")
-- create a TCP socket and bind it to the local host, at any port
server = assert(socket.bind("*", 7777))
-- loop forever waiting for clients
while 1 do
  -- wait for a connection from any client
  local client = server:accept()
  -- make sure we don't block waiting for this client's line
  client:settimeout(10)
  -- receive the line
data, err = client:receive()
    --log(data)
    -- if there was no error, send it back to the client
    if not err then client:send('Received OK: '..data .. "\n") end
  -- done with client, close the object
  client:close()
end
end
Reply
#11
It does work but this way you can process only one client at a time. But it should not be a problem a client sends a small amount of data and disconnects after that. Otherwise you should use copas to handle multiple clients in parallel.
Reply


Forum Jump: