V
V
Vlad Zaitsev2015-07-15 17:55:56
Programming
Vlad Zaitsev, 2015-07-15 17:55:56

How to put a set of commands into a separate function in lua?

There is this code in lua:

require('sem')
semaphore = sem.open('RS485-3')
timeout = 10 * 2 
while not semaphore:trywait() and timeout > 0 do 
  sleep(0.5)
  timeout = timeout - 1
end

semaphore:post()
sem_local:close()

Implements semaphores so that different threads do not compete for a port. The problem is that these particular pieces of code are repeated in this form quite often in different scripts, and it would be nice to put them in one place so as not to copy back and forth. Yes, and edit if anything - in one place, and not in many. Ideally, I want functions like semaphore_manager:wait and semaphore_manager:release instead of these lines of code.
Tried to do something like this:
function semaphore_manager:wait(name)
require('sem')
semaphore = sem.open(name)
timeout = 10 * 2 
while not semaphore:trywait() and timeout > 0 do 
  sleep(0.5)
  timeout = timeout - 1
end 
return semaphore
end

function semaphore_manager:wait(sem_local)
sem_local:post()
sem_local:close()
end

but somehow it didn’t work out, he swears indistinctly. "loop or previous error loading module 'user.sem_manager'" and "attempt to index global 'sem_manager' (a nil value)"
I understand that this should be done in the sem library itself, which implements this, but there is no access to it , but I want order in the code.
And after the question about the same thing. I am making this function:
function modbus(action, slave)
  if (action == 'connect') then
    require('luamodbus')
    mb = luamodbus.rtu()
    mb:open('/dev/RS485-3', 115200, 'N', 8, 1, 'H')
    mb:connect()
    mb:setslave(slave)
    local amount,  modbus_error = mb:readregisters(1)
    return amount, modbus_error
  elseif (action == 'disconnect') then
    mb:close()
  end
end

And call her accordingly
count_sensors, m_error = modbus('connect', slave_devices[i])
  modbus('disconnect')

Am I doing the right thing? It seems to work, but sometimes it glitches. Could this be the issue?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Misbakh-Soloviev, 2016-07-28
@mva

1) for starters, you should not call requireinside functions.
2) further, don't pollute the global space (and tell the author of the 'sem' module about the same if its module can only export to the global space, and not return a method table).
3) (variation of item 2) use local variables as often as possible, especially inside functions. Use globals only where absolutely necessary. Otherwise, you will run into sudden collisions.
4) I have, in general, a lot of complaints about your code in both inserts. From their number, I just want to take and rewrite, without explaining anything, and then answer all questions about why this is so :)
5)

Am I doing the right thing

Given all of the above, it should look very different.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question