Logic Machine Forum
Read with Wait function - 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: Read with Wait function (/showthread.php?tid=2139)



Read with Wait function - Thomas - 02.07.2019

Hello
I'm looking for a function which sends read request to KNX bus and waits for the result. The reason is I have two inputs which I need to be sure their values are actual.
This works but it's ugly because I don't check if the readings of both values were successful:

grp.read('6/4/28')
grp.read('6/4/45')
os.sleep(5)

val_a=grp.getvalue('6/4/28')
val_b=grp.getvalue('6/4/45')


Is there such
err,val_a=grp.readAndWait('6/4/28')
?


RE: Read with Wait function - admin - 03.07.2019

Use this function:
Code:
function readresponse(alias, timeout)
  local obj, lb, ts, tu, delta, value
  obj = grp.find(alias)

  if not obj then
    return nil, 'object not found'
  end

  timeout = timeout or 3

  lb = require('localbus').new(timeout / 10)
  lb:sethandler('groupresponse', function(event)
    if event.dst == obj.address then
      value = busdatatype.decode(event.datahex, obj.datatype)
    end
  end)

  grp.read(obj.address)
  ts, tu = os.microtime()

  repeat
    lb:step()
    delta = os.udifftime(ts, tu)
  until value ~= nil or delta >= timeout

  if value ~= nil then
    return value
  else
    return nil, 'timeout'
  end
end

res, err = readresponse('0/0/3')
log(res, err)



RE: Read with Wait function - Thomas - 03.07.2019

Excellent as usual Admin. Thank you.
But does lb:step() do yield? Isn't the cycle utilising the CPU to 100%?


RE: Read with Wait function - admin - 03.07.2019

It returns once a single local bus message has been received or timeout happens. Script does not consume CPU while waiting.


RE: Read with Wait function - benanderson_475 - 19.07.2019

Is it possible for the timeout to re-trigger and over ride the last timeout every time the function i called? i am having a few instances running at the same time


RE: Read with Wait function - admin - 23.07.2019

Do you mean that you want to send several read requests at once and then return once all responses are received or timeout happens?