Logic Machine Forum
copas connecting to tcp server cannot receive percentage - Printable Version

+- Logic Machine 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: copas connecting to tcp server cannot receive percentage (/showthread.php?tid=837)



copas connecting to tcp server cannot receive percentage - rocfusion - 14.06.2017

Hi,

Ok so I have a resident script using a copas udp server on the localhost to receive commands from knx objects (event script) , then there is a copas tcp client connecting to an tcp server on the local network. I did it this way so they don't block each other.  The issue I am experiencing is that when the tcp server sends out an percentage character the tcp client stops receiving anything else from the server.

Does someone have an idea?

Thanks,

Roger

Code:
if not ready then
 socket = require("socket")
 copas = require("copas")
 alert('not Ready')
 ready=true
 
 function parse(data)
   alert('parsing...  '..data)
 end
   
 function fromKNX(command)
  alert('from KNX.. '..command)
 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
 alert('not socket')
 if skt then
   skt:close()
   skt = nil
 end
 -- create tcp client
 skt, err = socket.connect('10.10.10.41', 4999)
 skt:settimeout(0)
 copas.addthread(function()
     while true do
      local resp,err = copas.receive(skt)
       if not resp then
         alert("TCP Receive error: ", err)
         break
       end
       parse(resp)
     end
   end)
 -- connect ok, reset buffer
 if skt then
   alert('[tcp-client] connection ok')
   warningfailed = true
   init()
   -- error while connecting,
 else
   if warningfailed then alert('[tcp-client] connection failed (conn): %s', err) end
   warningfailed = false
 end  
end

copas.loop()



RE: copas connecting to tcp server cannot receive percentage - admin - 14.06.2017

Default receive behaviour is to read a single so it will not return until a new line ("\n") character is found in the buffer. Are you sure the server is sending it correctly?


RE: copas connecting to tcp server cannot receive percentage - rocfusion - 14.06.2017

Hi Admin,

Yes, I am testing with the hercules tcp server. for example, i send

1234
12345%

and I don't see 12345% as a alert.

If I then restart the script and send %% then I receive a single % in the alert and then I can continue sending from the tcp server and the client resident script continues to alert the received data.

Thanks,

Roger

You can get the hercules app from http://www.hw-group.com/products/hercules/index_en.html


RE: copas connecting to tcp server cannot receive percentage - admin - 14.06.2017

Alert behaves like string.format and if you pass 12345% it will result in an error. This will be fixed in RC4. For now, just change alert to log.


RE: copas connecting to tcp server cannot receive percentage - rocfusion - 14.06.2017

Hi,

So I changed the copas.addthread function.


Code:
 copas.addthread(function()
     while true do
      local resp,err = copas.receive(skt,'*l')
       if not resp then
         alert("Jandy Receive error: ", err)
         copas.removeserver(skt)
         skt = nil          
         break
       end
       parse(resp)
     end
   end)




Now when I send % I see in the alerts
there was an error/usr/share/lua/genohm-scada.lua:0: bad argument #2 to 'format' (no value)

Any ideas?

Thanks,


Roger


RE: copas connecting to tcp server cannot receive percentage - admin - 14.06.2017

Code:
function parse(data)
  alert('parsing: %s', data)
end



RE: copas connecting to tcp server cannot receive percentage - rocfusion - 14.06.2017

Ok, wow... so everything was blocking because the alert statement couldn't concatenate the string?


Big thanks Admin


Thanks,


Roger