![]() |
|
Receive UDP string - Printable Version +- LogicMachine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Receive UDP string (/showthread.php?tid=960) |
Receive UDP string - leonidas - 24.08.2017 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 ! RE: Receive UDP string - admin - 24.08.2017 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)
endRE: Receive UDP string - AlexLV - 24.08.2017 (24.08.2017, 17:51)admin Wrote: Resident script: 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... RE: Receive UDP string - leonidas - 28.08.2017 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)
endNevermind! I solved it by putting logic in "if data then" statement RE: Receive UDP string - admin - 28.08.2017 @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
endRE: Receive UDP string - AlexLV - 04.09.2017 Thank you very mutch for helping! Will test today. RE: Receive UDP string - Diggerz - 15.02.2021 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
endRE: Receive UDP string - Erwin van der Zwart - 15.02.2021 Hi, I created these in 2014, not sure if they still valid, but have no reason to assume they are not..
Tcp (Telnet) Sender.txt (Size: 700 bytes / Downloads: 20)
Tcp (Telnet) Receiver.txt (Size: 2.04 KB / Downloads: 30)RE: Receive UDP string - admin - 15.02.2021 See this example as well: https://openrb.com/example-lm2-as-tcp-server-for-external-requests/ RE: Receive UDP string - Diggerz - 18.02.2021 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
endRE: Receive UDP string - admin - 18.02.2021 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. |