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.

Read with Wait function
#1
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')
?
LM5Lp, firmware: 2018.08.22, FlashSYS v2, ARMv7 Processor rev 5 (v7l), kernel 4.4.151





Reply
#2
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)
Reply
#3
Excellent as usual Admin. Thank you.
But does lb:step() do yield? Isn't the cycle utilising the CPU to 100%?
LM5Lp, firmware: 2018.08.22, FlashSYS v2, ARMv7 Processor rev 5 (v7l), kernel 4.4.151





Reply
#4
It returns once a single local bus message has been received or timeout happens. Script does not consume CPU while waiting.
Reply
#5
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
Reply
#6
Do you mean that you want to send several read requests at once and then return once all responses are received or timeout happens?
Reply


Forum Jump: