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 that you accept these cookies being set.

How to share file descriptor between scripts
#1
Hi All,

I have to open file descriptor once at system init and share it between all scripts. But the following approach doesn't work because this type of data looks like not supported by storage.
How to get it working?
Thanks

init script:

local mb = luamodbus.rtu()
mb:open('/dev/RS485-1', 38400, 'N', 8, 1, 'H')
mb:connect()
storage.set('f_mb_rtu_1', mb)

resident script:

local mb =  storage.get('f_mb_rtu_1')
mbConfusedetslave(6)
mb:writebits(0, true)


-- but result is nil due to wrong mb
Reply
#2
use something like this in a resident script

Code:
if not start then
  -- your init code here
  start = true
end

--code
Reply
#3
Each script is a separate OS process. File descriptors are private for each process and cannot be shared.

Can you explain your task? Are you trying to share access to the same RS-485 port between several scripts? You can use semaphores (http://openrb.com/docs/semaphore.htm) for locking or just create a profile and use modbus mapper which will take care of data exchange.
Reply
#4
Yes, I meant sharing access to the same RS-485 port between several scripts. Currently it's done as you wrote. Opening/closing the same file every time the script run seems to be excessively, but if there is no ability to open file once from the init process and them share descriptor to the all childs, question is eliminated

Thank you for answer!
Reply
#5
Performance hit for every open/close is minimal. The only problem you might have is when several scripts are trying to access the bus at once.

Another solution is to have a single resident script with bus access and other scripts communicating with the resident internally. I'll post our modbus proxy example later on.
Reply


Forum Jump: